0

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?

Genesis
  • 3
  • 3
  • 2
    For sure this is a duplicate https://stackoverflow.com/questions/20199126/reading-json-from-a-file or https://stackoverflow.com/questions/12309269/how-do-i-write-json-data-to-a-file – dosas May 12 '21 at 12:29
  • Does this answer your question? [Convert JS object to JSON string](https://stackoverflow.com/questions/4162749/convert-js-object-to-json-string) – KnowledgeGainer May 12 '21 at 12:48
  • Does this answer your question? [How do I write JSON data to a file?](https://stackoverflow.com/questions/12309269/how-do-i-write-json-data-to-a-file) – Pranav Singh May 13 '21 at 03:52

2 Answers2

0

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)
Siddhartha
  • 311
  • 3
  • 8
0

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

Ammar
  • 1,305
  • 2
  • 11
  • 16