Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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' <<< ...
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading