我希望根據公共 id 匹配 2 個 json 檔案,我嘗試過使用 awk、jq 和 npm json 包以很多不同的方式,但沒有什么能接近作業。2 個 json 檔案未排序,并且不包含所有相同的條目。它們包含公共網路 ID,我只希望輸出包含來自 file2 的條目。希望有人能幫忙!這是一個例子。
檔案 1.json:
[
{
"customerId": "id1",
"networkId": "L_653021545945744804"
},
{
"customerId": "id2",
"networkId": "L_653021545955724805"
},
{
"customerId": "id3",
"networkId": "L_655051945958724557"
},
{
"customerId": "id4",
"networkId": "L_655567989968735408"
}
]
檔案2.json:
[
{
"name": "a",
"networkId": "L_653021545945744804"
},
{
"name": "b",
"networkId": "L_655051945958724557"
}
]
想要的輸出:
[
{
"customerId": "id1",
"name": "a",
"networkId": "L_653021545945744804"
},
{
"customerId": "id3",
"name": "b",
"networkId": "L_655051945958724557"
}
]
uj5u.com熱心網友回復:
這是INDEX,JOIN和的任務add:
jq '[JOIN(INDEX(.networkId); input[]; .networkId; add)]' file1.json file2.json
[
{
"name": "a",
"networkId": "L_653021545945744804",
"customerId": "id1"
},
{
"name": "b",
"networkId": "L_655051945958724557",
"customerId": "id3"
}
]
演示
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/466761.html
上一篇:修復使用錯誤字符集編碼的檔案
