用 Bash 解析 JSON jq 问题

Parsing JSON with Bash jq Issue

使用 bash JQ 解析器,我正在尝试解析 cURL JSON 响应中的字段。

在文件中 'a.json' 有 4 个 'hash' 值,'b.json' 有 5 个 'hash' 值。基于我的结果类似于 "a.json" 的假设,我为它编写了一个解析器。

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

示例 JSON 个文件

#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
          }
}
}

但有时结果也会像 "b.json" 并且有 5 个字段。当我尝试使用我编写的 JQ 命令进行解析时,只会给我 4 个字段并遗漏 "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

现在,我们如何才能 select 仅来自所需 'name' 的哈希值。

示例:如果我们想 select 使用 JQ 仅对任何 JSON 文件中字符串 'names' B、C、E 的散列值?

有什么建议吗?

你可以用这个得到所有的值:

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

假设您只需要名称 == "B"

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

假设您只需要名称 == "B" "C"

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

"in" 函数检查传入的字符串是否是给定对象中的键。 {"B":1,"C":1} 的值是任意的。参考:https://stedolan.github.io/jq/manual/#in

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 ?

这是一个使用 indices

的解决方案
  .info.file.hashes
| (map(.name) | [ indices($names[]) | .[] ]) as $found
| .[ $found[] ]
| .value

如果此过滤器在 filter.jq 中,样本数据在 a.jsonb.json 中,则

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

产生

3c6781d16dc26baf6422bb24d1cd0f650e451b99
3c6781d16dc26baf6422bb24d1cd0f650e451b99
66031dad95dfe6ad10b35f06c4342faa
9df25fa4e379837e42aaf6d05d92012018d4b659
e445f412f92b25f3343d5f7adc3c94bdc950601521d5b91e7ce77c21a18259c9

请注意第一个散列在示例数据中重复。如果这是一个问题,可以使用 unique 或其他 post 处理轻松处理。