Skip to content

Instantly share code, notes, and snippets.

@soxofaan
Last active February 15, 2021 11:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save soxofaan/957bf4ab53a6cdbd36ffd441d0567ee9 to your computer and use it in GitHub Desktop.
Save soxofaan/957bf4ab53a6cdbd36ffd441d0567ee9 to your computer and use it in GitHub Desktop.
Poor man's jq

jq is a command line tool to process JSON documents with loads of features.

If you just want quick (re-)indentation, use one of these Python powered poor man's alternatives:

python -c "import json,sys;json.dump(json.load(sys.stdin),sys.stdout,indent=2)" < data.json

python -c "import json,sys,pprint;pprint.pprint(json.load(sys.stdin))" < data.json

Examples:

$> echo '{"foobar the xuv wih":[1,2,3,{"hello":["world","earth","apple","orange","banana"]}]}' > data.json

$> python -c "import json,sys;json.dump(json.load(sys.stdin),sys.stdout,indent=2)" < data.json
{
  "foobar the xuv wih": [
    1,
    2,
    3,
    {
      "hello": [
        "world",
        "earth",
        "apple",
        "orange",
        "banana"
      ]
    }
  ]
}

$> python -c "import json,sys,pprint;pprint.pprint(json.load(sys.stdin))" < data.json
{'foobar the xuv wih': [1,
                        2,
                        3,
                        {'hello': ['world',
                                   'earth',
                                   'apple',
                                   'orange',
                                   'banana']}]}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment