0

I have the following json object, all levels of the object can be repeated.

{
  "Datacenter1": {
    "prd1": [
      "clone"
    ]
  }
}
{
  "Datacenter1": {
    "prd1": [
      "rrr"
    ]
  }
}
{
  "BackupSite": {
    "prd2": [
      "vol2"
    ]
  }
}

Is it possible to create a consolidated object using jq that will look like this, objects will be consolidated and lists within the object will be concatenated. I'm looking for a general solution that will work on any type of structure and depth.

{
  "Datacenter1": {
    "prd1": [
      "clone",
      "rrr"
    ]
  },
  "BackupSite": {
    "prd2": [
      "vol2"
    ]
  }
}
Haim Marko
  • 41
  • 2
  • Merging objects, concatenating arrays? How "dynamic" is the structure? Is it always an object with an array or can it be mix and match? What happens if you try to consolidate an array and an object? – knittl Apr 10 '23 at 09:18
  • @knittl: But [this](https://stackoverflow.com/a/24300110/724039) comment does not seem to work?: `jq -s add input.json` (It no longer has "clone") – Luuk Apr 10 '23 at 09:23
  • @Luuk I don't think that will work. But first OP has to tell us what behavior is expected exactly. – knittl Apr 10 '23 at 09:27
  • This comment has the correct answer: https://stackoverflow.com/a/68362041/724039. (When the question is changed from "Is it possible to create a consolidated object ...." to " How to create a consolidated ........" ) – Luuk Apr 10 '23 at 09:33
  • Can you provide an example that is not working with the provided answer? – Luuk Apr 10 '23 at 11:44
  • The example here https://stackoverflow.com/questions/53661930/jq-recursively-merge-objects-and-concatenate-arrays is relevant to 2 json inputs in my case there is one json file that contains unknow number of objects within it – Haim Marko Apr 10 '23 at 11:46
  • But you can use the same input twice, like: `jq -s 'def deepmerge......' input.json input.json` – Luuk Apr 10 '23 at 12:11

0 Answers0