0

I am using a site's REST API's and have been primarily using Python's 'requests' module to GET json responses. The goal of the GET requests are to ultimately pull a user's form response which ends up being a complex json document. To deal with this:

user_form_submission = requests.get('https://www.url/doc.json',
        auth = (api_key, secret),
        params = params)

python_obj = json.loads(user_form_submission.text)
trimmed_dict = python_obj['key'][0]['keys']

For context, this is what trimmed_dict would look like formatted as .json:

{
    "Date": { "value": "2020-04-26", "type": "date" },
    "Location": {
      "value": "Test ",
      "type": "text",
      "geostamp": "lat=34.00000, long=-77.00000, alt=17.986118, hAccuracy=65.000000, vAccuracy=10.000000, timestamp=2020-04-26T23:39:56Z"
    },
    "form": {
      "value": [
        {
          "form_Details": {
            "value": [
              {
                "code": {
                  "value": "0000000000",
                  "type": "barcode"
                },
                "Name": { "value": "bob", "type": "text" }
              }
            ],
            "type": "group"
          },
          "Subtotal": { "value": "4", "type": "decimal" },
          "form_detail2": {
            "value": [
              {
                "name": {
                  "value": "billy",
                  "type": "text"
                },
                "code": {
                  "value": "00101001",
                  "type": "barcode"
                },
                "Classification": {
                  "value": "person",
                  "type": "select1"
                },
                "Start_Time": { "value": "19:43:00", "type": "time" },
                "time": { "value": "4", "type": "decimal" }
                }
            ],
      "type": "subform"}
        }
    ]
    }
}

Now I have a portion of the json that contains both the useful and useless. From this point, can I pass this obj in a POST? I've tried every way that I can think of approaching it, and have been shut down.

Understanding how I want to go about this, this is how I thought it would go:

json_post = requests.post(' https://url/api/doc.json', 
        auth = (api_key, secret), 
        json = {
            "form_id" : 'https://url.form.com/formid',
            'payload':{
                 json.dumps(trimmed_dict)
             }})

But, when I do this, I get the following error --

    TypeError: Object of type set is not JSON serializable

How can I push this dict through this POST? If there's a more effective way of going about it, I am very open to suggestion.

3
  • 2
    did you try converting json data into python dict and use that dict value instead of json variable name? Commented Apr 27, 2020 at 18:17
  • Well that's what json.loads does, right? It takes a json doc and converts it into whatever the appropriate python objs are from the json structure. python_obj is type dict in my example. Are you asking if I tried to pass the entire python_obj before trimming it down? No, I haven't, I assume it will raise 'set cannot be json serializable' but I'll go ahead and test it for the heck of it. Commented Apr 28, 2020 at 0:14
  • ``` json_post = requests.post(' url/api/doc.json', auth = (api_key, secret), json = { "form_id" : 'url.form.com/formid', 'payload':{ json.dumps(python_obj) }}) ``` raised TypeError: Object of type set is not JSON serializable Commented Apr 28, 2020 at 0:15

2 Answers 2

1

Try removing the curly braces around json.dumps(trimmed_dict). json.dumps turns your trimmed_dict into a string, which becomes a python set when surrounded with braces.

Additionally you could remove json.dumps and plug the trimmed_dict into the structure directly as the value associated with payload.

Sign up to request clarification or add additional context in comments.

1 Comment

I'll be damned. I assumed since the content type of the post was json that the body would have to be formatted as such before the request. But nope, so I tried exactly what you suggested and ' 'payload':trimmed_dict ' gave back a 201 server status, and ' 'payload': json.dumps(trimmed_dict) ' 400 me. The post crashes the app that it goes too, but it's a step in the right direction. Thanks a lot!
1

Remove the extra {} from the payload. payload itself is a key and json.dumps(trimmed_dict) as a value is enough

json_post = requests.post(' https://url/api/doc.json', 
        auth = (api_key, secret), 
        json = {
            "form_id" : 'https://url.form.com/formid',
            "payload": json.dumps(trimmed_dict)
             })

1 Comment

Tried this out and it's definitely the way I should have approached this, but the server is responding with a 400 towards it. When I just use the python obj without the json.dumps serializing it, server is sends a 201. Not entirely sure why yet, but thank you for the suggestion!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.