I have two JSON files:
$ jq . a.json b.json
{
"id": "ZGVhZGJlZWY=",
"name": "first file",
"version": 1,
"description": "just a simple json file"
}
{
"version": 2,
"name": "fake name",
"dependencies": [
4,
2
],
"comment": "I'm just sitting here, ignore me"
}
and want to merge them into a single file (think of file 1 as "template" and file 2 as "actual values"). I don't want to merge all properties, I only want to transfer some properties of the second file (specifically only version
and dependencies
). version
should overwrite the value in the original file and dependencies
should be added to the new file. name
must not be overwritten and the original name must be kept.
This is the expected result:
{
"id": "ZGVhZGJlZWY=",
"name": "first file",
"version": 2,
"description": "just a simple json file",
"dependencies": [
4,
2
]
}
I know that jq supports the +
and *
operators to merge or merge recursively, but how can I apply those to only some properties and not all? How can I access both files in my jq program; do I have to preprocess the file and then use --arg
in a second jq call?
Obviously, jq '. + {version, dependencies}' a.json b.json
does not work. What is the correct program here?
What would the solution look like if description
should also be dropped from the output?