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
3
Answers
With nodejs:
From a script :
output file:
You’re looking for something like this:
Using
reduce
would be another way. Same lengths provided, it allows iterating over the entries along with their indices, which can then be used to access the input array.To make this approach compatible with jq 1.4, replace the variable destructuring with a plain variable, and access its parts later, e.g.
Output: