在bash中使用jq合并两个JSON文件

lrpiutwd  于 2023-01-03  发布在  其他
关注(0)|答案(1)|浏览(205)

我希望有人能帮忙,我正在合并两个json文件。下面是我的bash脚本:

script_directory="/home/joey/scripts/scripts/delete"
region_file="US.en.json"
cnmts_file="cnmts.json"

wget https://github.com/blawar/titledb/raw/master/$region_file -O $script_directory/$region_file
wget https://github.com/blawar/titledb/raw/master/$cnmts_file -O $script_directory/$cnmts_file

#This is here just to simplify the json files
cat $script_directory/$region_file | jq '.[] | {id: .id}' > $script_directory/region_file_id.txt
cat $script_directory/$cnmts_file | jq '.[] | .[] | {titleId: .titleId, otherApplicationId: .otherApplicationId}' > $script_directory/cnmts_titleId_otherApplicationId.txt

基本上,我得到了两个文件:
区域文件ID. txt:

{
  "id": "01007EF00011E000"
}
{
  "id": "0100225000FEE000"
}
{
  "id": "0100BCE000598000"
}
{
  "id": "0100B42001DB4000"
}
{
  "id": "01008A9001DC2000"
}

和cnmts_标题Id_其他应用程序Id. txt:

{
  "titleId": "0100000000010000",
  "otherApplicationId": "0100000000010800"
}
{
  "titleId": "0100000000010800",
  "otherApplicationId": "0100000000010000"
}
{
  "titleId": "010000000e5ee000",
  "otherApplicationId": "010000000e5ee800"
}
{
  "titleId": "010000000eef0000",
  "otherApplicationId": "010000000eef0800"
}
{
  "titleId": "010000000eef0800",
  "otherApplicationId": "010000000eef0000"
}
{
  "titleId": "0100000011d90000",
  "otherApplicationId": "0100000011d90800"
}
{
  "titleId": "0100000011d90800",
  "otherApplicationId": "0100000011d90000"
}
{
  "titleId": "0100000011d90800",
  "otherApplicationId": "0100000011d90000"
}
    • 请注意,这只是文件的一个片段,请随时运行bash脚本以获取更准确的文件。**

'region_file_id'中的所有"id"都等于'cnmts_titleId_otherApplicationId'中某个地方的"titleId"(反之则不成立,因为它包括来自不同区域的id)。我正尝试通过交叉引用获取'region_file_id'中每个"id"的"otherApplicationId"值,并创建如下json:(对region_file_id中的每个"id"重复)

{
 "id": "111000"
 "titleId": "111000" (this one is optional as it is a duplicate from 'id')
 "otherApplicationId": 111800"
}

我试过搜索不同的片段:

jq -s '.[0] * .[1]' $script_directory/region_file_id.txt cnmts_titleId_otherApplicationId.txt (only returned 1 object for some reason)
jq -s '{ .[0] as $u | .[1] | select(.id == $u.titleId) |= $u }' $script_directory/region_file_id.txt cnmts_titleId_otherApplicationId.txt

更新:
正如匹克所指出的:

jq -n --slurpfile ids region_file_id.txt  '
  INDEX(inputs; .titleId | ascii_upcase) as $dict
  | $ids[].id as $id
  | {$id} + $dict[$id]
' cnmts_titleId_otherApplicationId.txt > merged.txt

这似乎工作,直到我击中"null"值,我的文件不包括正确的id,这是另一个问题在一起!

ulydmbyx

ulydmbyx1#

“区域文件ID”中的所有“ID”等于“cnmts_titleId_otherApplicationId”中某处的“titleId”
如果确实如此,那么您可以按照以下步骤操作:

< cnmts_titleId_otherApplicationId.txt jq -n --slurpfile ids region_file_id.txt  '
  INDEX(inputs; .titleId) as $dict
  | $ids[].id as $id
  | {$id} + $dict[$id]
'

相关问题