0

How to append value to json key using?

val=""text"",""text"",""text""

jq '.doc[1].DEF[3].value="update comma separated val here" <<< "$jsonStr"
2
  • 2
    Please follow the minimal reproducible example guidelines. Commented Jan 15, 2019 at 14:59
  • Your assignment is exactly the same as val=text,text,text; the quotes do nothing whatsoever (they don't become part of the shell variable's value, because they're parsed as syntax). You can verify this with echo "$val". Commented Jan 15, 2019 at 15:59

1 Answer 1

2

If your goal is to split a string on commas to generate a list, and use your list in jq, that might look like:

val=text1,text2,text3
jq --arg val "$val" '.whatever.item |= ($val | split(","))' <<<'{"whatever": {}}'

Note:

  • There's no point to paired double-quote sets in the shell assignment -- they literally cancel each other out and don't become part of the variable's value.
  • The jq argument --arg is used to pass that variable from a shell context to a jq context.
  • The |= construct is used to modify a nested value while still evaluating to the larger document.
Sign up to request clarification or add additional context in comments.

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.