til

Today I Learned: collection of notes, tips and tricks and stuff I learn from day to day working with computers and technology as an open source contributor and product manager

View project on GitHub

Prettify JSON on the command line

I have experienced getting a huge JSON file and when I opened op in Visual Studio Code, it would complain and disable all the nifty JSON tools, like pretty printing etc.

Luckily making a pretty version on the command line is possible, without any complaints. This can be done using jq.

cat file.json | jq . > prettyfile.json

Example JSON (non-pretty):

{"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"}}}}}

Example lifted from json.org

Example JSON (pretty), after jq clean up:

{
  "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"
        }
      }
    }
  }
}

Resources and References

  1. StackExchange: “Is there a CLI tool that would prettify a JSON string”
  2. json.org