Removing a key from parent object with jq

Using the builtin function del: $ jq ‘del(.pages)’ myfile.json { “actions”: { “pages”: { “stuff”: “…” } } } Try it online at jqplay.org To remove all pages everywhere, instead of just the outer level, you would use something like $ jq ‘del(.. | .pages?)’ myfile.json { “actions”: {} } Try it online at jqplay.org

jq returning null as string if the json is empty

Something useful I found for shell scripts was: jq ‘.foo // empty’ Which returns the match if successful, and the empty string if unsuccessful. So in bash I use: addr=$(./xuez-cli getnetworkinfo | jq -r ‘.localaddresses[0].address // empty’) if [[ ! -z “$addr” ]]; then # do something fi Ref: https://github.com/stedolan/jq/issues/354#issuecomment-43147898 https://unix.stackexchange.com/questions/451479/jq-print-for-null-values

How do I sum all numbers from output of jq

the simplest solution is the add filter: jq ‘[.duration] | add’ the [ brackets ] are needed around the value to sum because add sums the values of an array, not a stream. (for stream summation, you would need a more sophisticated solution, e.g. using reduce, as detailed in other answers.) depending on the exact … Read more

jq combine output on a single line separated by space

The usual way would be to use the @csv or @tsv operators to convert the result in the CSV or tab-delimited format. These operators need the result to be contained in an array. For your case also to have a single space delimit, we can do a simple join(” “) operation jq -r ‘[.Accounts[].Id]|join(” “)’

jq parsing get value [closed]

EDIT: After clarification in the comments, to retrieve the states of devices whose key begins with “dimmer”, use jq ‘[ .devices | to_entries[] | select(.key | startswith(“dimmer”)) | .value = .value.state ] | from_entries’ filename.json Output: { “dimmer1”: “off”, “dimmer2”: “off” } This works as follows: .devices selects the .devices attribute of the JSON object … Read more

Flatten a JSON document using jq

This one was a tricky one to craft. map ( with_entries(select(.key != “fields”)) + (.fields | with_entries(.value = .value[0])) ) Let’s break it down and explain the bits of it For every item in the array… map(…) Create a new object containing the values for all except the fields property. with_entries(select(.key != “fields”)) Combine that … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)