I have some code that calls an API with JSON data and the Requests library in Python, it works well when the JSON strings are hard coded. When the JSON data needs to be dynamic I decided to go with dumping a python object to JSON via json.dumps()
as follows:
def testJSON(testVar): #set testVar = 22.22
data = {}
data['Key1'] = 'Value1'
data['Key2'] = 'Value2'
data['Key3'] = str(testVar)
params = json.dumps(data)
print params
params2 = {"Key1":"Value1","Key2":"Value2","Key3":"22.22"}
print params2
when I run this from the command line the output is as follows:
{"Key1":"Value1","Key2":"Value2","Key3":"22.22"}
{'Key1':'Value1','Key2':'Value2','Key3':'22.22'}
The hard coded JSON string params2 works as expected in an API call but the object dumped to JSON with json.dumps()
doesn't. The output of both these methods look the same except for single and double quotes, which shouldn't matter, right?