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:

gt; cat ~/.docker/config.json { "auths": { "": { "auth": "aBcDeFghiJKlMn=" }, [...] } }
gt; cat .docker/config.json | jq . { "auths": { "": { "auth": "aBcDeFghiJKlMn=" }, [...] } }
gt; cat .docker/config.json | jq .auths { "": { "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:

gt; cat .docker/config.json | jq '.auths[""].auth' "aBcDeFghiJKlMn="
gt; cat .docker/config.json | jq -r '.auths[""].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.
Notify of

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

Inline Feedbacks
View all comments
Back to top button