You don't dump a string but to a string.
d = {"foo": "bar"}
j = json.dumps(d)
print(j)
Output:
{"foo": "bar"}
It makes no sense to use a string as the argument of json.dumps which is expecting a dictionary or a list/tuple as the argument.
Answer from user1907906 on Stack OverflowSerialize
objto a JSON formatted str using this conversion table.
python - Using json.dumps(string) to dump a string - Stack Overflow
json.dump with python.dump
python json dumps - Stack Overflow
python - How to prettyprint a JSON file? - Stack Overflow
Videos
You don't dump a string but to a string.
d = {"foo": "bar"}
j = json.dumps(d)
print(j)
Output:
{"foo": "bar"}
It makes no sense to use a string as the argument of json.dumps which is expecting a dictionary or a list/tuple as the argument.
Serialize
objto a JSON formatted str using this conversion table.
In [17]: d = {"foo": "bar"}
In [18]: j = json.dumps(d)
In [19]: j
Out[19]: '{"foo": "bar"}'
Now if you need your dictionary back instead of a string
In [37]: l=json.loads(j)
In [38]: l
Out[38]: {u'foo': u'bar'}
or alternatively
In [20]: import ast
In [22]: k=ast.literal_eval(j)
In [23]: k
Out[23]: {'foo': 'bar'}
When you do json.dump you can pass data into the function and where to put it, and it creates a new json file. How could I do this but have the language be python when it dumps? I need to do this because json does not read tuples and other things I am wanting to save into the file. Thanks
This works but doesn't seem too elegant
import json
json.dumps(json.JSONDecoder().decode(str_w_quotes))
json.dumps thinks that the " is part of a the string, not part of the json formatting.
import json
json.dumps(json.load(str_w_quotes))
should give you:
[{"id": 2, "name": "squats", "wrs": [["55", 9]]}]
Use the indent= parameter of json.dump() or json.dumps() to specify how many spaces to indent by:
>>> import json
>>> your_json = '["foo", {"bar": ["baz", null, 1.0, 2]}]'
>>> parsed = json.loads(your_json)
>>> print(json.dumps(parsed, indent=4))
[
"foo",
{
"bar": [
"baz",
null,
1.0,
2
]
}
]
To parse a file, use json.load():
with open('filename.txt', 'r') as handle:
parsed = json.load(handle)
You can do this on the command line:
python3 -m json.tool some.json
(as already mentioned in the commentaries to the question, thanks to @Kai Petzke for the python3 suggestion).
Actually python is not my favourite tool as far as json processing on the command line is concerned. For simple pretty printing is ok, but if you want to manipulate the json it can become overcomplicated. You'd soon need to write a separate script-file, you could end up with maps whose keys are u"some-key" (python unicode), which makes selecting fields more difficult and doesn't really go in the direction of pretty-printing.
You can also use jq:
jq . some.json
and you get colors as a bonus (and way easier extendability).
Addendum: There is some confusion in the comments about using jq to process large JSON files on the one hand, and having a very large jq program on the other. For pretty-printing a file consisting of a single large JSON entity, the practical limitation is RAM. For pretty-printing a 2GB file consisting of a single array of real-world data, the "maximum resident set size" required for pretty-printing was 5GB (whether using jq 1.5 or 1.6). Note also that jq can be used from within python after pip install jq.