使用JQ在JSON上加入Match

问题描述

我有两个输出文件,并且它们具有以下结构的多个重复条目:

file1.json

{
 "value": [
    {
          "description": "foo bar","id": "111111-0000-0000-1111-foobar",},]
}

file2.json

[
    {
        "businessPhones": [555-505-4321],"displayName": "Abc","givenname": "abc","id": "000000-1111-1111-0000-abcabc","descriptionId": "111111-0000-0000-1111-foobar"
    },]

我需要匹配在 file1 中找到的 file2 descriptionId的值;但是要产生一个最终结果-即使它是 file3 ,它也会以某种方式替换该键值,最终变成如下所示:

file3.json

[
    {
        "businessPhone": [555-505-4321],"description": "foo bar"
    },]

到目前为止,我已经能够使用Bash和JQ整理一些数据,但是在如何将它们解析在一起方面却存在分歧。

解决方法

在文件中进行了明显的更正之后,执行以下调用:

jq --argfile file1 file1.json -f program.jq file2.json

产生所需的结果,其中program.jq包含:

($file1 | INDEX(.value[]; .id) | map_values(.description)) as $dict
| map(.description = $dict[.descriptionId] | del(.descriptionId))

如果您确信那是您真正想要的,则可以使用moreutil的来覆盖file2.json。