Converting Python Dictionary To Json Array
I currently have a Python Dictionary that looks something like this: OrderedDict([('2017-07-24', 149.7619), ('2017-07-25', 150.4019), ('2017-07-26', 151.1109), ... that I am conve
Solution 1:
json.dumps(list(priceDict.items()))
But why do you have an OrderedDict
in first place? If you pass the same list you passed to OrderedDict
to json.dumps
it will generate your array:
json.dumps([('2017-07-24', 149.7619), ('2017-07-25', 150.4019),....])
No need for OrderedDict
in this case
Solution 2:
If you want to convert a Python Dictionary to JSON using the json.dumps() method.
`
import json
from decimal import Decimal
d = {}
d["date"] = "2017-07-24"
d["quantity"] = "149.7619"print json.dumps(d, ensure_ascii=False)
`
Solution 3:
I removed the OrderedDict but kept all the other data, I think I understand the request. See if this works for you:
import json
my_dict = ([('2017-07-24', 149.7619), ('2017-07-25', 150.4019), ('2017-07-26', 151.1109)])
print(json.dumps(my_dict, indent=4, sort_keys=True))
Post a Comment for "Converting Python Dictionary To Json Array"