1

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 ?

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

2 Comments

Glen : Thanks , This will give me all values exactly. If i need get the "values" from particular "name" in all the JSON files , How can i do that ?
Glen : Thanks Glean. Select is the option i was looking. Now got it
0

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 ?

Here is a solution which uses indices

  .info.file.hashes
| (map(.name) | [ indices($names[]) | .[] ]) as $found
| .[ $found[] ]
| .value

If this filter is in filter.jq and the sample data in a.json and b.json then

jq -M -r --argjson names '["B","C","E"]' -f filter.jq a.json b.json

produces

3c6781d16dc26baf6422bb24d1cd0f650e451b99
3c6781d16dc26baf6422bb24d1cd0f650e451b99
66031dad95dfe6ad10b35f06c4342faa
9df25fa4e379837e42aaf6d05d92012018d4b659
e445f412f92b25f3343d5f7adc3c94bdc950601521d5b91e7ce77c21a18259c9

note the first hash is duplicated in the sample data. If that is a problem it can easily be handled with unique or other post-processing.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.