How to parse YAML string via Bash

If you like parse YAML via command line (bash or other), you can write script on JS for Nodejs, Ruby, PHP or other… Or you can use ready command line tools. It’s simply.

Of course you can search the finished tool, or even write your own parser, such as this:

This is great, but is it intended to work only with indentations made of two spaces. It’s not problem, you can change this script. But you can do all this even easier and better.

First step: convert YAML to JSON

Install Ruby and add to .bashrc code:

alias yaml2json="ruby -ryaml -rjson -e 'puts JSON.pretty_generate(YAML.load(ARGF))'"

or write function

function yaml2json()
{
ruby -ryaml -rjson -e \
'puts JSON.pretty_generate(YAML.load(ARGF))' $*
}

or write bash script yaml2json and put it into /usr/local/bin directory.

Second step: parse JSON

On Linux, there is a command-line JSON processor called jq which does exactly that. Using jq, you can parse, filter, map, and transform JSON-structured data effortlessly.

Install JQ

For Debian and Ubuntu

$ sudo apt-get install jq

For Centos

$ sudo yum install jq

For others see https://stedolan.github.io/jq/download/

How use JQ

An example JSON Schema:

$ cat anyfile.json
{
"name": "NewHR",
"location": {
"street": "Tverskaya",
"city": "Moscow",
"country": "Russian Federation"
},
"employees": [
{
"name": "Alexander Majorov",
"division": "Engineering"
}
]
}

To parse a JSON object:

$ cat anyfile.json | jq ‘.name’
"NewHR"

To parse a nested JSON object:

$ cat anyfile.json | jq ‘.location.city’
"Moscow"

To parse a JSON array:

$ cat anyfile.json | jq ‘.employees[0].name’
"Alexander Majorov"

To extract specific fields from a JSON object:

$ cat anyfile.json | jq ‘.location | {street, city}’
{
"street": "Tverskaya",
"city": "Moscow"
}

Parse YAML

Write pipe line:

cat anyfile.yaml | yaml2json | jq 'your query'

or write bash script and call it with arguments.