Skip to content

Latest commit

 

History

History
52 lines (42 loc) · 1.73 KB

prettify_json_on_the_command_line.md

File metadata and controls

52 lines (42 loc) · 1.73 KB

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