y
y
yq
Search…
Encode / Decode
Encode operators will take the piped in object structure and encode it as a string in the desired format. The decode operators do the opposite, they take a formatted string and decode it into the relevant object structure.
Note that you can optionally pass an indent value to the encode functions (see below).
These operators are useful to process yaml documents that have stringified embedded yaml/json/props in them.
Format
Decode (from string)
Encode (to string)
Yaml
from_yaml
to_yaml(i)/@yaml
JSON
from_json
to_json(i)/@json
Properties
from_props
to_props/@props
CSV
to_csv/@csv
TSV
to_tsv/@tsv
XML
from_xml
to_xml(i)/@xml
Base64
@base64d
@base64
CSV and TSV format both accept either a single array or scalars (representing a single row), or an array of array of scalars (representing multiple rows).
XML uses the --xml-attribute-prefix and xml-content-name flags to identify attributes and content fields.
Base64 assumes rfc4648 encoding. Encoding and decoding both assume that the content is a string.
Note that versions prior to 4.18 require the 'eval/e' command to be specified.
yq e <exp> <file>

Encode value as json string

Given a sample.yml file of:
1
a:
2
cool: thing
Copied!
then
1
yq '.b = (.a | to_json)' sample.yml
Copied!
will output
1
a:
2
cool: thing
3
b: |
4
{
5
"cool": "thing"
6
}
Copied!

Encode value as json string, on one line

Pass in a 0 indent to print json on a single line.
Given a sample.yml file of:
1
a:
2
cool: thing
Copied!
then
1
yq '.b = (.a | to_json(0))' sample.yml
Copied!
will output
1
a:
2
cool: thing
3
b: '{"cool":"thing"}'
Copied!

Encode value as json string, on one line shorthand

Pass in a 0 indent to print json on a single line.
Given a sample.yml file of:
1
a:
2
cool: thing
Copied!
then
1
yq '.b = (.a | @json)' sample.yml
Copied!
will output
1
a:
2
cool: thing
3
b: '{"cool":"thing"}'
Copied!

Decode a json encoded string

Keep in mind JSON is a subset of YAML. If you want idiomatic yaml, pipe through the style operator to clear out the JSON styling.
Given a sample.yml file of:
1
a: '{"cool":"thing"}'
Copied!
then
1
yq '.a | from_json | ... style=""' sample.yml
Copied!
will output
1
cool: thing
Copied!

Encode value as props string

Given a sample.yml file of:
1
a:
2
cool: thing
Copied!
then
1
yq '.b = (.a | @props)' sample.yml
Copied!
will output
1
a:
2
cool: thing
3
b: |
4
cool = thing
Copied!

Decode props encoded string

Given a sample.yml file of:
1
a: |-
2
cats=great
3
dogs=cool as well
Copied!
then
1
yq '.a |= from_props' sample.yml
Copied!
will output
1
a:
2
cats: great
3
dogs: cool as well
Copied!

Encode value as yaml string

Indent defaults to 2
Given a sample.yml file of:
1
a:
2
cool:
3
bob: dylan
Copied!
then
1
yq '.b = (.a | to_yaml)' sample.yml
Copied!
will output
1
a:
2
cool:
3
bob: dylan
4
b: |
5
cool:
6
bob: dylan
Copied!

Encode value as yaml string, with custom indentation

You can specify the indentation level as the first parameter.
Given a sample.yml file of:
1
a:
2
cool:
3
bob: dylan
Copied!
then
1
yq '.b = (.a | to_yaml(8))' sample.yml
Copied!
will output
1
a:
2
cool:
3
bob: dylan
4
b: |
5
cool:
6
bob: dylan
Copied!

Decode a yaml encoded string

Given a sample.yml file of:
1
a: 'foo: bar'
Copied!
then
1
yq '.b = (.a | from_yaml)' sample.yml
Copied!
will output
1
a: 'foo: bar'
2
b:
3
foo: bar
Copied!

Update a multiline encoded yaml string

