Jq: How to add a JSON object at a particular location in an existing JSON?

Created on 6 Nov 2015  路  5Comments  路  Source: stedolan/jq

I have a JSON like

{ "a": { "b": {}}}

and I need to add

"c": { ... }

where c is a fairly complex JSON object in it's own right. The former is in a file a.json, the latter in c.json. I came across the simple jq -s add a.json c.json - but I can't figure out how to add c.json at a particular location in a.json, outputting the correct mashup of the two.

Thanks in advance.

Most helpful comment

$ jq --argfile c c.json '.c = $c[0]' a.json

All 5 comments

It's unclear what you mean by "correct mashup"; also I'm not clear what exactly is in c.json (is it the object you represented by "{...}"?).

Also, for future reference, usage questions such as this seems to be are generally best asked over at https://stackoverflow.com/questions/tagged/jq

So - I have control over what goes in c.json - but nothing I've worked has been successful yet. I could have c.json contain

{ "c": { ... } }

or

{ "a": { "b": { "c": { ... } } } }

but, the a.json is going to have other ("d","e","f",etc.) keys defined on the "b" object. - and I need the merged output to have "c" added to it.

Does that help?

Sorry - didn't know about the SO tag. Thx, will use next time.

$ jq --argfile c c.json '.c = $c[0]' a.json

Yeah, bam - I can work with that @nicowilliams. Thanks.

How to do the same without an arg file ? Basically, I want to add in package.json into scripts.myscript = 'myCmd'
?

Was this page helpful?
0 / 5 - 0 ratings