Home > Blockchain >  How to use the linux command line json parser (jq) to extract a specific value?
How to use the linux command line json parser (jq) to extract a specific value?

Time:06-16

I'm running this command line json parser (jq) in Debian GNU/Linux 11 (bullseye) to decode the json data:

jq -R 'split(".") | .[0],.[1] | @base64d | fromjson' <<< eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyLCJleHAiOjE2NTUzODQ4MzJ9.QVevY3qVLWHxfkNX1S4bas6PKSN75Nf96V7WmkcgbrE

{
  "alg": "HS256",
  "typ": "JWT"
}
{
  "sub": "1234567890",
  "name": "John Doe",
  "iat": 1516239022,
  "exp": 1655384832
}

Instead of returning ALL key/value pairs, is there a way to modify that command to extract only the expiration date 1655384832 value?

CodePudding user response:

You don't need the first object, you can ignore it (remove [0]). Then just specify which key you want:

jq -R 'split(".") | .[1] | @base64d | fromjson | .exp' <<< ...
  • Related