Home » DevOps » Escape JSON property names in jq

About 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.

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.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

 

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

 

and many more ....

 

Receive Java & Developer job alerts in your Area

I have read and agree to the terms & conditions

 

Subscribe
Notify of
guest

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

0 Comments
Inline Feedbacks
View all comments