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

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 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’

tech