This seems so basic that I can't believe I haven't found a simple solution. I am trying to evaluate a json string representation in python which contains backslash delimited double quote marks and boolean values.
eg s = "{\"data\":true}"
.
This is valid json in javascript as JSON.parse("{\"data\":true}")
returns a valid object.
I've tried multiple methods, none of which provide the desired solution of a pythonised string representation of a json object:
Using eval(s)
gives NameError: name 'true' is not defined
.
Using ast.literal_eval(s)
gives ValueError: malformed string
.
Using json.dumps(json.loads(s))
returns the same format of string.
The output I want is "{'data':True}"
as my neo4j database does not recognise \
as a delimiter for storing purposes and therefore produces an error when attempting to store the initial format. I am trying to avoid a hard replace of \"
with '
or true
with True
as there must be a better, faster and easier solution to this.
Use json.loads :
import json
result = str(json.loads("{\"data\":true}"))
print(result, type(result))
Output
{'data': True} <class 'str'>
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.