With Python 2.6+ or 3 you can use the json.tool module:

echo '{"foo": "lorem", "bar": "ipsum"}' | python -m json.tool

or, if the JSON is in a file, you can do:

python -m json.tool my_json.json

if the JSON is from an internet source such as an API, you can use

curl http://my_url/ | python -m json.tool

For convenience in all of these cases you can make an alias:

alias prettyjson='python -m json.tool'

For even more convenience with a bit more typing to get it ready:

prettyjson_s() {
    echo "$1" | python -m json.tool
}

prettyjson_f() {
    python -m json.tool "$1"
}

prettyjson_w() {
    curl "$1" | python -m json.tool
}

for all the above cases. You can put this in .bashrc and it will be available every time in shell. Invoke it like prettyjson_s '{"foo": "lorem", "bar": "ipsum"}'.

Note that as @pnd pointed out in the comments below, in Python 3.5+ the JSON object is no longer sorted by default. To sort, add the --sort-keys flag to the end. I.e. ... | python -m json.tool --sort-keys.

Another useful option might be --no-ensure-ascii which disables escaping of non-ASCII characters (new in version 3.9).

Top answer
1 of 16
5227

With Python 2.6+ or 3 you can use the json.tool module:

echo '{"foo": "lorem", "bar": "ipsum"}' | python -m json.tool

or, if the JSON is in a file, you can do:

python -m json.tool my_json.json

if the JSON is from an internet source such as an API, you can use

curl http://my_url/ | python -m json.tool

For convenience in all of these cases you can make an alias:

alias prettyjson='python -m json.tool'

For even more convenience with a bit more typing to get it ready:

prettyjson_s() {
    echo "$1" | python -m json.tool
}

prettyjson_f() {
    python -m json.tool "$1"
}

prettyjson_w() {
    curl "$1" | python -m json.tool
}

for all the above cases. You can put this in .bashrc and it will be available every time in shell. Invoke it like prettyjson_s '{"foo": "lorem", "bar": "ipsum"}'.

Note that as @pnd pointed out in the comments below, in Python 3.5+ the JSON object is no longer sorted by default. To sort, add the --sort-keys flag to the end. I.e. ... | python -m json.tool --sort-keys.

Another useful option might be --no-ensure-ascii which disables escaping of non-ASCII characters (new in version 3.9).

2 of 16
1654

You can use: jq

It's very simple to use and it works great! It can handle very large JSON structures, including streams. You can find their tutorials here.

Usage examples:

$ jq --color-output . file1.json file1.json | less -R

$ command_with_json_output | jq .

$ jq # stdin/"interactive" mode, just enter some JSON

$ jq <<< '{ "foo": "lorem", "bar": "ipsum" }'
{
  "bar": "ipsum",
  "foo": "lorem"
}

Or use jq with identity filter:

