Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a nested JSON request with Python

A user needs to pass a json object as a part of the request. It would 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. First, I can't figure out how to test this code by recreating the nested POST. I can successfully POST a dict but posting the list of addresses within the JSON object is messing me up.

Simply using cURL, how might I do this? How might I do it with urrlib2?

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

Any tips?

like image 574
Ben Avatar asked Oct 10 '22 21:10

Ben


1 Answers

First make sure your JSON is valid. Paste it into the JSONLint web page.

Currently your JSON has two issues:

  1. there is no comma between "token" :"ayaljltja" and "addresses": [...]
  2. a single quote is not a valid way of delimiting a JSON string, replace them all with double quotes.
like image 161
JeremyP Avatar answered Oct 13 '22 10:10

JeremyP