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:


local prefix=$2
local s='[[:space:]]*' w='[a-zA-Z0-9_]*' fs=$(echo @|tr @ '\034')

sed -ne "s|^\($s\)\($w\)$s:$s\"\(.*\)\"$s\$|\1$fs\2$fs\3|p" \
     -e "s|^\($s\)\($w\)$s:$s\(.*\)$s\$|\1$fs\2$fs\3|p"  $1 |

awk -F$fs '{
   indent = length($1)/2;
   vname[indent] = $2;
   for (i in vname) {if (i > indent) {delete vname[i]}}
   if (length($3) > 0) {
      vn=""; for (i=0; i<indent; i++) {vn=(vn)(vname[i])("_")}
      printf("%s%s%s=\"%s\"\n", "'$prefix'",vn, $2, $3);

# Usage
# yaml_bash_parser.sh any_config.yml "config_"

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’

To parse a nested JSON object:

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

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.