json - 如何在 bash shell 中合并具有唯一 InstanceId 的对象?
问题描述
我有两个json文件如下:
我想将 tmp1.json 和 tmp2.json 中的对象与 bash shell 中的 InstanceId 唯一值合并。
我已经尝试使用带有 argjson 选项的 jq,但我的 jq 1.4 版本不支持此选项。抱歉,我无法将 jq 更新到 1.5 版本。
#cat tmp1.json
{
"VolumeId": "vol-046e0be08ac95095a",
"Instances": [
{
"InstanceId": "i-020ce1b2ad08fa6bd"
}
]
}
{
"VolumeId": "vol-007253a7d24c1c668",
"Instances": [
{
"InstanceId": "i-0c0650c15b099b993"
}
]
}
#cat tmp2.json
{
"InstanceId": "i-0c0650c15b099b993",
"InstanceName": "Test1"
}
{
"InstanceId": "i-020ce1b2ad08fa6bd",
"InstanceName": "Test"
}
我想要的是:
{
"VolumeId": "vol-046e0be08ac95095a",
"Instances": [
{
"InstanceId": "i-020ce1b2ad08fa6bd"
"InstanceName": "Test"
}
]
}
{
"VolumeId": "vol-007253a7d24c1c668",
"Instances": [
{
"InstanceId": "i-0c0650c15b099b993"
"InstanceName": "Test1"
}
]
}
解决方案
#!/bin/bash
JQ=jq-1.4
# For ease of understanding, the following is a bit more verbose than
# necessary.
# One way to get around the constraints of using jq 1.4 is
# to use the "slurp" option so that the contents of the two files can
# be kept separately.
# Note that jq 1.6 includes the following def of INDEX, but we can use it with jq 1.4.
($JQ -s . tmp1.json ; $JQ -s . tmp2.json) | $JQ -s '
def INDEX(stream; idx_expr):
reduce stream as $row ({};
.[$row|idx_expr|
if type != "string" then tojson
else .
end] |= $row);
.[0] as $tmp1
| .[1] as $tmp2
| INDEX($tmp2[]; .InstanceId) as $dict
| $tmp1
| map( .Instances |= map(.InstanceName = $dict[.InstanceId].InstanceName))
| .[]
'
流线型
INDEX(.[1][]; .InstanceId) as $dict
| .[0][]
| .Instances |= map(.InstanceName = $dict[.InstanceId].InstanceName)
推荐阅读
- nestjs - 在 Seqelize 中使用“where”选项调用 findOne() 时出现无效值异常
- oracle - 在 Oracle SQL 的功能块中声明数字变量时出错
- excel - 转换为数据库布局、VLookup、Round、Months
- magento - Magento 2 - 使用 GraphQL 单独加载不可见的产品
- jupyter-notebook - 设置 ipython 内核启动的路径(不是 jupyter)
- mysql - 当我在 MYSQL 8.0 中执行 char(97) 时,它显示 0x61。为什么?我想要'a'作为答案
- atomic - 原子值/可分性达到 1NF
- r - 按集群导出 Seurat 对象数据
- wordpress - 如何在 All-in-One WP Migration 中排除文件夹?
- python - 使用气流在 S3 上上传数据