Working with json using jq

Installing jq

# Debian/Ubuntu
$ apt-get install jq
# Fedora
$ dnf install jq
# macOS
$ brew install jq

Parsing a json object into jq

$ json_data='{"name":"Tony","age":"35","gender":"male"}'
$ echo ${json_data}
{"name":"Tony","age":"35","gender":"male"}
$ cat sample.json
{"name":"Tony","age":"35","gender":"male"}
$ echo $json_data | jq '.'
{
"name": "Tony",
"age": "35",
"gender": "male"
}
$ cat sample.json | jq '.'
{
"name": "Tony",
"age": "35",
"gender": "male"
}
$ jq '.' sample.json
{
"name": "Tony",
"age": "35",
"gender": "male"
}
$ jq '.age' sample.json
"35"
$ jq -r '.age' sample.json
35
$ echo '{
"name": "Tony",
"age": 35,
"gender": "male",
"favourite": {
"color": "red",
"fruit": "apple",
"sport": "tennis"
}
}' | jq '.favourite.fruit'
"apple"

Builtin Operators

$ echo '{
"name": "Tony",
"age": 35,
"gender": "male",
"favourite": {
"color": "red",
"fruit": "apple",
"sport": "tennis"
}
}' > tony.json
$ jq '.favourite | .fruit' tony.json
"apple"
$ jq '.favourite.color = "blue"' tony.json
{
"name": "Tony",
"age": 35,
"gender": "male",
"favourite": {
"color": "blue",
"fruit": "apple",
"sport": "tennis"
}
}
$ jq '.favourite.color = "blue" | .age = 36' tony.json
{
"name": "Tony",
"age": 36,
"gender": "male",
"favourite": {
"color": "blue",
"fruit": "apple",
"sport": "tennis"
}
}
$ jq '.favourite |= . + {"movie": "Die Hard"}' tony.json
{
"name": "Tony",
"age": 35,
"gender": "male",
"favourite": {
"color": "red",
"fruit": "apple",
"sport": "tennis",
"movie": "Die Hard"
}
}
$ jq '.favourite |= . + {"color": "blue"}' tony.json
{
"name": "Tony",
"age": 35,
"gender": "male",
"favourite": {
"color": "blue",
"fruit": "apple",
"sport": "tennis"
}
}

Builtin Functions

$ jq '. | keys' sample.json
[
“age”,
“gender”,
“name”
]
$ jq '.age | type' tony.json
"number"
$ jq ‘.favourite.sport | length’ tony.json
6

Working with arrays (aka lists)

$ cat sample.json
[{"name":"Tony","age":"35","gender":"male"},{"name":"Jane","age":"30","gender":"female"}]
$ jq '.' sample.json
[
{
"name": "Tony",
"age": "35",
"gender": "male"
},
{
"name": "Jane",
"age": "30",
"gender": "female"
}
]
$ jq 'keys' sample.json
[
0,
1
]
$ jq '.[]' sample.json
{
"name": "Tony",
"age": "35",
"gender": "male"
}
{
"name": "Jane",
"age": "30",
"gender": "female"
}
$ jq '.[].age' sample.json
"35"
"30"
$ jq '.[0].age' sample.json
"35"
$ jq '.[] | select(.name=="Tony")' sample.json
{
"name": "Tony",
"age": "35",
"gender": "male"
}
$ jq '.[] | select(.name=="Tony").age' sample.json
"35"
$ jq '.[] | select(.name=="Tony") | .age' sample.json
"35"

Looping through an Array

$ cat sample.json
{ "employees": [{"name":"Tony","age":"35","gender":"male"},{"name":"Jane","age":"30","gender":"female"}]}
$ jq '.' sample.json
{
"employees": [
{
"name": "Tony",
"age": "35",
"gender": "male"
},
{
"name": "Jane",
"age": "30",
"gender": "female"
}
]
}
$ jq '.employees[1]' sample.json
{
"name": "Jane",
"age": "30",
"gender": "female"
}
$ jq -c '.employees[]' sample.json
{"name":"Tony","age":"35","gender":"male"}
{"name":"Jane","age":"30","gender":"female"}
#!/bin/bashfor employee in $(jq -c '.employees[]' sample.json)
do
employee_name=$(echo ${employee} | jq '.name')
employee_age=$(echo ${employee} | jq '.age')
echo "employee name is: ${employee_name}"
echo "employee age is: ${employee_age}"
echo ""
done
employee name is: "Tony"
employee age is: "35"
employee name is: "Jane"
employee age is: "30"

Creating and editing json objects

$ jq -n --arg my_username "john" --arg my_id "25" '{
"username": $my_username,
"id": $my_id
}'
{
"username": "john",
"id": "25"
}
$ cat sample.json
[{"name":"Tony","age":"35","gender":"male"},{"name":"Jane","age":"30","gender":"female"}]
$ cat sample.json | jq '.[] | select(.name=="Tony")' | jq '{
"username": .name,
"current_age": .age
}'
{
"username": "Tony",
"current_age": "35"
}

Miscellaneous

$ jq '.' sample.json | jq -c '.'
{"name":"Tony","age":"35","gender":"male"}
$ jq keys sample1.json
[
"age",
"gender",
"name"
]

Advanced examples

kubectl get deployments {deployment-name} -o json | \ 
jq '(.spec.template.spec.containers[] | select(.name | contains("{container-name}")) | .env[] | select(.name | contains("{environment-variable-name}")) | .value) |= "{new-value}"' | \
kubectl -f -
kubectl get secrets -o json | jq 'del(.items[].metadata ["creationTimestamp", "namespace", "resourceVersion", "generation", "selfLink", "uid"] )'

Further Reading

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store