0

i have this two json files: ubuntubionic.json:

  {
    "ubuntu": {
      "os_ver": "bionic",
      "image": "abcdef",
      "image_tag": "3.33.3",
      "docker_compiler_image": "abc",
      "image_compiler_tag": "4.44.4"
    }
  }

and ubuntufocal.json:

cat ubuntubionic.json
  {
    "ubuntu": {
      "os_ver": "focal",
      "image": "xxxx",
      "image_tag": "3.33.3",
      "docker_compiler_image": "xxxx",
      "image_compiler_tag": "4.44.4"
    }
  }`

i want to merge these two files into 1 file to get output that looks like this:

{
    "ubuntu": {
      "os_ver": "focal",
      "image": "abcdef",
      "image_tag": "3.33.3",
      "docker_compiler_image": "abc",
      "image_compiler_tag": "4.44.4"
    },
      "os_ver": "bionic",
      "image": "xxxx",
      "image_tag": "3.33.3",
      "docker_compiler_image": "xxxx",
      "image_compiler_tag": "4.44.4"

  }

i tried jq -s add ubuntufocal.json ubuntubionic.json > all_os.json but i'm getting that bionic is overwriting focal

cat all_os.json
{
  "ubuntu": {
    "os_ver": "bionic",
    "image": "xxxx",
    "image_tag": "3.33.3",
    "docker_compiler_image": "xxxx",
    "image_compiler_tag": "4.44.4"
  }
}

how can this be solved? got totally lost in the JQ man page

  • Your desired output misses the array brackets `[…]` (if your intention is to merge the files into an array). Having them just right after each other, separated by a comma, is not valid JSON. – pmf Jan 08 '22 at 10:53

1 Answers1

0

To just make it an array of the files' contents, you don't need add builtin as the -s flag already puts them together in an array

jq -s . ubuntubionic.json ubuntufocal.json
[
  {
    "ubuntu": {
      "os_ver": "bionic",
      "image": "abcdef",
      "image_tag": "3.33.3",
      "docker_compiler_image": "abc",
      "image_compiler_tag": "4.44.4"
    }
  },
  {
    "ubuntu": {
      "os_ver": "focal",
      "image": "xxxx",
      "image_tag": "3.33.3",
      "docker_compiler_image": "xxxx",
      "image_compiler_tag": "4.44.4"
    }
  }
]

If you want to pull out the ubuntu field name, and have each object's values (which happen to be objects as well) merged into an array, also use the map builtin:

jq -s '{ubuntu: map(.ubuntu)}' ubuntubionic.json ubuntufocal.json

or

jq -s '{ubuntu: map(add)}' ubuntubionic.json ubuntufocal.json
{
  "ubuntu": [
    {
      "os_ver": "bionic",
      "image": "abcdef",
      "image_tag": "3.33.3",
      "docker_compiler_image": "abc",
      "image_compiler_tag": "4.44.4"
    },
    {
      "os_ver": "focal",
      "image": "xxxx",
      "image_tag": "3.33.3",
      "docker_compiler_image": "xxxx",
      "image_compiler_tag": "4.44.4"
    }
  ]
}
pmf
  • 24,478
  • 2
  • 22
  • 31