jq
How check if there is an array or an object in jq?
Use the type function: type The type function returns the type of its argument as a string, which is one of null, boolean, number, string, array or object. Example 1: echo ‘[0, false, [], {}, null, “hello”]’ | jq ‘map(type)’ [ “number”, “boolean”, “array”, “object”, “null”, “string” ] Example 2: echo ‘[0,1]’ | jq ‘if … Read more
How to add a field to a JSON object with the jq command?
This type of update is where the magic of += comes into play. With your input, the following invocation: jq ‘.module.data.item_i77f664a2.fields += {“operation”:”delete”}’ produces the output you indicate you want: { “success”: true, “module”: { “data”: { “item_i77f664a2”: { “id”: “i77f664a2”, “tag”: “item”, “fields”: { “cartItemId”: 2012636322, “operation”: “delete” }, “type”: “biz” } } } … Read more
jq & bash: make JSON array from variable
In jq 1.3 and up you can use the –arg VARIABLE VALUE command-line option: jq -n –arg v “$VAR” ‘{“foo”: $v}’ I.e., –arg sets a variable to the given value so you can then use $varname in your jq program, and now you don’t have to use shell variable interpolation into your jq program. EDIT: … Read more
How to add a header to CSV export in jq?
Just add the header text in an array in front of the values. [“Commit Message”,”Committer Name”], (.[].commit | [.message,.committer.name]) | @csv
Filter by Regex in JQ
If you tacked the following filter onto the one you already have, then you’d get the output shown below: map(select(.Address | test(“^[0-9]”))) Output: [ { “Address”: “1 Bridge Rd” } ] For robustness, you might like to consider adding ? after the test: map(select(.Address | test(“^[0-9]”)?)) Or, you could combine the two calls to map … Read more
jq: how to query for array values that don’t contain text “foo”?
You can use not to reverse the logic (.imageTags[] | contains(\”latest\”) | not) Also, I’d imagine you can simplify your pipeline into a single jq call.
jq: how to filter an array of objects based on values in an inner array?
Very close! In your select expression, you have to use a pipe (|) before contains. This filter produces the expected output. . – map(select(.Names[] | contains (“data”))) | .[] .Id The jq Cookbook has an example of the syntax. Filter objects based on the contents of a key E.g., I only want objects whose genre … Read more
jq: Getting two levels of keys
Use map_values instead of map to map the values of a JSON object while preserving the keys: jq ‘.p | map_values(keys)’ On jq versions lower than 1.5, map_values is not defined: instead, you can use []|=: jq ‘.p | . []|= keys’