2

I'm trying to figure out how to use jq to load the content of one json file into the hash of another file. E.g.:

this file:

{
  "annotations": {...},
  "rows": [ {...}, {...}]
}

should be inserted into this file at the hash dashboard:

{
  "dashboard": { },
  "overwrite": true,
  "message": "new commit"
}

so the resulting file should be

{
  "dashboard": {
     "annotations": {...},
     "rows": [ {...}, {...}]
  },
  "overwrite": true,
  "message": "new commit"
}

I was thinking to do it with an pipe | or |= operator but I can't figure out how to use one content and assign it to a select filter of the other file.

peak
  • 105,803
  • 17
  • 152
  • 177

2 Answers2

1

jq solution:

jq --slurpfile annot annot.json '.dashboard |= $annot[0]' dashb.json

--slurpfile variable-name filename:

This option reads all the JSON texts in the named file and binds an array of the parsed JSON values to the given global variable.

RomanPerekhrest
  • 88,541
  • 4
  • 65
  • 105
0

If your jq does not have --slurpfile, then you could, for example, run jq as follows:

jq -s '.[0] as $fragment | .[1] | (.dashboard |= $fragment)' fragment.json dashboard.json
peak
  • 105,803
  • 17
  • 152
  • 177