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