thiagowfx's avatar

Β¬ just serendipity πŸ€ (not just serendipity)

Convert JSON to YAML

β€’ 119 words β€’ 1 min β€’ updated

⚠️ This post is over one year old. It may no longer be up to date or relevant. Opinions may have changed.

Given a YAML file that is effectively JSON, convert it to YAML format.

Input #

shell
% cat file.yaml
{
  "title": "The Big Bang Theory",
  "characters": [
    {
      "name": "Sheldon"
    },
    {
      "name": "Leonard"
    }
  ]
}

Output #

shell
% yq -i -P file.yaml
% cat file.yaml
title: The Big Bang Theory
characters:
  - name: Sheldon
  - name: Leonard

yq is like jq for YAML.

-i is for in-place modification1, -P is for pretty-printing.

If you don’t know about -i, you can always use sponge:

shell
% yq -P file.yaml | sponge file.yaml

Happy YAML’ing!


  1. In this context avoid using > output redirection because it would mangle the input file. Unless you redirect to another file, of course. ↩︎