I have two files
file 1
{
"version": "2",
"run_id": "1",
"crn": "crn",
"date": "2022-11-02T02:09:30.629Z",
"evidences": [
{
"version": "2",
"id": "2d"
},
{
"version": "2",
"id": "3e"
}
]
file 2
[
{
"version": "3",
"id": "3d"
},
{
"version": "2",
"id": "3f"
}
]
can we use jq to merge contents from file 2 into evidences section of file 1? which the result should like below
file 1
{
"version": "2",
"run_id": "1",
"crn": "crn",
"date": "2022-11-02T02:09:30.629Z",
"evidences": [
{
"version": "2",
"id": "2d"
},
{
"version": "2",
"id": "3e"
},
{
"version": "3",
"id": "3d"
},
{
"version": "2",
"id": "3f"
}
]
>Solution :
All you need is += to add up arrays while retaining the context, and input to access the second file:
jq '.evidences += input' file1.json file2.json
{
"version": "2",
"run_id": "1",
"crn": "crn",
"date": "2022-11-02T02:09:30.629Z",
"evidences": [
{
"version": "2",
"id": "2d"
},
{
"version": "2",
"id": "3e"
},
{
"version": "3",
"id": "3d"
},
{
"version": "2",
"id": "3f"
}
]
}