Creating a nested JSON request using Python

The user must pass the json object as part of the request. It will look something like this:

{"token" :"ayaljltja", "addresses": [ {'name':'Home','address':'20 Main Street', 'city':'new-york'}, {'name':'work', 'address':'x Street', 'city':'ohio'} ]} 

I have two problems right now. Firstly, I cannot figure out how to verify this code by re-creating the nested POST. I can successfully execute a POST dict, but posting a list of addresses in a JSON object is causing an error.

Just using cURL, how can I do this? How can I do this with urrlib2?

My second problem is deserializing the server side JSON POST object. I guess I just need to see a successful POST to determine the input (and then deserialize it using the json module).

Any tips?

+4
source share
2 answers

First make sure your JSON is valid. Paste it into your JSONLint webpage .

Your JSON currently has two problems:

  • there is no comma between "token" :"ayaljltja" and "addresses": [...]
  • a single quote is not a valid way to delimit a JSON string, replace them with double quotes.
+1
source

With the command line freezing, save the JSON to a file, say data.json. Then try: curl -X POST -d @data.json http://your.service.url

It is also possible to enter JSON directly in the -d option, but (you think you already know) you need to specify your citation and acceleration exactly.

+2
source

All Articles