Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Escaping single quotes JSON

Tags:

python

json

I am using the json module to convert a series of dictionaries to json but I am not sure why when dumping to json the strings that contain a single quote (e.g: My father's car) are escaped to (My father\'s car). When I check in an online validator it saying that the format is wrong. Why dumps escape them when it is not correct?

I tried to replace the strings using replace but it does not act of them. Would that be a valid solution? Why is not working the following snipped

formatted_json = json.dumps(OrderedDict([("nodes", json_graph['nodes']), ("links", json_graph['links'])])).replace('\'',"'")

Thanks!

like image 479
ddomingo Avatar asked Jan 04 '23 10:01

ddomingo


1 Answers

You probably see the value representation from python interpreter. If you print the value or store it in a file, you'll see the correct behaviour.

>>> import json
>>> json.dumps("a'b")
'"a\'b"'
>>> print json.dumps("a'b")
"a'b"
like image 99
JuniorCompressor Avatar answered Jan 07 '23 11:01

JuniorCompressor