Extracting JSON value from command line with jq and Python

Developing modern web applications you often come to around checking REST API responses and parsing JSON values. You can do it with a combination of Unix tools like sed, cut and awk but if you’re allowed to install extra tools or use Python then things get easier. This post shows you couple of options for extracting JSON values with Unix tools.

There are a number of tools specifically designed for the purpose of manipulating JSON from the command line, and will be a lot easier and more reliable than doing it with awk. One of those tools is jq as shown Stack Overflow. You can install it in macOS from Homebrew: brew install jq.

$ curl -s 'https://api.github.com/users/walokra' | jq -r '.name'

If you’re limited to tools that are likely installed on your system such as Python, using the json module gives you the benefit of a proper JSON parser and avoiding any extra dependencies.

Python:

$ curl -s 'https://api.github.com/users/walokra' | \
    python -c "import sys, json; print(json.load(sys.stdin)['name'])"

Stack Overflow answers to the question of “Parsing JSON with Unix tools” shows you other options with standard tools like sed, cut and Awk and more exotic options with Perl, Node.js and PHP.


Posted

in

by

Comments

2 responses to “Extracting JSON value from command line with jq and Python”

  1. Kelly Brazil Avatar

    I created a tool called jello that expands on your python example. It gives you the convenience of jq but uses python syntax. https://github.com/kellyjonbrazil/jello

  2. […] If you’re allowed to install extra tools or use Python then things get easier as you can use command line and combine jq and Python to extract JSON values. And a further note you can also use jp, command line interfacee to […]

Leave a Reply to 2018 Retrospective – Rule of Tech Cancel reply

Your email address will not be published. Required fields are marked *