2

I have a json object named version6json as follows

{
  "20007.098": {
    "os_version": "6.9",
    "kernel": "2.6.32-696",
    "sfdc-release": "2017.08"
  },
  "200907.09678”: {
    "os_version": "6.9",
    "kernel": "2.6.32-696",
    "sfdc-release": "201.7909"
  },
  "206727.1078”: {
    "os_version": "6.9",
    "kernel": "2.6.32-696.10.2.el6.x86_64",
    "sfdc-release": "20097.109”
  }
}

I want to add one more key value pair. The key is also a variable and the value too. bundle_release="2019.78" and value= {"release":"2018.1006","kernel":"2.6.32-754.3.5.el6.x86_64","os":"6.10","current":true} Now I want the bundle_release as key and value as its value, So the new entry would be "2018.1006": {"release":"2018.1006","kernel":"2.6.32-754.3.5.el6.x86_64","os":"6.10","current":true}

To achieve this, I am doing the folllowing

echo "$version6json" | jq --arg "$bundle_release" "$value" '. + {$bundle_release: "${value}"}'

Any help will be appriciated.

P.S- The question is edited as suggested by peak

3
  • What is in the bundle_release variable? Commented Oct 24, 2018 at 10:38
  • bundle_release="1034,567" I want to use this as key and a value against it. The value is {"release":"2018.1006","kernel":"2.6.32-754.3.5.el6.x86_64","os":"6.10","current":true} This I am fetching from items array. Thats why "${items[$i-1]}" , it is the value. Commented Oct 24, 2018 at 11:44
  • Please fix the JSON. should be replaced by ". You can use jq to validate the JSON, e.g. jq empty <<< "$version6json" Commented Oct 24, 2018 at 20:20

2 Answers 2

2

First, when specifying a key name using a variable in the way you are doing, the variable must be parenthesized, so you would have:

 {($bundle_release): ...}

Next, jq variables are not the same as shell variables and should be specified without quoting them, and without using bash-isms.

Third, when setting the value of the shell variable named value, you would have to quote the expression appropriately.

Fourth, to simplify things, use --argjson for $value.

Fifth, your sample JSON is not quite right. Once it's fixed, the following will work in a bash or bash-like environment (assuming you're using a version of jq that supports --argjson):

bundle_release="1034,567"
value='{"release":"2018.1006","kernel":"2.6.32-754.3.5.el6.x86_64","os":"6.10","current":true}'

jq --arg b "$bundle_release" --argjson v "$value"  '
  . + {($b): $v}' <<< "$version6json"
Sign up to request clarification or add additional context in comments.

4 Comments

I think I am not clear to u about my question. bundle_release is a variable having some value , I want to use this variable as a key and some other variable (say value) as value. The result you provided is not giving desired result. It is giving key and value same.
Your example only had one shell variable, so I used that. You could use —arg value "$value" for the other value, and then use $value for the value.
So if bundle_release="2098.09" is key and value={ "os_version": "6.9", "kernel": "2.6.32-696.23.1.el6.x86_64", "sfdc-release": "2018.0505" } then the command would be--> . jq --arg bundle_release value '. + {($bundle_release): $value}' . I tried this but not working
The answer has been updated in accordance with the updated question.
1

You're not giving the --arg option enough parameters: from the manual:

--arg name value:

This option passes a value to the jq program as a predefined variable. If you run jq with --arg foo bar, then $foo is available in the program and has the value "bar". Note that value will be treated as a string, so --arg foo 123 will bind $foo to "123".

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.