I have a bash $string containing the values: abc,def and a file.json looking like this:
[
{
"loc": "51.12345, 12.12345",
"city": "CityName1"
},
{
"loc": "65.12345, 15.12345",
"city": "CityName2"
}
]
I'm trying to update the city field with the values from the string to get this result:
[
{
"loc": "51.12345, 12.12345",
"city": "abc"
},
{
"loc": "65.12345, 15.12345",
"city": "def"
}
]
I'm trying this code but it doesn't work, any suggestions?
string="abc,def"; jq --arg variable "$string" '.city = $string' file.json
You're looking for something like this:
$ string=abc,def
$ jq --arg cities "$string" '[., ($cities / ",")] | transpose | map(.[0] + {city: .[1]})' file.json
[
{
"loc": "51.12345, 12.12345",
"city": "abc"
},
{
"loc": "65.12345, 15.12345",
"city": "def"
}
]
$
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With