Learn Docker With My Newest Course

Dive into Docker takes you from "What is Docker?" to confidently applying Docker to your own projects. It's packed with best practices and examples. Start Learning Docker →

Pretty Print JSON in Your Terminal with jq or Python

pretty-print-json-in-your-terminal-with-jq-or-python.jpg

We'll go over creating a script to read in JSON from a file or your clipboard complete with multi-line syntax highlighting.

Quick Jump:

I like using jq instead of Python for this because you also get syntax highlighting without needing any additional dependencies, but we’ll cover using Python too in case you don’t want to install jq. In the end we’ll create a ppjson script that uses jq under the hood.

I recently created the script because I found myself wanting to skim and parse a few hundred lines of JSON that was painful to read on 1 line and I got tired of using online pretty printers and having to check if they send your JSON to their server.

Before getting to the script let’s go over using the raw commands for 2 different use cases such as pretty printing JSON from a file and what’s in your clipboard.

We’ll be able to convert something like this:

{"glossary":{"title":"example glossary","GlossDiv":{"title":"S","GlossList":{"GlossEntry":{"ID":"SGML","SortAs":"SGML","GlossTerm":"Standard Generalized Markup Language","Acronym":"SGML","Abbrev":"ISO 8879:1986","GlossDef":{"para":"A meta-markup language, used to create markup languages such as DocBook.","GlossSeeAlso":["GML","XML"]},"GlossSee":"markup"}}}}}

Into this with 1 command:

{
  "glossary": {
    "title": "example glossary",
    "GlossDiv": {
      "title": "S",
      "GlossList": {
        "GlossEntry": {
          "ID": "SGML",
          "SortAs": "SGML",
          "GlossTerm": "Standard Generalized Markup Language",
          "Acronym": "SGML",
          "Abbrev": "ISO 8879:1986",
          "GlossDef": {
            "para": "A meta-markup language, used to create markup languages such as DocBook.",
            "GlossSeeAlso": [
              "GML",
              "XML"
            ]
          },
          "GlossSee": "markup"
        }
      }
    }
  }
}

The syntax highlighting will look even nicer with jq too!

# Python

This could be handy to use on a system where jq isn’t or can’t be installed onto. Python is available on most systems by default so there’s nothing to install.

python3 -m json.tool data.json
python3 -m json.tool --indent 2 data.json

# Replace `xclip -out -selection clipboard` with `pbpaste` if you're on macOS
xclip -out -selection clipboard | python3 -m json.tool
xclip -out -selection clipboard | python3 -m json.tool --indent 2

# jq

You can sudo apt-get install jq, brew install jq, etc. it on most systems.

jq < data.json
jq --indent 4 < data.json

# Replace `xclip -out -selection clipboard` with `pbpaste` if you're on macOS
xclip -out -selection clipboard | jq
xclip -out -selection clipboard | jq --indent 4

# ppjson Script

I created a small ppjson script which is available in my dotfiles. It requires having jq installed and it will either use xclip or pbpaste depending on what OS you have.

Here’s a couple of usage examples:

# Pretty print the contents of your clipboard with 2 spaces of indentation
ppjson

# Pretty print the content of a file
ppjson data.json

In either case you can pass in any arguments that jq supports after an optional file name. That lets you parse the JSON such as ppjson .glossary.title -r to return back “example glossary” in the above example JSON.

The video below demos running these commands.

# Demo Video

Timestamps

  • 0:08 – Choosing Python or jq?
  • 0:24 – The ppjson script
  • 1:32 – Using Python directly
  • 3:11 – Using jq directly
  • 4:19 – Checking out the ppjson script’s source code
  • 5:45 – Why I created this script

What was the last JSON object you pretty printed? Let me know below.

Never Miss a Tip, Trick or Tutorial

Like you, I'm super protective of my inbox, so don't worry about getting spammed. You can expect a few emails per year (at most), and you can 1-click unsubscribe at any time. See what else you'll get too.



Comments