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/@yamld | to_yaml(i)/@yaml |
JSON | from_json/@jsond | to_json(i)/@json |
Properties | from_props/@propsd | to_props/@props |
CSV | from_csv/@csvd | to_csv/@csv |
TSV | from_tsv/@tsvd | to_tsv/@tsv |
XML | from_xml/@xmld | to_xml(i)/@xml |
Base64 | @base64d | @base64 |
URI | @urid | @uri |
Shell | | @sh |
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 utf-8 string and not binary content.
Given a sample.yml file of:
a:
cool: thing
then
yq '.b = (.a | to_json)' sample.yml
will output
a:
cool: thing
b: |
{
"cool": "thing"
}
Pass in a 0 indent to print json on a single line.
Given a sample.yml file of:
a:
cool: thing
then
yq '.b = (.a | to_json(0))' sample.yml
will output
a:
cool: thing
b: '{"cool":"thing"}'
Pass in a 0 indent to print json on a single line.
Given a sample.yml file of:
a:
cool: thing
then
yq '.b = (.a | @json)' sample.yml
will output
a:
cool: thing
b: '{"cool":"thing"}'
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:
a: '{"cool":"thing"}'
then
yq '.a | from_json | ... style=""' sample.yml
will output
cool: thing
Given a sample.yml file of:
a:
cool: thing
then
yq '.b = (.a | @props)' sample.yml
will output
a:
cool: thing
b: |
cool = thing
Given a sample.yml file of:
a: |-
cats=great
dogs=cool as well
then
yq '.a |= @propsd' sample.yml
will output
a:
cats: great
dogs: cool as well
Given a sample.yml file of:
a: |-
cats,dogs
great,cool as well
then
yq '.a |= @csvd' sample.yml
will output
a:
- cats: great
dogs: cool as well
Given a sample.yml file of:
a: |-
cats dogs
great cool as well
then
yq '.a |= @tsvd' sample.yml
will output
a:
- cats: great
dogs: cool as well
Indent defaults to 2
Given a sample.yml file of:
a:
cool:
bob: dylan
then
yq '.b = (.a | to_yaml)' sample.yml
will output
a:
cool:
bob: dylan
b: |
cool:
bob: dylan
You can specify the indentation level as the first parameter.
Given a sample.yml file of:
a:
cool:
bob: dylan
then
yq '.b = (.a | to_yaml(8))' sample.yml
will output
a:
cool:
bob: dylan
b: |
cool:
bob: dylan
Given a sample.yml file of:
a: 'foo: bar'
then
yq '.b = (.a | from_yaml)' sample.yml
will output
a: 'foo: bar'
b:
foo: bar
Given a sample.yml file of:
a: |
foo: bar
baz: dog
then
yq '.a |= (from_yaml | .foo = "cat" | to_yaml)' sample.yml
will output
a: |
foo: cat
baz: dog
Given a sample.yml file of:
a: 'foo: bar'
then
yq '.a |= (from_yaml | .foo = "cat" | to_yaml)' sample.yml
will output
a: 'foo: cat'
Scalars are strings, numbers and booleans.
Given a sample.yml file of:
- cat
- thing1,thing2
- true
- 3.40
then
yq '@csv' sample.yml
will output
cat,"thing1,thing2",true,3.40
Given a sample.yml file of:
- - cat
- thing1,thing2
- true
- 3.40
- - dog
- thing3
- false
- 12
then
yq '@csv' sample.yml
will output
cat,"thing1,thing2",true,3.40
dog,thing3,false,12
Scalars are strings, numbers and booleans.
Given a sample.yml file of:
- - cat
- thing1,thing2
- true
- 3.40
- - dog
- thing3
- false
- 12
then
yq '@tsv' sample.yml
will output
cat thing1,thing2 true 3.40
dog thing3 false 12
Given a sample.yml file of:
then
yq '.a | to_xml' sample.yml
will output
<cool id="hi">
<foo>bar</foo>
</cool>
Given a sample.yml file of:
then
yq '.a | @xml' sample.yml
will output
<cool id="hi"><foo>bar</foo></cool>
Given a sample.yml file of:
then
yq '{"cat": .a | to_xml(1)}' sample.yml
will output
cat: |
<cool id="hi">
<foo>bar</foo>
</cool>
Given a sample.yml file of:
a: <foo>bar</foo>
then
yq '.b = (.a | from_xml)' sample.yml
will output
a: <foo>bar</foo>
b:
foo: bar
Given a sample.yml file of:
coolData: a special string
then
yq '.coolData | @base64' sample.yml
will output
YSBzcGVjaWFsIHN0cmluZw==
Pipe through @yaml first to convert to a string, then use @base64 to encode it.
Given a sample.yml file of:
a: apple
then
yq '@yaml | @base64' sample.yml
will output
YTogYXBwbGUK
Given a sample.yml file of:
coolData: this has & special () characters *
then
yq '.coolData | @uri' sample.yml
will output
this+has+%26+special+%28%29+characters+%2A
Given a sample.yml file of:
this+has+%26+special+%28%29+characters+%2A
then
yq '@urid' sample.yml
will output
this has & special () characters *
Sh/Bash friendly string
Given a sample.yml file of:
coolData: strings with spaces and a 'quote'
then
yq '.coolData | @sh' sample.yml
will output
strings' with spaces and a '\'quote\'
Decoded data is assumed to be a string.
Given a sample.yml file of:
coolData: V29ya3Mgd2l0aCBVVEYtMTYg8J+Yig==
then
yq '.coolData | @base64d' sample.yml
will output
Works with UTF-16 😊
Pipe through
from_yaml
to parse the decoded base64 string as a yaml document.Given a sample.yml file of:
coolData: YTogYXBwbGUK
then
yq '.coolData |= (@base64d | from_yaml)' sample.yml
will output
coolData:
a: apple
Last modified 5d ago