Example: jq

575 words · 3 min read

Tutorial: String output

I simplified examples from here: https://medium.com/@frontman/how-to-parse-yaml-string-via-command-line-374567512303

cat ./data/ex_jq_2.json

## {
##    "field": "a",
##    "object": {
##       "field": "b"
##    },
##    "array": [
##      {
##         "f1": "c",
##         "f2": "d"
##      }
##    ]
## }
## {
##    "field": "a",
##    "object": {
##       "field": "b"
##    },
##    "array": [
##      {
##         "f1": "c",
##         "f2": "d"
##      }
##    ]
## }

String output

Get by key: .key

cat ./data/ex_jq_2.json | jq '.field'
## "a"
## "a"

Get nested element by key: .array[0].key

cat ./data/ex_jq_2.json | jq '.array[0].f1'
## "c"
## "c"

Json output

Get multiple fields: {field1, field2}

cat ./data/ex_jq_2.json | jq '.object | {field}'

## {
##   "field": "b"
## }
## {
##   "field": "b"
## }

Using jq for yaml files

yq is a jq wrapper. Download: https://github.com/kislyuk/yq

cat ./data/ex_jq_2.yaml

## ---
## field: a
## object:
##   field: b
## array:
## - f1: c
## - f2: d
## ---
## field: a
## object:
##   field: b
## array:
## - f1: c
## - f2: d

String output

Get by key: .key

cat ./data/ex_jq_2.yaml | yq '.field'
## "a"
## "a"

Get nested element by key: .array[0].key

cat ./data/ex_jq_2.yaml | yq '.array[0].f1'
## "c"
## "c"

yaml output

Get multiple fields: {field1, field2}

cat ./data/ex_jq_2.yaml | yq '.object | {field}'

## {
##   "field": "b"
## }
## {
##   "field": "b"
## }
cat ./data/ex_jq_2.yaml | yq -y '.object | {field}'

## field: b
## field: b

Official Tutorial jq

I simplified examples from here: https://stedolan.github.io/jq/tutorial/

curl 'https://api.github.com/repos/stedolan/jq/commits?per_page=2' > ./data/ex_jq_github.json
head -n 6 ./data/ex_jq_github.json
cat ./data/ex_jq_github.json | jq '.' | head -n 5
## [
##   {
##     "sha": "61edf3fa93f6177ef099b1b0cb2b49813a35c546",
##     "commit": {
##       "author": {
## [
##   {
##     "sha": "61edf3fa93f6177ef099b1b0cb2b49813a35c546",
##     "commit": {
##       "author": {

Get first item: .[0]

cat ./data/ex_jq_github.json | jq '.[0]' | head -n 5
## {
##   "sha": "61edf3fa93f6177ef099b1b0cb2b49813a35c546",
##   "commit": {
##     "author": {
##       "name": "Larry Aasen",
## {
##   "sha": "61edf3fa93f6177ef099b1b0cb2b49813a35c546",
##   "commit": {
##     "author": {
##       "name": "Larry Aasen",

Get first item: .[0] Then get .commit.message

cat ./data/ex_jq_github.json | jq '.[0] | {message: .commit.message}' | head -n 5
## {
##   "message": "Updated the compile-ios.sh script to fix issues with local oniguruma path."
## }
## {
##   "message": "Updated the compile-ios.sh script to fix issues with local oniguruma path."
## }

Get all items: .[] Then get .commit.message from each

cat ./data/ex_jq_github.json | jq '.[] | {message: .commit.message}' | head -n 5

## {
##   "message": "Updated the compile-ios.sh script to fix issues with local oniguruma path."
## }
## {
##   "message": "Update AUTHORS"
## {
##   "message": "Updated the compile-ios.sh script to fix issues with local oniguruma path."
## }
## {
##   "message": "Update AUTHORS"

Wrap output as a single array, by wrapping the filter in brackets: [<filter>]

cat ./data/ex_jq_github.json | jq '[.[] | {message: .commit.message}]' | head -n 5

## [
##   {
##     "message": "Updated the compile-ios.sh script to fix issues with local oniguruma path."
##   },
##   {
## [
##   {
##     "message": "Updated the compile-ios.sh script to fix issues with local oniguruma path."
##   },
##   {

 Tech    12 Dec, 2017

Any work (images, writings, presentations, ideas or whatever) which I own is always provided under
Creative Commons License Creative Commons Attribution-Share Alike 3.0 License

Mert Nuhoglu is a Trabzon-born programmer and data scientist.

You may also like...