Using Jq to Fetch Key Value from JSON Output

Using jq to fetch key value from json output

You need to combine filters by means of | operator:

$ jq -r '.[] | .[] | .name' test.json 
rhel6.6
rhel7

The first .[] fetches repositories array. The next .[] fetches all the items of the repositories array. Finally, .name extracts properties from the array items(objects).

Note, the first .[] works on object because it is a documented feature:

.[]
If you use the .[index] syntax, but omit the index entirely, it
will return all of the elements of an array...

You can also use this on an object, and it will return all the
values of the object.

how to output all the keys and values from json using jq?

The detailed requirements are unclear, but hopefully you'll be able to use the following jq program as a guide:

..
| objects
| select( .data10 == "true" )
| to_entries[]
| select(.key != "data10")
| [.key, .value]

This will recursively (thanks to the initial ..) examine all the JSON objects in the input.

p.s.

If you want to make the selection based on whether .data10 is "true" or true, you could change the criterion to .data10 | . == true or . == "true".

how to print all key and one of the values from json using jq

Two approaches:

jq -r 'to_entries[] | "\( .key ): \( .value.abc )"'

Demo on jqplay

jq -r 'keys[] as $key | .[$key] | "\( $key ): \( .abc )"'

Demo on jqplay

How to get key value pairs of the objects from complex JSON using jq and map? (Active Campaign)

Here's another, shorter approach that doesn't use group_by. Instead, this directly iterates over the initial object using reduce and imediately sets all the fields accordingly if the key followed the space-separated role-key pattern.

reduce (to_entries[] | .key /= " ") as {key: [$role, $key], $value} ({};
if $key then
.[$role] += {({Email: "email_address", Name: "name"}[$key]): $value, $role}
else . end
)
{
"Presenter": {
"name": "Roney",
"role": "Presenter",
"email_address": "roney@domain.com"
},
"Approver": {
"name": "Tim",
"role": "Approver",
"email_address": "tim@domain.com"
},
"Customer": {
"name": "Alex",
"role": "Customer",
"email_address": "alex@domain.com"
}
}

Demo

How to get key names from JSON using jq

You can use:

jq 'keys' file.json

Complete example

$ cat file.json
{ "Archiver-Version" : "Plexus Archiver", "Build-Id" : "", "Build-Jdk" : "1.7.0_07", "Build-Number" : "", "Build-Tag" : "", "Built-By" : "cporter", "Created-By" : "Apache Maven", "Implementation-Title" : "northstar", "Implementation-Vendor-Id" : "com.test.testPack", "Implementation-Version" : "testBox", "Manifest-Version" : "1.0", "appname" : "testApp", "build-date" : "02-03-2014-13:41", "version" : "testBox" }

$ jq 'keys' file.json
[
"Archiver-Version",
"Build-Id",
"Build-Jdk",
"Build-Number",
"Build-Tag",
"Built-By",
"Created-By",
"Implementation-Title",
"Implementation-Vendor-Id",
"Implementation-Version",
"Manifest-Version",
"appname",
"build-date",
"version"
]

UPDATE: To create a BASH array using these keys:

Using BASH 4+:

mapfile -t arr < <(jq -r 'keys[]' ms.json)

On older BASH you can do:

arr=()
while IFS='' read -r line; do
arr+=("$line")
done < <(jq 'keys[]' ms.json)

Then print it:

printf "%s\n" "${arr[@]}"

"Archiver-Version"
"Build-Id"
"Build-Jdk"
"Build-Number"
"Build-Tag"
"Built-By"
"Created-By"
"Implementation-Title"
"Implementation-Vendor-Id"
"Implementation-Version"
"Manifest-Version"
"appname"
"build-date"
"version"

or use:

declare -p arr

You may also use sort for better visibility like this:

jq 'keys | sort' file.json

how to extract specific keys from json

.[].builds | to_entries[] | "\(.key), \(.value | keys[])"

Will generate

"test, 2021.1.2"
"dev, 2021.2.2"
"test, 2021.1.2"
"dev, 2021.2.1"
"test, 2021.3.1"

Use jq --raw-output to remove the "'s around each line


Online demo



Related Topics



Leave a reply



Submit