Last updated on August 27, 2020 by Dan Nanni
When you are developing a web application, you may often deal with JSON-formatted input/output. When you are inspecting such JSON-formatted data, you may want to see it in a human-readable format (e.g., with proper indentation and line breaks).
In this case, you can use Python's JSON tool. This tool is used to validate and pretty-print
any JSON-formatted text. It can be invoked with Python interpreter with -m
option.
For example:
$ JSON_STRING='{"name":"Google","location":{"street":"1600 Amphitheatre Parkway","city":"Mountain View","state":"California","country": "US"},"employees":[{"name":"Michael","division":"Engineering"},{"name":"Laura","division":"HR"},{"name":"Elise","division":"Marketing"}]}' $ echo $JSON_STRING | python -mjson.tool
{ "employees": [ { "division": "Engineering", "name": "Michael" }, { "division": "HR", "name": "Laura" }, { "division": "Marketing", "name": "Elise" } ], "location": { "city": "Mountain View", "country": "US", "state": "California", "street": "1600 Amphitheatre Parkway" }, "name": "Google" }
If you are looking to extract individual elements from a JSON-formatted string, you may want to use a more powerful JSON parsing tool such as jq
.
This website is made possible by minimal ads and your gracious donation via PayPal or credit card
Please note that this article is published by Xmodulo.com under a Creative Commons Attribution-ShareAlike 3.0 Unported License. If you would like to use the whole or any part of this article, you need to cite this web page at Xmodulo.com as the original source.
Xmodulo © 2021 ‒ About ‒ Write for Us ‒ Feed ‒ Powered by DigitalOcean