Объединение двух файлов JSON, получение значения атрибута на основе сопоставления значения ключа - PullRequest
0 голосов
/ 31 октября 2018

Я пытаюсь получить размер тома диска, который находится в другом файле 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? Спасибо за вашу помощь

1 Ответ

0 голосов
/ 31 октября 2018

Во-первых, давайте подумаем о подходящем способе вызова 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])
...