yq/pkg/yqlib/doc/usage/kyaml.md
Robin H. Johnson c6029376a5
feat: K8S KYAML output format support (#2560)
* feat: K8S KYAML output format support

Reference: https://github.com/kubernetes/enhancements/blob/master/keps/sig-cli/5295-kyaml/README.md
Co-authored-by: Codex <codex@openai.com>
Generated-with: OpenAI Codex CLI (partial)
Signed-off-by: Robin H. Johnson <rjohnson@coreweave.com>

* build: gomodcache/gocache should not be committed

Signed-off-by: Robin H. Johnson <rjohnson@coreweave.com>

* chore: fix spelling of behaviour

Signed-off-by: Robin H. Johnson <robbat2@gentoo.org>

* build: pass GOFLAGS to docker to support buildvcs=false

In trying to develop the KYAML support, various tests gave false
positive results because they made assumptions about Git functionality
Make it possible to avoid that by passing GOFLAGS='-buildvcs=false' to
to Makefile.

Signed-off-by: Robin H. Johnson <robbat2@gentoo.org>

* doc: cover documentScenarios for tests

Signed-off-by: Robin H. Johnson <rjohnson@coreweave.com>

* build: exclude go caches from gosec

Without tuning, gosec scans all of the vendor/gocache/gomodcache, taking
several minutes (3m35 here), whereas the core of the yq takes only 15
seconds to scan.

If we intend to remediate upstream issues in future; add a seperate
target to scan those.

Signed-off-by: Robin H. Johnson <rjohnson@coreweave.com>

---------

Signed-off-by: Robin H. Johnson <rjohnson@coreweave.com>
Signed-off-by: Robin H. Johnson <robbat2@gentoo.org>
Co-authored-by: Codex <codex@openai.com>
2026-01-01 15:14:53 +11:00

3.1 KiB

KYaml

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.
  • Anchors and aliases are expanded (KYaml output does not emit them).

Encode kyaml: plain string scalar

Strings are always double-quoted in KYaml output.

Given a sample.yml file of:

cat

then

yq -o=kyaml '.' sample.yml

will output

"cat"

encode flow mapping and sequence

Given a sample.yml file of:

a: b
c:
  - d

then

yq -o=kyaml '.' sample.yml

will output

{
  a: "b",
  c: [
    "d",
  ],
}

encode non-string scalars

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",
}

quote non-identifier keys

Given a sample.yml file of:

"1a": b
"has space": c

then

yq -o=kyaml '.' sample.yml

will output

{
  "1a": "b",
  "has space": "c",
}

escape quoted strings

Given a sample.yml file of:

a: "line1\nline2\t\"q\""

then

yq -o=kyaml '.' sample.yml

will output

{
  a: "line1\nline2\t\"q\"",
}

preserve comments when encoding

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
}

Encode kyaml: anchors and aliases

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",
  },
}

Encode kyaml: yaml to kyaml shows formatting differences

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",
    ],
  },
}

Encode kyaml: nested lists of objects

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",
          },
        ],
      },
    ],
  },
]