Given a sample.yml file of:
1
a: |
2
foo: bar
3
baz: dog
Copied!
then
1
yq '.a |= (from_yaml | .foo = "cat" | to_yaml)' sample.yml
Copied!
will output
1
a: |
2
foo: cat
3
baz: dog
Copied!

Update a single line encoded yaml string

Given a sample.yml file of:
1
a: 'foo: bar'
Copied!
then
1
yq '.a |= (from_yaml | .foo = "cat" | to_yaml)' sample.yml
Copied!
will output
1
a: 'foo: cat'
Copied!

Encode array of scalars as csv string

Scalars are strings, numbers and booleans.
Given a sample.yml file of:
1
- cat
2
- thing1,thing2
3
- true
4
- 3.40
Copied!
then
1
yq '@csv' sample.yml
Copied!
will output
1
cat,"thing1,thing2",true,3.40
Copied!

Encode array of arrays as csv string

Given a sample.yml file of:
1
- - cat
2
- thing1,thing2
3
- true
4
- 3.40
5
- - dog
6
- thing3
7
- false
8
- 12
Copied!
then
1
yq '@csv' sample.yml
Copied!
will output
1
cat,"thing1,thing2",true,3.40
2
dog,thing3,false,12
Copied!

Encode array of array scalars as tsv string

Scalars are strings, numbers and booleans.
Given a sample.yml file of:
1
- - cat
2
- thing1,thing2
3
- true
4
- 3.40
5
- - dog
6
- thing3
7
- false
8
- 12
Copied!
then
1
yq '@tsv' sample.yml
Copied!
will output
1
cat thing1,thing2 true 3.40
2
dog thing3 false 12
Copied!

Encode value as xml string

Given a sample.yml file of:
1
a:
2
cool:
3
foo: bar
4
+id: hi
Copied!
then
1
yq '.a | to_xml' sample.yml
Copied!
will output
1
<cool id="hi">
2
<foo>bar</foo>
3
</cool>
Copied!

Encode value as xml string on a single line

Given a sample.yml file of:
1
a:
2
cool:
3
foo: bar
4
+id: hi
Copied!
then
1
yq '.a | @xml' sample.yml
Copied!
will output
1
<cool id="hi"><foo>bar</foo></cool>
Copied!

Encode value as xml string with custom indentation

Given a sample.yml file of:
1
a:
2
cool:
3
foo: bar
4
+id: hi
Copied!
then
1
yq '{"cat": .a | to_xml(1)}' sample.yml
Copied!
will output
1
cat: |
2
<cool id="hi">
3
<foo>bar</foo>
4
</cool>
Copied!

Decode a xml encoded string

Given a sample.yml file of:
1
a: <foo>bar</foo>
Copied!
then
1
yq '.b = (.a | from_xml)' sample.yml
Copied!
will output
1
a: <foo>bar</foo>
2
b:
3
foo: bar
Copied!

Encode a string to base64

Given a sample.yml file of:
1
coolData: a special string
Copied!
then
1
yq '.coolData | @base64' sample.yml
Copied!
will output
1
YSBzcGVjaWFsIHN0cmluZw==
Copied!

Encode a yaml document to base64

Pipe through @yaml first to convert to a string, then use @base64 to encode it.
Given a sample.yml file of:
1
a: apple
Copied!
then
1
yq '@yaml | @base64' sample.yml
Copied!
will output
1
YTogYXBwbGUK
Copied!

Decode a base64 encoded string

Decoded data is assumed to be a string.
Given a sample.yml file of:
1
coolData: V29ya3Mgd2l0aCBVVEYtMTYg8J+Yig==
Copied!
then
1
yq '.coolData | @base64d' sample.yml
Copied!
will output
1
Works with UTF-16 😊
Copied!

Decode a base64 encoded yaml document

Pipe through from_yaml to parse the decoded base64 string as a yaml document.
Given a sample.yml file of:
1
coolData: YTogYXBwbGUK
Copied!
then
1
yq '.coolData |= (@base64d | from_yaml)' sample.yml
Copied!
will output
1
coolData:
2
a: apple
Copied!