yq/pkg/yqlib/doc/usage/convert.md
2022-02-06 14:39:46 +11:00

2.1 KiB

JSON

Encode and decode to and from JSON. Note that YAML is a superset of JSON - so yq can read any json file without doing anything special.

This means you don't need to 'convert' a JSON file to YAML - however if you want idiomatic YAML styling, then you can use the -P/--prettyPrint flag, see examples below.

{% hint style="warning" %} Note that versions prior to 4.18 require the 'eval/e' command to be specified.

yq e <exp> <file> {% endhint %}

Parse json: simple

JSON is a subset of yaml, so all you need to do is prettify the output

Given a sample.json file of:

{"cat": "meow"}

then

yq -P '.' sample.json

will output

cat: meow

Parse json: complex

JSON is a subset of yaml, so all you need to do is prettify the output

Given a sample.json file of:

{"a":"Easy! as one two three","b":{"c":2,"d":[3,4]}}

then

yq -P '.' sample.json

will output

a: Easy! as one two three
b:
  c: 2
  d:
    - 3
    - 4

Encode json: simple

Given a sample.yml file of:

cat: meow

then

yq -o=json '.' sample.yml

will output

{
  "cat": "meow"
}

Encode json: simple - in one line

Given a sample.yml file of:

cat: meow # this is a comment, and it will be dropped.

then

yq -o=json -I=0 '.' sample.yml

will output

{"cat":"meow"}

Encode json: comments

Given a sample.yml file of:

cat: meow # this is a comment, and it will be dropped.

then

yq -o=json '.' sample.yml

will output

{
  "cat": "meow"
}

Encode json: anchors

Anchors are dereferenced

Given a sample.yml file of:

cat: &ref meow
anotherCat: *ref

then

yq -o=json '.' sample.yml

will output

{
  "cat": "meow",
  "anotherCat": "meow"
}

Encode json: multiple results

Each matching node is converted into a json doc. This is best used with 0 indent (json document per line)

Given a sample.yml file of:

things: [{stuff: cool}, {whatever: cat}]

then

yq -o=json -I=0 '.things[]' sample.yml

will output

{"stuff":"cool"}
{"whatever":"cat"}