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: Leonardyq 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.yamlHappy YAML’ing!
-
In this context avoid using
>output redirection because it would mangle the input file. Unless you redirect to another file, of course. ↩︎