0

I have variables with JSON:

export RESPONSE=$(curl -s --request POST --data "$some_data" $some_url)
In RESPONSE
{
 "data": {
   "warnings": "1",
   "auth": "no"
  ... and a lot of variables
  }
}

And I need to create variables how it:

$warnings="1"
$auth="no"
....

I only need variables from the field "data".

And in bash, as far as I understand, there is no to_entries.

And yet, I need that the data from #RESPONSE is not written to the console while the script is running.

Update:

I have this working code but it writes all JSON to console and that's not what I want.

export ENV_SETTER=$(for i in $KEYS; do echo -n env.$i=$(echo $RESPONSE | jq -r .data.$i),;done)

Next i need this

--set "${ENV_SETTER}"
knittl
  • 246,190
  • 53
  • 318
  • 364
LeNi1
  • 3
  • 3
  • 1
    `$RESPONSE={…` is not valid bash syntax to begin with. Please provide a [mre] (take the [tour], read [ask]). – knittl Oct 20 '22 at 09:40
  • 1
    And are you stuck with bash or with jq? With jq it should be as simple as `.data.warnings`. Please provide more details about the actual problem (again: a [mre] helps tremendously) – knittl Oct 20 '22 at 09:41
  • 1
    @LinFelix first answer https://unix.stackexchange.com/questions/413878/json-array-to-bash-variables-using-jq – LeNi1 Oct 20 '22 at 09:48
  • And you want _all_ properties turned into variables or just warnings and auth? Even if they are called `PATH`, potentially wreaking havoc? How trusted is the data? Why do you have to use variables and not use command substitution where the value is required? Asked differently: what do you gain by putting those values in distinct variables? – knittl Oct 20 '22 at 09:52
  • `to_entries` is not part of bash but part of jq – LinFelix Oct 20 '22 at 09:53
  • 1
    Does this answer your question? [using jq to assign multiple output variables](https://stackoverflow.com/questions/43291389/using-jq-to-assign-multiple-output-variables) – 0stone0 Oct 20 '22 at 09:54
  • @knittl all properties to variables. Data is secure. I am Update question, check please. – LeNi1 Oct 20 '22 at 10:25
  • @LinFelix Ok, ty. When i try to use this, i get this: /bin/bash: line 196: to_entries: command not found – LeNi1 Oct 20 '22 at 10:26
  • Show us line 196. – glenn jackman Oct 20 '22 at 13:23
  • @glennjackman, I am try this: 1. `echo RESPONSE | jq -r '.data | to_entries | .[] | .key + "=" + (.value | @sh)'` 2. `echo RESPONSE | jq -r .data | to_entries | .[] | .key + "=" + (.value | @sh)` 3. `echo RESPONSE | jq -r .data | to_entries | .[] | .key + "=" + .value` 4. `echo RESPONSE | jq -r .data | to_entries` – LeNi1 Oct 20 '22 at 14:49
  • Looks like you're forgetting to put the jq code in quotes – glenn jackman Oct 20 '22 at 15:11

1 Answers1

0

It is dangerous to set variables the way you want to. Already existing variables with the same name will be overwritten and this can damage your environment. Think about this security vulnerability beforehand.

I come up with two solutions

  1. using eval
  2. avoiding eval

Solution 1

eval $(jq -r '.data | to_entries | map("export " + .key + "='"'"'" + .value + "'"'"'")[]' "$FILE")

eval executes the code generated by jq:

export warnings='1'
export auth='no'

Remark

  • the cryptic '"'"'" is not jq syntax. It is a way to add single quotes to a string in bash

Solution 2

RESPONSE='
{
  "data": {
    "warnings": "1",
    "auth": "no"
  }
}
'

KEYS=$(jq -r '.data | keys[]' <<< "$RESPONSE")
for KEY in $KEYS; do
  VALUE=$(jq -r --arg key "$KEY" '.data | .[$key]' <<< "$RESPONSE")
  read "$KEY" <<< "$VALUE"
done
jpseng
  • 1,618
  • 6
  • 18
  • Thank you, but eval is not way to me – LeNi1 Nov 07 '22 at 08:53
  • I have added a second solution that avoids `eval`. – jpseng Nov 07 '22 at 09:49
  • Thank you. But solution 2 is not way to me too, because 1. My json not in file 2. On each iteration to console wrote all file. My json very big and it turns, what these records are logged and very large logs are obtained – LeNi1 Nov 08 '22 at 09:49
  • It is easy to change the input from a file to a string. I have edited solution 2 for you. On my computer nothing is written to the console – jpseng Nov 08 '22 at 10:45