首页 > 解决方案 > 如何在 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"
        }
      ]
    }

标签: jsonjq

解决方案


#!/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)

推荐阅读