【问题标题】:Merge two json files, retrieving attribute value based on key value matching合并两个json文件,根据键值匹配检索属性值
【发布时间】:2018-10-31 01:12:15
【问题描述】:

我正在尝试根据从包含虚拟机信息的主 json 文件派生的 storage_volume 名称检索另一个 json 文件中的磁盘卷大小,以下结构的两个 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 完成这个?感谢您的帮助

【问题讨论】:

标签: json file merge jq


【解决方案1】:

首先,让我们想一个合适的方式来调用 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])

【讨论】:

  • 如果这是一个适合您的解决方案,那么接受@seetharamannandyal 的答案是合适的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-08-02
  • 1970-01-01
  • 1970-01-01
  • 2017-05-22
  • 2021-09-17
  • 1970-01-01
  • 2018-12-05
相关资源
最近更新 更多