Home » DevOps » Escape JSON property names in jq

About Sebastian Daschner

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.

0 0 vote
Article Rating
(0 rating, 0 votes)
You need to be a registered member to rate this.
Start the discussion Views Tweet it!
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 ....
I agree to the Terms and Privacy Policy
Subscribe
Notify of
guest
The comment form collects your name, email and content to allow us keep track of the comments placed on the website. Please read and accept our website Terms and Privacy Policy to post a comment.

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

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x