如何在bash shell中合并具有InstanceId唯一性的对象?

时间:2018-12-12 08:01:26

标签: json jq

我有两个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"
        }
      ]
    }

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:-1)

  1. 缩小两个json文件
  2. 尝试以下命令:

    cat tmp2.json|jq -r '"\(.InstanceId) \(.InstanceName)"'|xargs -n2 sh -c 'cat tmp1.json|jq "if .Instances[0].InstanceId==\"$0\" then .Instances[0].InstanceName=\"$1\" else empty end"'
    

以下是输出:

{
  "VolumeId": "vol-007253a7d24c1c668",
  "Instances": [
    {
      "InstanceId": "i-0c0650c15b099b993",
      "InstanceName": "Test1"
    }
  ]
}
{
  "VolumeId": "vol-046e0be08ac95095a",
  "Instances": [
    {
      "InstanceId": "i-020ce1b2ad08fa6bd",
      "InstanceName": "Test"
    }
  ]
}