使用jq根据键值合并两个json值

使用jq根据键值合并两个json值,json,file,merge,jq,key-value,Json,File,Merge,Jq,Key Value,我有两个json文件,我想根据一个键的值合并它们。两个json文件中的键名不同,但值相同。我正在使用jq来尝试完成这项工作。我发现的大多数示例都是基于键名而不是值进行合并的 sample1.json [ { "unique_id": "pp1234", "unique_id_type": "netid", "rfid": "12245556890478&qu

我有两个json文件,我想根据一个键的值合并它们。两个json文件中的键名不同,但值相同。我正在使用jq来尝试完成这项工作。我发现的大多数示例都是基于键名而不是值进行合并的

sample1.json

 [
  {
    "unique_id": "pp1234",
    "unique_id_type": "netid",
    "rfid": "12245556890478",
 },
{
    "unique_id": "aqe123",
    "unique_id_type": "netid",
    "rfid": "12234556890478",
 }
] 
[
 {
 "mailing_state": "New York",
  "mobile_phone_number": "(982) 2541212",
  "netid": "pp1234",
  "netid_reachable": "Y",
 },
 {
 "mailing_state": "New York",
  "mobile_phone_number": "(982) 5551212",
  "netid": "aqe123",
  "netid_reachable": "Y",
 }
] 
 [
      {
        "unique_id": "pp1234",
        "unique_id_type": "netid",
        "rfid": "12245556890478",
        "mailing_state": "New York",
        "mobile_phone_number": "(982) 2541212",
        "netid_reachable": "Y",
     },
     {
        "unique_id": "aqe123",
        "unique_id_type": "netid",
        "rfid": "12234556890478",
        "mailing_state": "New York",
        "mobile_phone_number": "(982) 5551212",
        "netid_reachable": "Y",
    }
]
sample2.json

 [
  {
    "unique_id": "pp1234",
    "unique_id_type": "netid",
    "rfid": "12245556890478",
 },
{
    "unique_id": "aqe123",
    "unique_id_type": "netid",
    "rfid": "12234556890478",
 }
] 
[
 {
 "mailing_state": "New York",
  "mobile_phone_number": "(982) 2541212",
  "netid": "pp1234",
  "netid_reachable": "Y",
 },
 {
 "mailing_state": "New York",
  "mobile_phone_number": "(982) 5551212",
  "netid": "aqe123",
  "netid_reachable": "Y",
 }
] 
 [
      {
        "unique_id": "pp1234",
        "unique_id_type": "netid",
        "rfid": "12245556890478",
        "mailing_state": "New York",
        "mobile_phone_number": "(982) 2541212",
        "netid_reachable": "Y",
     },
     {
        "unique_id": "aqe123",
        "unique_id_type": "netid",
        "rfid": "12234556890478",
        "mailing_state": "New York",
        "mobile_phone_number": "(982) 5551212",
        "netid_reachable": "Y",
    }
]
我希望输出看起来像:

results.json

 [
  {
    "unique_id": "pp1234",
    "unique_id_type": "netid",
    "rfid": "12245556890478",
 },
{
    "unique_id": "aqe123",
    "unique_id_type": "netid",
    "rfid": "12234556890478",
 }
] 
[
 {
 "mailing_state": "New York",
  "mobile_phone_number": "(982) 2541212",
  "netid": "pp1234",
  "netid_reachable": "Y",
 },
 {
 "mailing_state": "New York",
  "mobile_phone_number": "(982) 5551212",
  "netid": "aqe123",
  "netid_reachable": "Y",
 }
] 
 [
      {
        "unique_id": "pp1234",
        "unique_id_type": "netid",
        "rfid": "12245556890478",
        "mailing_state": "New York",
        "mobile_phone_number": "(982) 2541212",
        "netid_reachable": "Y",
     },
     {
        "unique_id": "aqe123",
        "unique_id_type": "netid",
        "rfid": "12234556890478",
        "mailing_state": "New York",
        "mobile_phone_number": "(982) 5551212",
        "netid_reachable": "Y",
    }
]

只要记录是基于netid/unique_id键合并的,结果的顺序就无关紧要。如果有必要,我愿意使用jq以外的东西。请提前感谢。

一旦纠正了示例输入文件,下面的调用应该会起到作用:

jq --argfile uid sample1.json '
  ($uid | INDEX(.unique_id)) as $dict
  | map( $dict[.netid] + del(.netid) )
' sample2.json

如果您不喜欢使用--argfile,因为它已被弃用,您可以(例如)使用--slurpfile并将jq程序中的
$uid
更改为
$uid[0]

请修复JSON(例如,运行
jq empty data.JSON
查看故障点,或使用jsonlint.com)。此外,据说预期的输出与输入文件不匹配。最后,由于SO不是免费的编程服务,请至少展示一次解决问题的尝试。您可能会发现在上查看指南很有帮助。谢谢您的提示。我尝试使用提供的语法,它合并了第二条记录,但由于某种原因没有合并第一条记录。(我还修复了原始示例中不正确的unique_id。)您修复了输入文件吗?我已经验证了在修复它们之后,如图所示的调用将生成results.json。