合并两个json文件,根据键值匹配检索属性值

时间:2018-10-31 01:12:15

标签: json file merge jq

我试图根据从包含虚拟机信息的主json文件(以下结构的两个json文件)派生的storage_volume名称检索另一个json文件中的磁盘卷大小

file1.json

      {
 "hostname": "samplevm",
 "state": "running",
 "storage_attachments": [
    {
       "index": 1,
      "name": "/myaccount/user1@mycompany.com/cloud/samplevm/db_1/vm-1/ae53e3fb-0f90-4b93-adf5-1eee2ec78b00/0dc4d0cd-4be1-4220-b10b-5a1b105f1678",
      "storage_volume_name": "/myaccount/user1@mycompany.com/cloud/samplevm/db_1/vm-1/boot"
    },
    {
       "index": 2,
       "name": "/myaccount/user1@mycompany.com/cloud/samplevm/db_1/vm-1/ae53e3fb-0f90-4b93-adf5-1eee2ec78b00/1ac17918-c999-49a1-b200-957b2d56dbf7",
       "storage_volume_name": "/myaccount/user1@mycompany.com/cloud/samplevm/db_1/vm-1/user_data2"
      },
    {
      "index": 3,
      "name": "/myaccount/user1@mycompany.com/cloud/samplevm/db_1/vm-1/ae53e3fb-0f90-4b93-adf5-1eee2ec78b00/a068182e-882f-4314-bf00-0a4001935b26",
    "storage_volume_name": "/myaccount/user1@mycompany.com/cloud/samplevm/db_1/vm-1/user_data1"
  },
    {
    "index": 4,
      "name": "/myaccount/user1@mycompany.com/cloud/samplevm/db_1/vm-1/ae53e3fb-0f90-4b93-adf5-1eee2ec78b00/117a5abe-c5dd-43a6-8935-bebb739d358b",
       "storage_volume_name": "/myaccount/user1@mycompany.com/cloud/samplevm/db_1/vm-1/bits"
     },
     {
     "index": 5,
       "name": "/myaccount/user1@mycompany.com/cloud/samplevm/db_1/vm-1/ae53e3fb-0f90-4b93-adf5-1eee2ec78b00/ae5c5916-0a21-492e-ae20-5dd0fb383f0f",
      "storage_volume_name": "/myaccount/user1@mycompany.com/cloud/samplevm/db_1/vm-1/data"
      }
       ]
             }

file2.json

      [
  {
    "name": "/myaccount/user1@mycompany.com/cloud/samplevm/db_1/vm-1/bits",
    "size": "64424509440",
    "status": "Online"
  },
  {
    "name": "/myaccount/user1@mycompany.com/cloud/samplevm/db_1/vm-1/boot",
    "size": "34359738368",
    "status": "Online"
  },
  {
    "name": "/myaccount/user1@mycompany.com/cloud/samplevm/db_1/vm-1/data",
    "size": "536870912000",
    "status": "Online"
   },
  {
    "name": "/myaccount/user1@mycompany.com/cloud/samplevm/db_1/vm-1/user_data1",
    "size": "912680550400",
    "status": "Online"
  },
  {
    "name": "/myaccount/user1@mycompany.com/cloud/samplevm/db_1/vm-1/user_data2",
    "size": "80530636800",
    "status": "Online"
  }
]

我想合并这些文件,使输出文件看起来像

result.json

           {
  "hostname": "samplevm",
  "state": "running",
  "storage_attachments": [
    {
      "index": 1,
      "name": "/myaccount/user1@mycompany.com/cloud/samplevm/db_1/vm-1/ae53e3fb-0f90-4b93-adf5-1eee2ec78b00/0dc4d0cd-4be1-4220-b10b-5a1b105f1678",
      "storage_volume_name": "/myaccount/user1@mycompany.com/cloud/samplevm/db_1/vm-1/boot",
      "size": "34359738368",
      "status": "Online"
    },
    {
      "index": 2,
      "name": "/myaccount/user1@mycompany.com/cloud/samplevm/db_1/vm-1/ae53e3fb-0f90-4b93-adf5-1eee2ec78b00/1ac17918-c999-49a1-b200-957b2d56dbf7",
      "storage_volume_name": "/myaccount/user1@mycompany.com/cloud/samplevm/db_1/vm-1/user_data2",
      "size": "80530636800",
      "status": "Online"
    },
    {
      "index": 3,
      "name": "/myaccount/user1@mycompany.com/cloud/samplevm/db_1/vm-1/ae53e3fb-0f90-4b93-adf5-1eee2ec78b00/a068182e-882f-4314-bf00-0a4001935b26",
      "storage_volume_name": "/myaccount/user1@mycompany.com/cloud/samplevm/db_1/vm-1/user_data1",
      "size": "912680550400",
      "status": "Online"
     },
    {
      "index": 4,
      "name": "/myaccount/user1@mycompany.com/cloud/samplevm/db_1/vm-1/ae53e3fb-0f90-4b93-adf5-1eee2ec78b00/117a5abe-c5dd-43a6-8935-bebb739d358b",
      "storage_volume_name": "/myaccount/user1@mycompany.com/cloud/samplevm/db_1/vm-1/bits",
      "size": "64424509440",
      "status": "Online"
    },
    {
      "index": 5,
      "name": "/myaccount/user1@mycompany.com/cloud/samplevm/db_1/vm-1/ae53e3fb-0f90-4b93-adf5-1eee2ec78b00/ae5c5916-0a21-492e-ae20-5dd0fb383f0f",
      "storage_volume_name": "/myaccount/user1@mycompany.com/cloud/samplevm/db_1/vm-1/data",
      "size": "536870912000",
      "status": "Online"
    }
   ] 
 }

如何使用jq完成此操作?谢谢您的协助

1 个答案:

答案 0 :(得分:0)

首先,让我们考虑一种调用jq的适当方法。这是一种可能性:

jq -n --argfile file1 file1.json --argfile file2 file2.json -f merge.jq

这是merge.jq,它使用|=更新数组:

# Create the dictionary based on .name
($file2 | map( {(.name): {size, status}} )|add) as $dict
| $file1
| .storage_attachments |= map(. + $dict[.storage_volume_name])