Json / jq

Install

sudo apt install -y jq

Parse

jq 'select(.geo != null)' all.json
 
# select value
jq -r '.[] | select(.protocol_port == 443) | .id'
 
# read two values and search from file
jq -r '.GeneralInfo.UUID + " " + (.NetworkEntry[] | select(.Notes | contains("-mgmt")).Entry)' *.json
 
# not contain
jq -r '.[] | select(.status | contains("CREATE_COMPLETE") | not).uuid'
 
# search
jq '[ .devices | select(.key | startswith("dimmer")) | .value = .value.state ] | from_entries' filename.json
jq -r '.[] | select(."Fixed IP Addresses"[].ip_address | startswith("10.11")).ID'
 
# pretty print
cat /tmp/in.json | python -m json.tool
 
# JSON Validator
https://jsonformatter.curiousconcept.com/
 
# parse key with spaces
cat x.json | jq '."General Info"."Model No."'
 
# marge JSON files
jq -s '.' /tmp/*.json > out.json
 
# output without quotes
cat file.json | jq -r '.name'
 
# key containing blank
storcli /c0/v0 show J | jq -r '.Controllers[0]."Response Data"."Virtual Drives"[0].State'
 
# show multiple values
jq -r '.[].Name, .[].HostConfig.RestartPolicy.Name'
jq -r '.[] | (.Name + " " + .HostConfig.RestartPolicy.Name)'
 
# show multiple values in multiple lines with additional text
jq -r '"my_id: " + .id, "my_secret: " + .secret'
 
# join lines
jq -r '.security_group_ids | join(",")')
 
# delete empty values
jq 'del(.[][] | nulls)'
jq 'del(.[][] | select(. == ""))'
del(.[][] | select(. == "" or .BAR==0))
 
# split array to line
jq -c '.[]'
 
# base64 (jq to bash)
echo "${sample}" | jq -r '.[] | @base64'
echo ${row} | base64 --decode | jq -r ${1}

processing Icinga warning Json list

curl -s -u "foo:bar" -k "https://monitoring.example.com/icinga/cgi-bin/status.cgi?servicestatustypes=20&noheader=1&servicestatustypes=29&sorttype=2&sortoption=3&scroll=963&jsonoutput"  | \
  jq --raw-output ' .status.service_status[] | select(.host_name | contains("dev")) | select(.status_information | contains("Apache", "Java")) | .host_name'

Marge files
https://stackoverflow.com/questions/49037956/how-to-merge-arrays-from-two-files-into-one-array-with-jq/

jq -s . /tmp/x.json /tmp/y.json
jq -s '[ .[0] + .[1] | group_by(.name)[] | select(length > 1) | add ]' registration.json useredits.json

Get first / last / Nth element from list

jq -r 'first(.security_group_ids[])'
jq -r 'last(.security_group_ids[])'
 
( nth(1; .[] | select(.a == "x")) )

Links
https://shapeshed.com/jq-json/
https://stedolan.github.io/jq/manual/
https://jqplay.org/
http://www.compciv.org/recipes/cli/jq-for-parsing-json/
https://github.com/stedolan/jq/issues/82
https://stedolan.github.io/jq/manual/