Parse JSON output from an API with Python

As great as PowerShell is with JSON objects you can’t always use it because it is not available on each and every machine with Linux out there. And I don’t know a way of parsing JSON code with plain bash properly. Fortunately a lot of servers come with “python” installed and it can be used to parse JSON and retrieve values from the object with a comprehensible notation.

For example, this is the response from a API call to create a new DNS record in a self managed zone:

    "record": {
        "id": "4711",
        "type": "A",
        "name": "testentry",
        "value": "",
        "ttl": 3600,
        "zone_id": "2",
        "created": "1970-01-01 00:00:00.00 +0000 UTC",
        "modified": "1970-01-01 00:00:00.00 +0000 UTC"

What I would need from this call is the record id. To show how this works, I have to explain and extend the example: Such a response comes most of the times when calling an API via “curl” for example. It is not just lying around on the disk. This in mind let’s assume that the shown JSON code is the result of the following call:

curl -s

Now curl just puts out the response on stdout and we can pipe it in the following command and read it there via stdin:

curl -s | python3 -c "import sys,json; print(json.load(sys.stdin)['record']['id'])"

With this code the value of “id” (['record']['id'] – compare with the source JSON, in the example “4711”) is printed (println) on the command line. Now, to store it in a variable one could do this:

$RECORDID=$(curl -s | python3 -c "import sys,json; print(json.load(sys.stdin)['record']['id'])")

And then save it on the disk and use it for your purposes. In my case for example I’d use it for deleting the record later in a process I’ll describe in another tutorial.