【发布时间】:2020-09-09 20:20:12
【问题描述】:
我有两个 json 文件,我想根据键的值合并它们。两个 json 文件中的键名不同,但值相同。我正在使用 jq 来尝试完成这项工作。我发现的大多数示例都是基于键名而不是值合并的。
sample1.json
[
{
"unique_id": "pp1234",
"unique_id_type": "netid",
"rfid": "12245556890478",
},
{
"unique_id": "aqe123",
"unique_id_type": "netid",
"rfid": "12234556890478",
}
]
sample2.json
[
{
"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",
}
]
我希望输出看起来像:
results.json
[
{
"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 以外的东西。提前致谢。
【问题讨论】:
-
请修复 JSON(例如运行
jq empty data.json以查看故障点,或使用 jsonlint.com)。此外,据说预期的输出与输入文件不匹配。最后,由于 SO 不是免费的编程服务,请至少展示您解决问题的一种尝试。您可能会发现查看minimal reproducible example 的指南会有所帮助。
标签: json file merge jq key-value