I want to modify the following json using variables specified in a Linux bash shell using jq.
var1="red"
var2="european.flowers"
var3="european_vegetables"
var4="20"
My json:
{
"plants": {
"flowers.small": {
"colour": "",
"age": "",
"vegetables": {
"root": "",
"height": ""
}
}
}
}
I want to modify the json using variables in jq:
{
"plants": {
"${var2}": {
"colour": "${var1}",
"age": "",
"${var3}": {
"root": "",
"height": "${var4}"
}
}
}
}
I am attempting to just set a field value from a variables:
Command:
cat myjson.json|jq '.plants["flowers.small"].colour = "${var1}"' -c
The result is:
{"plants":{"flowers.small":{"colour":"${var1}","age":"","vegetables":{"root":"","height":""}}}}
Command:
cat myjson.json|jq --arg v "$var1" '.plants.["flowers.small"].colour = [$v]' -c
The result is:
jq: error: syntax error, unexpected '[', expecting FORMAT or QQSTRING_START (Unix shell quoting issues?) at <top-level>, line 1:
.plants.["flowers.small"].colour = $v
jq: 1 compile error
My jq version is: jq-1.5-1-a5b5cbe
How can I rename a field and set a value for the key from the variables? Is this even doable using the jq version?
catin favor of passing the input file name straight tojqor using<myjson.json.