I have a python app that send a strange array of objects to my node js app in string format like this :
"[{'foo':'bar'},{'hello':'world'}]"
I am trying to parse it with JSON.parse()
but I get errors :
SyntaxError: Unexpected token ' in JSON at position 1
On python side the array is recognized as a true array list but when I pass it to nodejs with python shell , this become a stringified array of objects.
Any suggestion to parse it ?
JSON.parse() will not accept single quotes as string boundaries. This string, using double quotes, works fine:
console.log(JSON.parse('[{"foo":"bar"},{"hello":"world"}]'));
If it is impossible to change the Python app, you may replace the single quotes to double quotes before JSON parsing the string.
How about using json.load()?
import json
inp = "[{'foo':'bar'},{'hello':'world'}]"
a = json.loads(inp.replace("'",'"'))
print(a)
Output (list of dictionaries) :
[{'foo': 'bar'}, {'hello': 'world'}]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.