Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nested Dictionaries to JSON for the POST request Python

I am having trouble converting the payload data in the form of nested dictionaries to pass it as a data for the POST request using Python requests module. The form data is as below:

payload = {'request':  {
                'appkey': "936725A4-7D9A-11E5-81AC-86EC8D89CD5A"},
            'formdata':{
                    'currency':'US',
                    'dataview':'store_default',
                    'distinct':'_distance, clientkey',
                    'geolocs':{
                            'geoloc':[{
                                    '0':{
                                            'address1':'',
                                            'addressline':'19128, PA',
                                            'city':'Philadelphia',
                                            'country':'US',
                                            'latitude':'40.0532987',
                                            'longitude':'-75.23040379999998',
                                            'postalcode':'19128',
                                            'province':'',
                                            'state':'PA'}}]
                            },
                    'google_autocomplete':'true',
                    'limit':'250',
                    'nobf':'1',
                    'searchradius':'15|25|50|100|250|350|450|550|650|750|850|950',
                    'true':'1',
                    'where':{'partner_reseller': {'eq':'1'}}}                    
          }

r = requests.post(url,data=simplejson.dumps(payload),headers=header)
result = simplejson.loads(str(r.content))

Can somebody please assist me with structure and can point out the mistake in what I have written. I keep getting the following error:

{'code': 1008,
 'response': {'message': 'The submitted XML is not properly formed'}} 

I'll appreciate your help a lot. Thank you.

like image 369
Krishnang Dalal Avatar asked Apr 25 '17 01:04

Krishnang Dalal


People also ask

Can we convert dictionary to JSON in Python?

Python possesses a default module, 'json,' with an in-built function named dumps() to convert the dictionary into a JSON object by importing the "json" module. "json" module makes it easy to parse the JSON strings which contain the JSON object.

Can Python dictionaries be nested?

In Python, a Nested dictionary can be created by placing the comma-separated dictionaries enclosed within braces.

How do I call a nested json file in Python?

In Summary Python has built in functions that easily imports JSON files as a Python dictionary or a Pandas dataframe. Use pd. read_json() to load simple JSONs and pd. json_normalize() to load nested JSONs.

How do I store a dictionary in JSON?

You can save the Python dictionary into JSON files using a built-in module json. We need to use json. dump() method to do this. Use the indent parameter to prettyPrint your JSON data.


2 Answers

I had similar problem, frustrating but I solved it. Python requests do not work with nested json, they expect one layer json. It processes like form(application/x-www-form-urlencoded)

# Wrong
data = {'param1': {'a':[100, 200]},
        'param2': 'value2',
        'param3': False}

# You have to convert values into string:
data = {'param1': json.dumps({'a':[100, 200]}),
        'param2': 'value2',
        'param3': json.dumps(False)}

In your case:

import json
params = {
        'appkey': "936725A4-7D9A-11E5-81AC-86EC8D89CD5A"},
        'formdata':{
            'currency':'US',
            'dataview':'store_default',
            'distinct':'_distance, clientkey',
            'geolocs':{
                    'geoloc':[{
                            '0':{
                                    'address1':'',
                                    'addressline':'19128, PA',
                                    'city':'Philadelphia',
                                    'country':'US',
                                    'latitude':'40.0532987',
                                    'longitude':'-75.23040379999998',
                                    'postalcode':'19128',
                                    'province':'',
                                    'state':'PA'}}]
                    },
            'google_autocomplete':'true',
            'limit':'250',
            'nobf':'1',
            'searchradius':'15|25|50|100|250|350|450|550|650|750|850|950',
            'true':'1',
            'where':{'partner_reseller': {'eq':'1'}}}                    
          }
payload = {'request':  json.dumps(params) }

r = requests.post(url,data=payload) # important to keep payload as json!!!
result = r.text # or depends what the return is..
like image 156
NotTooTechy Avatar answered Oct 30 '22 14:10

NotTooTechy


My suggestion is to use the JSON parameter and let requests both encode the object to JSON and let requests set the Content-Type header to application/json.

It's very possible that the web service assumes you're passing it XML, unless you specify that you're passing JSON, via setting the Content-Type to application/json. (It's also possible this web API really wants XML too, the docs for the service would tell you)

requests.post(url,json=payload,headers=header)

like image 9
RyanWilcox Avatar answered Oct 30 '22 15:10

RyanWilcox