I've 2 files: changes.csv and sample.json. My csv file is like following:
header "a", "b"
"a11","b1"
"a22","b2"
"a33","b3"
and the json file is like:
[
{"a":"a1","b":"b1"},
{"a":"a2","b":"b2"},
{"a":"a3","b":"b3"},
{"a":"a4","b":"b4"}
]
I need to write a jq command, which make changes in json file using the csv file, i.e., the final output of the json file should be like following:
[
{"a":"a11","b":"b1"},
{"a":"a22","b":"b2"},
{"a":"a33","b":"b3"},
{"a":"a4","b":"b4"}
]
I wrote the following command:
while IFS=",", read f1 f2
do
jq --argjson k1 $f1 --argjson k2 $f2 '(.[] | select(.b == $k2) | .a) |= $k1' sample.json| sponge sample.json
done < changes.csv
Although, for each iteration it is able to filter and update the value of key "a", but when I try to sponge the results into the json file, it is unable to do so. Don't know where exactly I am missing out.