jq
jq
add or modify json key/value
let’s say you want to add "apitoken": "top-secret" within clodflare.
example cf.json test file:
{
"cloudflare": {
"TYPE": "CLOUDFLAREAPI",
"accountid": "foobar"
}
}do this:
jq '.cloudflare.apitoken = "top-secret"' cf.json | sponge cf.jsonwith sponge the file will be written to disk and updated.
delete key/value from json file
assume you want to delete the apitoken field from cf.json, do this:
jq 'del(.cloudflare.apitoken)' cf.json | sponge cf.jsonget multiple items
just comma separate what you want
bw list items --search zoho|jq '.[].login.username, .[].login.password'get raw output
… instead of JSON strings for example
-r output raw strings, not JSON texts;jq -r '.[].login.username, .[].login.password'get the first element
jq .[0] file.jsonmerge multiple json files
for example: fitbit export, separated by day but same format otherwise. I wanted one file out of 4 years of data:
jq -s . heart_rate*.json > ../heart_rate.jsonor just use flatten like this:
jq -c --slurp 'flatten' steps-*.json > ../steps.jsonpretty print json
your-json | jq
jq < file.jsonreformat json file (formatting, prettier)
jq < ~/downloads/data.json | sponge ~/downloads/data.jsonyou might not need jq
curl -sS https://abc.json | python -m json.tool