JSON: Difference between revisions
From charlesreid1
(Created page with "to print out pretty json using Python: <pre> d = dict(a=1,b=2,c=3) with open('pretty.json','w') as f: json.dump(d, f, separators=(',',': '),indent=4) </pre>") |
No edit summary |
||
| (3 intermediate revisions by the same user not shown) | |||
| Line 1: | Line 1: | ||
==from command line== | |||
to prettify json in a one-liner, just pipe it to python's json.tool: | |||
<pre> | |||
echo '{"foo": "lorem", "bar": "ipsum"}' | python -m json.tool | |||
</pre> | |||
==from a python script== | |||
to print out pretty json using Python: | to print out pretty json using Python: | ||
| Line 6: | Line 17: | ||
json.dump(d, f, separators=(',',': '),indent=4) | json.dump(d, f, separators=(',',': '),indent=4) | ||
</pre> | </pre> | ||
[[Category:JSON]] | |||
[[Category:Python]] | |||
Latest revision as of 12:32, 23 April 2018
from command line
to prettify json in a one-liner, just pipe it to python's json.tool:
echo '{"foo": "lorem", "bar": "ipsum"}' | python -m json.tool
from a python script
to print out pretty json using Python:
d = dict(a=1,b=2,c=3)
with open('pretty.json','w') as f:
json.dump(d, f, separators=(',',': '),indent=4)