Skip to content

Tratamiendo JSON

1. JQ, what is it?

There is a utility called jq [https://stedolan.github.io/jq/] that makes it easier to handle JSON structures from shell scripts.

2. Installation

Go to the official page and look for the installer for your operating system:
https://stedolan.github.io/jq/download/

3. Reading a JSON file

We have a file called mi_fichero.json

{
    "artifactId": "commons-lang3", 
    "groupId": "org.apache.commons",
    "version":"3.12.0"
}

This script example parses it and retrieves the values:

#!/bin/bash

# Get the file content into a variable
COMMONS_LANG_VERSION=$(cat ./docs/mi_fichero.json)

# Parse the content with jq and extract the value we are interested in
# (the -r operator returns the string content 3.12.0 instead of "3.12.0" with quotes)
COMMONS_LANG_VERSION=$(echo $COMMONS_LANG_VERSION | jq -r '.version')
echo $COMMONS_LANG_VERSION   # This prints 3.12.0

COMMONS_LANG_GROUPID=$(echo $COMMONS_LANG_VERSION | jq -r '.groupId')
echo $COMMONS_LANG_GROUPID   # This prints org.apache.commons

4. Reading a JSON file with arrays and nested objects + filtering

Let’s imagine the file personas.json, which is an array of objects and inside those objects there are nested objects:

[
    {
        "friendlyName": "El burbujas",
        "name": "Jose Luis",
        "description": "A vulgar guy but with charisma",
        "direccion": {
            "calle": "Avenida parqueMonte", 
            "numero": "37"
        }
    },{
        "friendlyName": "El santo",
        "name": "Paco Francis",
        "description": "He always blames you for everything",
        "direccion": {
            "calle": "el cielo", 
            "numero": "12"
        }
    },{
        "friendlyName": "Morat",
        "name": "Ramon",
        "description": "He is quite ugly",
        "direccion": {
            "calle": "Del rio", 
            "numero": "12"
        }
    }
]

Let’s see a shell script that extracts Ramon’s data:

#!/bin/bash

# Search for the data using select()
RAMON_INFO=$(cat ./docs/personas.json | jq -c '.[] | select(.name | contains("Ramon"))')
RAMON_INFO_CALLE=$(echo $RAMON_INFO | jq -r '.direccion.calle') 
echo $RAMON_INFO_CALLE  # Del rio

5. Conclusion

We have seen how, using JQ, we can parse and read complex JSON structures with Bash scripts.