FlyingPickle
FlyingPickle

Reputation: 1133

python converting dict to JSON

I have a python dict in the following format

{'status': ['Done'], 'urgency': 1, 'text': {'shorttext': 'Short Text', 'longtext': 'Long Text'}, 'startdate': '2019-03-03', 'enddate': '2019-03-03'}

which when I convert to json using json_dumps

obj=json_dumps(dict)
print(obj)

'{"status": ["Done"], "urgency": 1, "text": {"shorttext": "Short Text", "longtext": "Long Text"}, "startdate": "2019-03-03", "enddate": "2019-03-03"}'

Now when I try to post this payload to an api using request.post call in the format below

requests.post(url, headers, json=obj)

I get the below error

no String-argument constructor/factory method to deserialize from String value (\'{"status": ["Done"], "urgency": 1, "text": {"shorttext": "Short Text", "longtext": "Long Text"}, "startdate": "2019-03-03", "enddate": "2019-03-03"}'\)

Any inputs on what might be causing this? I suspect is the '' the payload is enclosed in, but am not completely sure. Thanks!

Upvotes: 0

Views: 174

Answers (1)

Kshitij Saxena
Kshitij Saxena

Reputation: 940

Don't dump the the dict!

requests.post(url, headers, json=dict)

Or, if you have to:

requests.post(url, headers, data=obj)

Upvotes: 6

Related Questions