jq比较2json个文件,按键值删除重复对象

jq compare 2 json files and delete duplicate objects by key value

我有 2 个 json 文件,我希望将它们相互比较,最后得到的文件仅包含第二个文件中的唯一对象。唯一对象不是由完整对象决定的,而是由其中一个名为 Car ID 的键的值决定的。下面是 json 文件的示例,以更好地解释这一点:

file1.json:

{
"User Data": [
{"First Name": "Tony", "Last Name": "Evans", "DOB": "1987-02-01", "Car ID": "UJ928JD9"},
{"First Name": "John", "Last Name": "Smith", "DOB": "1972-11-27", "Car ID": "UJ235UW8"},
{"First Name": "Kirsty", "Last Name": "Morgan", "DOB": "1991-06-08", "Car ID": "UJ424KL2"},
...
]
}

file2.json

{
"User Data": [
{"First Name": "Harry", "Last Name": "Jones", "DOB": "1983-03-09", "Car ID": "UJ928JD9"},
{"First Name": "Jeremy", "Last Name": "Blake", "DOB": "1965-09-21", "Car ID": "UJ345IE2"},
{"First Name": "Jason", "Last Name": "Roberts", "DOB": "1972-10-18", "Car ID": "UJ424KL2"},
...
]
}

在上面的示例中,两行的汽车 ID 相同,因此只应保留第二个文件中的第二个对象,如下所示:

{
    "User Data": [
    {"First Name": "Jeremy", "Last Name": "Blake", "DOB": "1965-09-21", "Car ID": "UJ345IE2"}
    ]
    }

使用 --argfile 将两个文件作为变量读入,然后 map 都只读入它们的 Car ID,然后从第二个数组中减去第一个数组得到 [=13] 的数组=]s 你想保留,最后使用键 select 来自第二个输入文件的正确元素。

jq -n \
  --argfile file1 file1.json \
  --argfile file2 file2.json \
  '
    # Take both input files and create an array $ids with all
    # Car IDs from the second file that are not part of the first one
    [ $file1, $file2 ]
    | map(.["User Data"] | map(.["Car ID"]))
    | (.[1] - .[0]) as $ids
    
    # Take the second input file and select only those entries
    # whose Car ID is part of the previously stored array of Car IDs
    | $file2
    | .["User Data"] |= map(select(.["Car ID"] == $ids[]))
  '