Arun Arun - 1 month ago 22
JSON Question

Parsing JSON with Bash JQ Issue

Using bash JQ parser, Im trying parse the fields from a cURL

JSON
response.

In file
'a.json'
has 4 'hash' values and
'b.json'
has 5 'hash' values. Based on the assumption that my results will be similar to
"a.json"
I wrote a parser for it.

#jq -r '.info[].hashes[0].value','.info[].hashes[1].value','.info[].hashes[2].value','.info[].hashes[3].value' a.json


Sample JSON files

#a.json

{
"info": {
"file": {
"Score": 4.01207390507143,
"file_subtype": "None",
"file_type": "EXE",
"hashes": [
{
"name": "A",
"value": "7e5dcd8ffdfa8d726ecbdd3c69e18230"
},
{
"name": "B",
"value": "3c6781d16dc26baf6422bb24d1cd0f650e451b99"
},
{
"name": "C",
"value": "3c6781d16dc26baf6422bb24d1cd0f650e451b99"
},
{
"name": "D",
"value": "c25561f3246ef188467a47971821bab93934842a1e2a48910db9768a2f66e828"
}
],
"size": 1912
}
}
}



#b.json
{
"info": {
"file": {
"Score": 4,
"file_subtype": "None",
"file_type": "Image",
"hashes": [
{
"name": "A",
"value": "f34d5f2d4577ed6d9ceec516c1f5a744"
},
{
"name": "B",
"value": "66031dad95dfe6ad10b35f06c4342faa"
},
{
"name": "C",
"value": "9df25fa4e379837e42aaf6d05d92012018d4b659"
},
{
"name": "D",
"value": "4a51cc531082d216a3cf292f4c39869b462bf6aa"
},
{
"name": "E",
"value": "e445f412f92b25f3343d5f7adc3c94bdc950601521d5b91e7ce77c21a18259c9"
}
],
"size": 500
}
}
}


But some times the results will be like "b.json" too and have 5 fields . when i'm trying to parse with the JQ command that i have written , Will give me only 4 fields and missing out the last value of "E".

#jq -r '.info[].hashes[0].value','.info[].hashes[1].value','.info[].hashes[2].value','.info[].hashes[3].value' b.json

Result :

f34d5f2d4577ed6d9ceec516c1f5a744
66031dad95dfe6ad10b35f06c4342faa
9df25fa4e379837e42aaf6d05d92012018d4b659
4a51cc531082d216a3cf292f4c39869b462bf6aa


Now , How can we select only the hash values from desired 'name'.

Example : If we want to select only hash values of string 'names' B,C,E in any JSON files using JQ ?

Any suggestions please ?

Answer

You can get all the values with this:

jq -r '.info.file.hashes[] | .value' *.json

Suppose you need only the values where name == "B"

jq -r '.info.file.hashes[] | select(.name == "B") | .value'

Suppose you need only the values where name == "B" or "C"

jq -r '.info.file.hashes[] | select(.name | in({"B":1,"C":1})) | .value'

The "in" function checks if the passed-in string is a key in the given object. The values of {"B":1,"C":1} are arbitrary. Ref: https://stedolan.github.io/jq/manual/#in