Json / jq

Install

sudo apt install -y jq

Parse

jq 'select(.geo != null)' all.json
 
# 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
 
# remove double quote from output
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)'
 
# join lines
jq -r '.security_group_ids | join(",")')

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

Links
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/