1

Below is the bash-script that i use to parse the json-response from my OAuth provider. My intention is to extract only the access-token from the json response

CLIENT_ID=MASKED
CLIENT_SECRET=MASKED
RESOURCE=MASKED

TOKEN_JSON_RESPONSE=$(
    curl https://corp.company.com/adfs/oauth2/token/ -H 'Accept: application/json' -H 'Content-Type: application/x-www-form-urlencoded' -d 'client_id='$CLIENT_ID'&client_secret='$CLIENT_SECRET'&grant_type=client_credentials&resource='$RESOURCE
)

echo 'The token response is ' $TOKEN_JSON_RESPONSE

echo ' @@@@@@@@@@@@@@@@@  '

ACCESS_TOKEN=$TOKEN_JSON_RESPONSE | jq '.access_token'

echo 'The access token is '$ACCESS_TOKEN

I am getting the JSON response properly and below is that

{
   "access_token":"eyJxxxxxxxx",
   "token_type":"bearer",
   "expires_in":3600
}

All that i need is to just to extract the value in the access_token .. Currently in the above script, i get an empty string after using jq-parser

Update:

when i directly print this way, it works echo $TOKEN_JSON_RESPONSE|jq '.access_token’

whereas when i put the same in a variable it doesn't work

ACCESS_TOKEN=$TOKEN_JSON_RESPONSE|jq '.access_token’ echo $ACCESS_TOKEN

Arun
  • 3,440
  • 11
  • 60
  • 108
  • What is this line `ACCESS_TOKEN=$TOKEN_JSON_RESPONSE | jq ...` supposed to do? – oguz ismail Aug 27 '20 at 08:14
  • This line is supposed to capture the access token alone in a variable and i will reuse this variable further while making an API call to my application .. will attach this bearer token and make multuple calls – Arun Aug 27 '20 at 08:16
  • 2
    It doesn't work like that though. You need a command substitution there, like `ACCESS_TOKEN=$(jq ... <<<"$TOKEN_JSON_RESPONSE")`. – oguz ismail Aug 27 '20 at 08:18
  • I'm not convinced this question should be closed as a duplicate of a non-jq-specific question and of https://stackoverflow.com/questions/4775548/how-to-pass-the-value-of-a-variable-to-the-stdin-of-a-command in particular. There are various ways to feed data to jq (in the pattern, via stdin and via a bunch of different command-line options) and some are more appropriate than others. Add to that the crux of this question is about capturing output into a variable, which is not a topic of the linked question. – Weeble Aug 30 '20 at 00:20

1 Answers1

3

You need to pass TOKEN_JSON_RESPONSE value to jq stdin. It will print to stout result and then you save this result to variable:

#!/bin/bash                                                                                                                                                                                                                                 

TOKEN_JSON_RESPONSE='                                                                                                                                                                                                                       
{                                                                                                                                                                                                                                           
   "access_token":"eyJxxxxxxxx",                                                                                                                                                                                                            
   "token_type":"bearer",                                                                                                                                                                                                                   
   "expires_in":3600                                                                                                                                                                                                                        
}'

echo 'The token response is ' $TOKEN_JSON_RESPONSE

ACCESS_TOKEN=$(echo $TOKEN_JSON_RESPONSE | jq -j '.access_token')

echo 'The access token is '$ACCESS_TOKEN

Execution:

The token response is  { "access_token":"eyJxxxxxxxx", "token_type":"bearer", "expires_in":3600 }
The access token is eyJxxxxxxxx

#Update1

As suggested in comments below it may be written without echo:

jq -jn "$TOKEN_JSON_RESPONSE|.access_token"
Łukasz Ślusarczyk
  • 1,775
  • 11
  • 20
  • 2
    you need `jq -j` or `jq -r` to output raw data, or you get the output as a JSON string with quotes. – Léa Gris Aug 27 '20 at 08:44
  • @LéaGris, thank you, fixed, nice way to get rid of quotes in answer which I did not know – Łukasz Ślusarczyk Aug 27 '20 at 08:48
  • `ACCESS_TOKEN=$(jq -jn "$TOKEN_JSON_RESPONSE|.access_token")` and no need for the `echo ... |`. `jq -n` for null input. – Léa Gris Aug 27 '20 at 08:55
  • 1
    I know it's a bit late since the question was closed, but rather than inserting arbitrary data under someone else's control into the pattern it's _generally_ better practice to pass it as a separate argument. E.g. `jq -jn -argjson response "$TOKEN_JSON_RESPONSE" '$response.access_token'` It probably doesn't matter too much here but if the response for some reason isn't valid JSON this avoids the risk of it getting interpreted as a pattern. – Weeble Aug 30 '20 at 00:18