0

I am trying to insert new key/value in json using jq. I am able to successfully insert the key but my value comes from a variable but I am not able to set it properly.

Here is my Code:

SHORT_NAME=`jq -r '.properties.cluster_name' filepath` //Output is "abc-test-00"
//Insert new key into json 
jq '.properties += { "short_name": "$SHORT_NAME" }' filepath 
    // It inserts short_name under properties but it shows "short_name" = "SHORT_NAME" instead of actual value
    ("abc-test-00")

JSON:

{
  "properties":
  {
     "cluster_name": "abc-test-00",
     "short_name": "$SHORT_NAME" 
  }
}

What I am missing ?

Bokambo
  • 4,204
  • 27
  • 79
  • 130
  • Please note that there is no need to use shell variables. With a very small modification to your program (e.g., using jq variables), you can achieve the result using just one invocation of jq. – peak Dec 08 '22 at 07:23
  • @peak : Example ? – Bokambo Dec 08 '22 at 17:25

0 Answers0