0

I am running the below command from ksh prompt.

export client_token=$(echo $client_token_response | jq  -r '.auth.client_token') 

It is giving me the below error .

jq: error (at <stdin>:1): Cannot index string with string "auth"

The same command is working fine in bash prompt.

Could you please let me know what I am doing wrong here.

RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93
Anoop
  • 1
  • 1
    We need to know what's in `client_token_response`. The problem isn't specific to your code; it's the combination of your code _and the data it's run against_. – Charles Duffy Feb 16 '22 at 14:19
  • 1
    ...in general, this is what you would see if you had, say, `client_token_response='"hello world"'` -- because `"hello world"` is a JSON string and not a JSON map/dictionary/object, you can't index into it with a string. – Charles Duffy Feb 16 '22 at 14:20
  • 1
    BTW, you've got a separate but probably unrelated problem here due to the unquoted argument to `echo`. **Always** quote your expansions, as in, `echo "$client_token_response"` -- even better would be `printf '%s\n' "$client_token_response"`. See [I just assigned a variable but `echo $variable` shows something else!](https://stackoverflow.com/questions/29378566), and [Why is printf better than echo?](https://unix.stackexchange.com/questions/65803/why-is-printf-better-than-echo) – Charles Duffy Feb 16 '22 at 14:20
  • ...anyhow -- for the moment I'm voting to close this as lacking a [mre], since it doesn't include information needed to reproduce the problem (specifically, a sample `client_token_response` value). BTW, you can make your script print the value in an unambiguous form (suitable for copying-and-pasting into the code in your question to _make_ it a MRE) with `declare -p client_token_response` just above the line that fails. (You could also collect the same details via xtrace logging; either run `bash -x yourscript`, or add the line `set -x` within the script). – Charles Duffy Feb 16 '22 at 14:23
  • Thanks @CharlesDuffy for the comments . Adding Sample values. "request_id":"44cb64bb-360c-cd86-2f15-def332eba573" "lease_id":"" "renewable":false "lease_duration":0 "data":null "wrap_info":null "warnings":null "auth":"client_token":"s.XXXXXXXXXXXXXXXXX" "auth":"accessor":"XYXYXYYXYXYXYYXYXY" "auth":"policies":["xvxvxvvx" "auth":"xvxvvxvxvxvvx" "auth":"xvxvvxvxvxv" "auth":"default" "auth":"xvxvvxvxvvx"] – Anoop Feb 16 '22 at 14:48
  • 1
    Adding quote to argument helped !! It is working now ! Thankyou @CharlesDuffy – Anoop Feb 16 '22 at 14:52
  • Interesting. I'm really glad that helped! For the future, extra information is best added to the question with the [edit] button rather than put into comments. Your sample data is missing `{` and `}` characters and commas as shown in the comment, but it's hard to tell if that's a comment-formatting issue or really present in the data, which is part of why I suggested using `declare -p` to print the data as a shell assignment. – Charles Duffy Feb 16 '22 at 15:12
  • (BTW, one way the missing quotes could cause that bug is regions with square brackets, which can be either deleted if the `nullglob` shell option is on, or replaced with filenames that match those sequences when parsed as globs; strings containing `*` or other glob characters can be similarly munged). – Charles Duffy Feb 16 '22 at 15:13

0 Answers0