'Use Jq to insert property of an object in another property

I have a json file that looks like this:

[
  {
    "account_id": 123, 
    "instances": [
      {"id": 1},
      {"id": 2}
    ]
  },
  {
    "account_id": 456, 
    "instances": [
      {"id": 1}
    ]
  }
]

I would like to flatten the instances into a single list and insert the account_id into each instance. Here is the result that I want.

[
  {"id": 1, "account_id": 123},
  {"id": 2, "account_id": 123},
  {"id": 1, "account_id": 456}
]

Here is a alternate solution that I came up with while waiting on an answer.

jq '.|map(.instances[]+{"account_id": .account_id})' file.json


Solution 1:[1]

First you have to change file permission like this chmod 400 key.pem then use ssh -i "key.pem" [email protected]

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 parhs