$ jq '.foo' <<< '{ "foo": "lorem", "bar": "ipsum" }'
"lorem"
🌐
GitHub
github.com › DevUtilsApp › jsonformatter.app
GitHub - DevUtilsApp/jsonformatter.app: JSON Formatter offline macOS app
You can format your JSON strings from anywhere in your macOS (terminal, in email, web browser,...).DevUtils will inspect your clipboard content and automatically select the JSON Formatter/Validator tool if the content is a valid JSON.
Starred by 7 users
Forked by 2 users
Languages   HTML 100.0% | HTML 100.0%
🌐
MacBlog
macblog.org › parse-json-command-line-mac
How to Parse JSON on the macOS Command Line Without External Tools Using JavaScript for Automation - MacBlog
November 24, 2021 - One way to parse JSON on the macOS command line, using only native tooling without external dependencies like jq or similar.
🌐
CodeJava
codejava.net › coding › prettify-json-with-curl-windows-and-macos
How to Prettify JSON with curl (Windows and macOS)
On macOS, you don’t have to install extra software as macOS comes with pre-installed tools that can format JSON, such as Python and json_pp. So in the terminal you just type:
🌐
JSON Formatter
jsonformatter.org › cb7da0
Terminal data JSON
Free JSON Formatting Online and JSON Validator work well in Windows, Mac, Linux, Chrome, Firefox, Safari, and Edge.
🌐
Okjson
okjson.app
Scriptable JSON Formatter for macOS - OK JSON
Apple Silicon & macOS Tahoe ready! The ultimate JSON formatter for your Mac.
🌐
DEV Community
dev.to › prasmalla › prettyprint-json-in-osx-cli-3h64
PrettyPrint JSON in OSX cli - DEV Community
May 13, 2023 - Now we can pipe our API call to json.tool for formatting and pygments for JS like colorized output curl -X GET ‘https://api.printful.com/store/products/307743308' | python -m json.tool | pygmentize -l javascript
🌐
App Store
apps.apple.com › us › app › ok-json-viewer-formatter › id1576121509
OK JSON - Viewer & Formatter App - App Store
OK JSON is a native and scriptable JSON formatter on macOS. It’s really fast and well-integrated with macOS. The notable features from OK JSON are: - JSON structure in both tree view and text editor.
Rating: 0 ​
Find elsewhere
🌐
Jsonviewer
jsonviewer.app
JSON Viewer
Requires macOS 10.14 or later · Visualize · Easily read JSON with syntax highlighting in a familiar color scheme · Interact · Navigate through large datasets by expanding and collapsing nodes · Format · Prettify or minify JSON in a single click · Validate · Spot invalid syntax with automatic error highlighting ·
🌐
Kyle Howells Blog
ikyle.me › blog › 2024 › pretty-print-json-terminal
Pretty Print JSON in the Terminal - Kyle Howells
August 8, 2024 - Instead of trying to read that densely packed json text, this is how to pretty print the json on the terminal instead.
🌐
Der Flounder
derflounder.wordpress.com › 2023 › 04 › 15 › using-the-plutil-command-line-tool-to-work-with-json-on-macos-monterey-and-later
Using the plutil command line tool to work with JSON on macOS Monterey and later | Der Flounder
April 15, 2023 - Something to be aware of is that there will be some limitations to this technique. plutil is designed to work with plist files, which means that if something isn’t formatted like it expects, plutil may not know how to handle it. Two limitations I know of are these: NULL values in JSON files – There’s no equivalent for NULL in plist files, so the plutil tool will fail to convert JSON files with NULL values to a plist file in XML format.
🌐
GitHub
github.com › cucumber › json-formatter
GitHub - cucumber/json-formatter: Provides a language-agnostic command-line tool to convert cucumber messages into a JSON document. · GitHub
Download cucumber-json-formatter-darwin-amd64 and rename it to cucumber-json-formatter ... At the last step, you may get a security warning from MacOS. If you do, open System Preferences. Go to Security Settings. You should see a question asking if you want to open it anyway.
Starred by 24 users
Forked by 8 users
Languages   Go 91.0% | Makefile 8.2% | Shell 0.8%
🌐
Jontsai
jontsai.com › 2017 › 10 › 09 › how-to-pretty-print-json-from-cli
How to pretty print JSON from CLI - Jonathan Tsai
$ echo '{"cool": { "story" : { "bro" : [1, 2, 3] } } }' | json { "cool": { "story": { "bro": [ 1, 2, 3, ] } } }
🌐
Quilime
quilime.com › mac_os_print_json
quilime > print formatted json, xml from macos command line
Working with various interfaces that output json or xml results in lots of situations where you have a single-line, unformatted output. Here’s a quick way to format json, xml from the CLI using Python or jq. ... xmllint is part of libxml2 and installed by default on OSX. Be aware that xmllint cleans up XML as well as formatting it, ocassionally modifying the output.
🌐
Ben Scheirman
benscheirman.com › 2011 › 12 › formatting-json-from-terminal.html
Formatting JSON From Terminal | Ben Scheirman
December 13, 2011 - Usually for this I just pipe the command into pbcopy to get it on my clipboard, then I visit JSON Lint to make it easy to read. I looked at ways of doing this all from the Terminal, and came across this python command: ... It takes input from stdin and outputs it formatted nicely.
🌐
GitHub
github.com › wiztools › json-validator
GitHub - wiztools/json-validator: Cli Json validator and formatter · GitHub
Mac users can use homebrew to install json-validator: brew tap wiztools/repo brew install json-validator ... $ java -jar json-validator-fat-VERSION.jar -h Usage: json-validator [options] [files] When files are not given, STDIN is read for input. Supported options are: --help Display this help. --noout Do not print formatted JSON to STDOUT.
Author   wiztools