json - jq - add new field with updating whole file -
i have json file constructed in simmilar way:
[ { "_id":"1234", "org":"org1", "int": {"url":"http://url.com.uk:1234"}}, { "_id":"4321", "org":"org2", "int": {"url":"http://url.com.us:4321"}}, ... ] now im "jumping" 1 entry , checking if under url application working properly. after check want add/update field "status". can't update whole file, im getting:
$ jq --arg mod "good" '.[0].int + {stat: $mod}' tmp.json { "url": "http://url.com.uk:1234", "stat": "good" } how can jq command new updated whole file, not part of it?
if put data in data.json , changes want make each record separate arg.json argument file like
{ "1234": { "int": { "stat": "good" } }, "4321": { "int": { "stat": "bad", "xxx": "yyy" } } } and run jq as
$ jq -m --argfile arg arg.json 'map(. + $arg[._id])' data.json then output updated data, e.g.
[ { "_id": "1234", "org": "org1", "int": { "stat": "good" } }, { "_id": "4321", "org": "org2", "int": { "stat": "bad", "xxx": "yyy" } } ] note + replaces keys. if want merge keys can use * e.g.
$ jq -m --argfile arg arg.json 'map(. * $arg[._id])' data.json which generates
[ { "_id": "1234", "org": "org1", "int": { "url": "http://url.com.uk:1234", "stat": "good" } }, { "_id": "4321", "org": "org2", "int": { "url": "http://url.com.us:4321", "stat": "bad", "xxx": "yyy" } } ] if want update data in place use sponge described in answer manipulate json jq e.g.
$ jq -m --argfile arg arg.json 'map(. * $arg[._id])' data.json | sponge data.json wiki
Comments
Post a Comment