I have this json file:[ {"gy":"1","te":"ggjf" }, {"gy":"2","te":"hgfjm" } ]
can you help me convert this json array of object to string?
I have this json file:[ {"gy":"1","te":"ggjf" }, {"gy":"2","te":"hgfjm" } ]
can you help me convert this json array of object to string?
Assuming you have this data stored in a file named file.json
, you can convert it into a string using:
import json
file = open('file.json')
data = json.load(file)
file.close()
string = json.dumps(data)
print(string)
so I did not really get what is the desired output, a string or an array.
so if you have:
>>> a = '[ {"gy":"1","te":"ggjf" }, {"gy":"2","te":"hgfjm" } ]'
if you want the array:
>>> import json
>>> json.loads(a)
[{'gy': '1', 'te': 'ggjf'}, {'gy': '2', 'te': 'hgfjm'}]
if you want the string:
>>> import json
>>> json.dumps(json.loads(a))
'[{"gy": "1", "te": "ggjf"}, {"gy": "2", "te": "hgfjm"}]'
if none of the above is your desired output, please clearify more :-)
official json docs: https://docs.python.org/3/library/json.html?highlight=json#module-json