A command-line JSON parser can be handy when you test or debug JSON web services. Unfortunately inspecting JSON responses via command line are hard to read and not easy to manipulate with traditional Unix utilities.

Today I stumbled on jq, via Angel Ramboi. jq is a lightweight and flexible command-line JSON processor.

Download the desired binary and then chmod +x ./jq.

I've prepared a sample json so we can see how easily it is to inspect and manipulate JSON strings.

>> cat sample.json

{
    "first": "John",
    "last": "Doe",
    "age": 27,
    "sex": "M",
    "registered": true,
    "interests": [ "Reading", "Mountain Biking", "Hacking" ],
    "favorites": {
        "color": "Blue",
        "sport": "Soccer",
        "food": "Spaghetti"
    },
    "skills": [
        {
            "category": "Python",
            "tests": [
                { "name": "One", "score": 90 },
                { "name": "Two", "score": 96 }
            ]
        },
        {
            "category": "GO",
            "tests": [
                { "name": "One", "score": 32 },
                { "name": "Two", "score": 84 }
            ]
        }
    ]
}

Simple filtering:

>> cat sample.json | jq '.skills[].category'

Python
Go

Filtering with index:

>> cat sample.json | ./jq '.["skills"][0]["tests"][1] | .name, .score'

"Two"
96

...or a different flavour:

>> cat sample.json | ./jq '.skills[0].tests[1] | .name, .score'

"Two"
96

Built-in operators:

>> cat sample.json | ./jq '.interests | length'

3

Manipulate JSON string:

>> cat sample.json | ./jq 'if .registered == true then .skills[].tests[].score = 1000 else null end' > new_sample.json

Head on and read the jq manual if you want to include it in your tester toolbox.