Reputation: 1023
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!
Upvotes: 5
Views: 8093
Reputation: 20015
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"
Upvotes: 9