简体   繁体   中英

Get value from JSON using jq

I am trying to export json as a variable:

$ export VARIABLE='[{"id": 2, "name": "job", "description": "job", "token": "toknetokentokentoken/token="}]'

$ echo $VARIABLE | jq .

[
  {
    "id": 2,
    "name": "job",
    "description": "job",
    "token": "toknetokentokentoken/token="
  }
]

and then I am trying to get the value from token:

TOKEN=$(echo "$VARIABLE" | jq -r '.[] | select (.name == "job").token')

but it returns nothing. I am looking on to that but I can't find the problem.

try

VARIABLE='[{"id": 2, "name": "job", "description": "job", "token": "toknetokentokentoken/token="}]'
TOKEN=$(echo "$VARIABLE"|jq '.[] | select(.name == "job").token')
echo "$TOKEN"

Edit: as suggested a few clarifications:

the OP did not clariy on the it erturns nothing part but as @chepner pointed out in the comments: assingments don't procude output

backticks and $() are interchangeable, you should use $() since backticks are obsolete and don't support nesting

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM