iiiiiiiiiiiii
iiiiiiiiiiiii

Reputation: 3

Using a comma-separated string in shell to update a JSON list with jq?

How to append value to json key using?

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

jq '.doc[1].DEF[3].value="update comma separated val here" <<< "$jsonStr"

Upvotes: 0

Views: 447

Answers (1)

Charles Duffy
Charles Duffy

Reputation: 295413

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.

Upvotes: 2

Related Questions