I'm trying to convert a python dictionary to the target JSON object below. I figured I'd use json.dumps() (as per this thread) but the result is not the same nevertheless. The target has some unconvential spacing in it, but I'm not allowed to change it or edit them out.
Any idea how to approach this?
import json
dict= {"token":{"name":"John Doe","code":"123456789"}}
target = '{ "token":{ "name":"John Doe", "code":"123456789" } }'
print(json.dumps(dict))
print(json.loads(json.dumps(dict)))
print(target)
>>>{"token": {"name": "John Doe", "code": "123456789"}}
>>>{'token': {'name': 'John Doe', 'code': '123456789'}}
>>>{ "token":{ "name":"John Doe", "code":"123456789" } }
For additional context, I'm trying to prepare the argument passed through Bambora's payment API. See the cURL example associated to this here.
json.loads(json.dumps(dict)) == json.loads(target)dumpswith default settings makes some particular assumptions about where whitespace does and does not go that are not matched by the string you have intarget. You can printjson.dumps(dict)to see what it's actually producing. Separately, I would recommend against using Python keywords likedictas variable names.json.dumps(). You could read the target string withjson.loadsand then dump it back withjson.dumps(). Then you should be able to do the comparison of the two strings and get a useful result.