pkg/yqlib/doc/usage/kyaml.md
Encode and decode to and from KYaml (a restricted subset of YAML that uses flow-style collections).
KYaml is useful when you want YAML data rendered in a compact, JSON-like form while still supporting YAML features like comments.
Notes:
Strings are always double-quoted in KYaml output.
Given a sample.yml file of:
cat
then
yq -o=kyaml '.' sample.yml
will output
"cat"
Given a sample.yml file of:
a: b
c:
- d
then
yq -o=kyaml '.' sample.yml
will output
{
a: "b",
c: [
"d",
],
}
Given a sample.yml file of:
a: 12
b: true
c: null
d: "true"
then
yq -o=kyaml '.' sample.yml
will output
{
a: 12,
b: true,
c: null,
d: "true",
}
Given a sample.yml file of:
"1a": b
"has space": c
then
yq -o=kyaml '.' sample.yml
will output
{
"1a": "b",
"has space": "c",
}
Given a sample.yml file of:
a: "line1\nline2\t\"q\""
then
yq -o=kyaml '.' sample.yml
will output
{
a: "line1\nline2\t\"q\"",
}
Given a sample.yml file of:
# leading
a: 1 # a line
# head b
b: 2
c:
# head d
- d # d line
- e
# trailing
then
yq -o=kyaml '.' sample.yml
will output
# leading
{
a: 1, # a line
# head b
b: 2,
c: [
# head d
"d", # d line
"e",
],
# trailing
}
KYaml output does not support anchors/aliases; they are expanded to concrete values.
Given a sample.yml file of:
base: &base
a: b
copy: *base
then
yq -o=kyaml '.' sample.yml
will output
{
base: {
a: "b",
},
copy: {
a: "b",
},
}
KYaml uses flow-style collections (braces/brackets) and explicit commas.
Given a sample.yml file of:
person:
name: John
pets:
- cat
- dog
then
yq -o=kyaml '.' sample.yml
will output
{
person: {
name: "John",
pets: [
"cat",
"dog",
],
},
}
Lists and objects can be nested arbitrarily; KYaml always uses flow-style collections.
Given a sample.yml file of:
- name: a
items:
- id: 1
tags:
- k: x
v: y
- k: x2
v: y2
- id: 2
tags:
- k: z
v: w
then
yq -o=kyaml '.' sample.yml
will output
[
{
name: "a",
items: [
{
id: 1,
tags: [
{
k: "x",
v: "y",
},
{
k: "x2",
v: "y2",
},
],
},
{
id: 2,
tags: [
{
k: "z",
v: "w",
},
],
},
],
},
]