DevOps

Escape JSON property names in jq

I’m using the command line a lot and jq is a helpful tool for dealing with JSON data. You can access JSON object properties and array elements by concatenating the property names with dots and square brackets:

1
gt; cat ~/.docker/config.json { "auths": { "https://index.docker.io/v1/": { "auth": "aBcDeFghiJKlMn=" }, [...] } }
1
gt; cat .docker/config.json | jq . { "auths": { "https://index.docker.io/v1/": { "auth": "aBcDeFghiJKlMn=" }, [...] } }
1
gt; cat .docker/config.json | jq .auths { "https://index.docker.io/v1/": { "auth": "aBcDeFghiJKlMn=" }, [...] }

You can also access and escape “non-trivial” field names in different ways. You can get the encoded Docker auth authentication as follows, by using square brackets and quotes to escape the property name:

1
gt; cat .docker/config.json | jq '.auths["https://index.docker.io/v1/"].auth' "aBcDeFghiJKlMn="
1
gt; cat .docker/config.json | jq -r '.auths["https://index.docker.io/v1/"].auth' aBcDeFghiJKlMn=

You’ll notice the formatted and colorized output of jq. Have a look at the documentation.

Published on Java Code Geeks with permission by Sebastian Daschner, partner at our JCG program. See the original article here: Escape JSON property names in jq

Opinions expressed by Java Code Geeks contributors are their own.

Sebastian Daschner

Sebastian Daschner is a self-employed Java consultant and trainer. He is the author of the book 'Architecting Modern Java EE Applications'. Sebastian is a Java Champion, Oracle Developer Champion and JavaOne Rockstar.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button