0

I have the following objects:

// exhibit a
{
  "exported_modules": ["a", "b"]
}
// exhibit b
{
  "name": "thing b"
}

They exist in separate files. I need to basically transform them such that they are output unified in the following state:

{
  "exported_modules": ["a", "b"],
  "modules": {
    "a": {
      "name": "thing b"
    }
 }
}

Essentially I need to insert the contents of exhibit b into .modules.a. Is there a way to do this in jq? I'm on jq 1.3 unfortunately, which is provided by my distribution.

I have been trying what has been recommended here but I have not been able to merge into a specific location rather than just merge all keys.

Naftuli Kay
  • 87,710
  • 93
  • 269
  • 411

1 Answers1

0

jq 1.3 is ancient so the solution using that version is rather inelegant. Perhaps the best approach would be to "slurp" the two files:

jq1.3 -s '.[1] as $o | .[0] | .modules.a = $o' exhibit-a.json exhibit-b.json

This will work with later versions as well, but for comparison, using jq 1.4 or later, one could write:

jq --argfile modules exhibit-a.json '
  . as $in | $modules | .modules.a = $in' exhibit-b.json
peak
  • 105,803
  • 17
  • 152
  • 177