Reputation: 2311
I am using json dump in python with separtors like below:
json.dump(data, outputfile, sort_keys=True, indent=4, separators=(',', ':'))
But my data has few key value pairs in which itemseparator ',' is also present. I do not want to treat that as itemseparator
For example: {"withComma" : "1,23"} This should be treated as "withComma": ["1,23"] and NOT as "withComma":["1","23"]
Upvotes: 1
Views: 6946
Reputation: 14645
separators
does not affect the formatting of strings, only the formatting of objects.
As the documentation describes
If specified, separators should be an (item_separator, key_separator) tuple. By default, (', ', ': ') are used. To get the most compact JSON representation, you should specify (',', ':') to eliminate whitespace.
You can easily see this with some examples. Note the extra spaces around the :
in the second case
$ python -c 'import json; print json.dumps({"withComma":"1,23"})'
{"withComma": "1,23"}
$ python -c 'import json; print json.dumps({"withComma":"1,23"}, separators=(" , ", " : "))'
{"withComma" : "1,23"}
If you require string values like "1,23"
to be converted to array values like ["1,23"]
or ["1", "23"]
you're going to need to do that on your own before calling json.dump
Upvotes: 3