yq/pkg/yqlib/doc/operators/encode-decode.md

517 lines
7.7 KiB
Markdown
Raw Normal View History

2021-11-03 02:54:09 +00:00
# Encoder / Decoder
2021-10-22 01:37:47 +00:00
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.
2021-10-26 04:07:50 +00:00
Note that you can optionally pass an indent value to the encode functions (see below).
2022-05-24 08:18:27 +00:00
These operators are useful to process yaml documents that have stringified embedded yaml/json/props in them.
2021-11-03 02:54:09 +00:00
2021-10-24 00:35:40 +00:00
2021-12-02 01:11:15 +00:00
| Format | Decode (from string) | Encode (to string) |
| --- | -- | --|
2022-08-01 04:12:35 +00:00
| 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 |
2022-08-01 04:12:35 +00:00
| XML | from_xml/@xmld | to_xml(i)/@xml |
2022-02-22 22:26:35 +00:00
| Base64 | @base64d | @base64 |
2023-01-23 00:37:18 +00:00
| URI | @urid | @uri |
2023-02-02 01:22:52 +00:00
| Shell | | @sh |
2021-12-02 01:11:15 +00:00
See CSV and TSV [documentation](https://mikefarah.gitbook.io/yq/usage/csv-tsv) for accepted formats.
2021-12-02 01:11:15 +00:00
2021-12-21 05:52:54 +00:00
XML uses the `--xml-attribute-prefix` and `xml-content-name` flags to identify attributes and content fields.
2021-12-02 01:11:15 +00:00
Base64 assumes [rfc4648](https://rfc-editor.org/rfc/rfc4648.html) encoding. Encoding and decoding both assume that the content is a utf-8 string and not binary content.
2022-02-22 22:26:35 +00:00
2021-12-02 01:11:15 +00:00
## Encode value as json string
2021-10-22 01:37:47 +00:00
Given a sample.yml file of:
```yaml
a:
2021-12-02 01:11:15 +00:00
cool: thing
2021-10-22 01:37:47 +00:00
```
then
```bash
2022-01-27 06:21:10 +00:00
yq '.b = (.a | to_json)' sample.yml
2021-10-22 01:37:47 +00:00
```
will output
```yaml
a:
2021-12-02 01:11:15 +00:00
cool: thing
2021-10-22 01:37:47 +00:00
b: |
2021-12-02 01:11:15 +00:00
{
"cool": "thing"
}
2021-10-24 00:35:40 +00:00
```
2021-12-02 01:11:15 +00:00
## Encode value as json string, on one line
Pass in a 0 indent to print json on a single line.
2021-10-24 00:35:40 +00:00
Given a sample.yml file of:
```yaml
a:
2021-12-02 01:11:15 +00:00
cool: thing
2021-10-24 00:35:40 +00:00
```
then
```bash
2022-01-27 06:21:10 +00:00
yq '.b = (.a | to_json(0))' sample.yml
2021-10-24 00:35:40 +00:00
```
will output
```yaml
a:
2021-12-02 01:11:15 +00:00
cool: thing
b: '{"cool":"thing"}'
2021-10-22 01:37:47 +00:00
```
2021-12-02 01:11:15 +00:00
## Encode value as json string, on one line shorthand
Pass in a 0 indent to print json on a single line.
2021-10-22 01:37:47 +00:00
Given a sample.yml file of:
```yaml
a:
cool: thing
```
then
```bash
2022-01-27 06:21:10 +00:00
yq '.b = (.a | @json)' sample.yml
2021-10-22 01:37:47 +00:00
```
will output
```yaml
a:
cool: thing
2021-12-02 01:11:15 +00:00
b: '{"cool":"thing"}'
2021-10-22 01:37:47 +00:00
```
2021-12-02 01:11:15 +00:00
## 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:
```yaml
a: '{"cool":"thing"}'
```
then
```bash
2022-01-27 06:21:10 +00:00
yq '.a | from_json | ... style=""' sample.yml
2021-12-02 01:11:15 +00:00
```
will output
```yaml
cool: thing
```
## Encode value as props string
2021-10-22 01:37:47 +00:00
Given a sample.yml file of:
```yaml
a:
cool: thing
```
then
```bash
2022-01-27 06:21:10 +00:00
yq '.b = (.a | @props)' sample.yml
2021-10-22 01:37:47 +00:00
```
will output
```yaml
a:
cool: thing
b: |
2021-12-02 01:11:15 +00:00
cool = thing
2021-10-22 01:37:47 +00:00
```
2022-03-28 08:48:30 +00:00
## Decode props encoded string
Given a sample.yml file of:
```yaml
a: |-
cats=great
dogs=cool as well
```
then
```bash
yq '.a |= @propsd' sample.yml
2022-03-28 08:48:30 +00:00
```
will output
```yaml
a:
cats: great
dogs: cool as well
```
## Decode csv encoded string
Given a sample.yml file of:
```yaml
a: |-
cats,dogs
great,cool as well
```
then
```bash
yq '.a |= @csvd' sample.yml
```
will output
```yaml
a:
- cats: great
dogs: cool as well
```
## Decode tsv encoded string
Given a sample.yml file of:
```yaml
a: |-
cats dogs
great cool as well
```
then
```bash
yq '.a |= @tsvd' sample.yml
```
will output
```yaml
a:
- cats: great
dogs: cool as well
```
2021-12-02 01:11:15 +00:00
## Encode value as yaml string
Indent defaults to 2
2021-10-24 00:35:40 +00:00
Given a sample.yml file of:
```yaml
a:
2021-12-02 01:11:15 +00:00
cool:
bob: dylan
2021-10-24 00:35:40 +00:00
```
then
```bash
2022-01-27 06:21:10 +00:00
yq '.b = (.a | to_yaml)' sample.yml
2021-10-24 00:35:40 +00:00
```
will output
```yaml
a:
2021-12-02 01:11:15 +00:00
cool:
bob: dylan
b: |
cool:
bob: dylan
2021-10-24 00:35:40 +00:00
```
2021-12-02 01:11:15 +00:00
## Encode value as yaml string, with custom indentation
You can specify the indentation level as the first parameter.
Given a sample.yml file of:
```yaml
a:
cool:
bob: dylan
```
then
```bash
2022-01-27 06:21:10 +00:00
yq '.b = (.a | to_yaml(8))' sample.yml
2021-12-02 01:11:15 +00:00
```
will output
```yaml
a:
cool:
bob: dylan
b: |
cool:
bob: dylan
```
2021-10-22 01:37:47 +00:00
## Decode a yaml encoded string
Given a sample.yml file of:
```yaml
a: 'foo: bar'
```
then
```bash
2022-01-27 06:21:10 +00:00
yq '.b = (.a | from_yaml)' sample.yml
2021-10-22 01:37:47 +00:00
```
will output
```yaml
a: 'foo: bar'
b:
foo: bar
```
## Update a multiline encoded yaml string
2021-10-22 01:37:47 +00:00
Given a sample.yml file of:
```yaml
a: |
foo: bar
baz: dog
2021-10-22 01:37:47 +00:00
```
then
```bash
2022-01-27 06:21:10 +00:00
yq '.a |= (from_yaml | .foo = "cat" | to_yaml)' sample.yml
2021-10-22 01:37:47 +00:00
```
will output
```yaml
a: |
foo: cat
baz: dog
```
## Update a single line encoded yaml string
Given a sample.yml file of:
```yaml
a: 'foo: bar'
```
then
```bash
2022-01-27 06:21:10 +00:00
yq '.a |= (from_yaml | .foo = "cat" | to_yaml)' sample.yml
```
will output
```yaml
a: 'foo: cat'
2021-10-22 01:37:47 +00:00
```
2021-12-02 01:11:15 +00:00
## Encode array of scalars as csv string
Scalars are strings, numbers and booleans.
Given a sample.yml file of:
```yaml
- cat
- thing1,thing2
- true
- 3.40
```
then
```bash
2022-01-27 06:21:10 +00:00
yq '@csv' sample.yml
2021-12-02 01:11:15 +00:00
```
will output
```yaml
cat,"thing1,thing2",true,3.40
```
## Encode array of arrays as csv string
Given a sample.yml file of:
```yaml
- - cat
- thing1,thing2
- true
- 3.40
- - dog
- thing3
- false
- 12
```
then
```bash
2022-01-27 06:21:10 +00:00
yq '@csv' sample.yml
2021-12-02 01:11:15 +00:00
```
will output
```yaml
cat,"thing1,thing2",true,3.40
dog,thing3,false,12
```
## Encode array of arrays as tsv string
2021-12-02 01:11:15 +00:00
Scalars are strings, numbers and booleans.
Given a sample.yml file of:
```yaml
- - cat
- thing1,thing2
- true
- 3.40
- - dog
- thing3
- false
- 12
```
then
```bash
2022-01-27 06:21:10 +00:00
yq '@tsv' sample.yml
2021-12-02 01:11:15 +00:00
```
will output
```yaml
cat thing1,thing2 true 3.40
dog thing3 false 12
```
2021-12-21 05:52:54 +00:00
## Encode value as xml string
Given a sample.yml file of:
```yaml
a:
cool:
foo: bar
2022-11-10 11:22:55 +00:00
+@id: hi
2021-12-21 05:52:54 +00:00
```
then
```bash
2022-01-27 06:21:10 +00:00
yq '.a | to_xml' sample.yml
2021-12-21 05:52:54 +00:00
```
will output
```yaml
<cool id="hi">
<foo>bar</foo>
</cool>
2021-12-21 05:59:09 +00:00
2021-12-21 05:52:54 +00:00
```
## Encode value as xml string on a single line
Given a sample.yml file of:
```yaml
a:
cool:
foo: bar
2022-11-10 11:22:55 +00:00
+@id: hi
2021-12-21 05:52:54 +00:00
```
then
```bash
2022-01-27 06:21:10 +00:00
yq '.a | @xml' sample.yml
2021-12-21 05:52:54 +00:00
```
will output
```yaml
<cool id="hi"><foo>bar</foo></cool>
2021-12-21 05:59:09 +00:00
2021-12-21 05:52:54 +00:00
```
## Encode value as xml string with custom indentation
Given a sample.yml file of:
```yaml
a:
cool:
foo: bar
2022-11-10 11:22:55 +00:00
+@id: hi
2021-12-21 05:52:54 +00:00
```
then
```bash
2022-01-27 06:21:10 +00:00
yq '{"cat": .a | to_xml(1)}' sample.yml
2021-12-21 05:52:54 +00:00
```
will output
```yaml
2021-12-21 05:59:09 +00:00
cat: |
<cool id="hi">
<foo>bar</foo>
</cool>
2021-12-21 05:52:54 +00:00
```
2021-12-21 04:02:07 +00:00
## Decode a xml encoded string
Given a sample.yml file of:
```yaml
a: <foo>bar</foo>
```
then
```bash
2022-01-27 06:21:10 +00:00
yq '.b = (.a | from_xml)' sample.yml
2021-12-21 04:02:07 +00:00
```
will output
```yaml
a: <foo>bar</foo>
b:
foo: bar
```
2022-02-22 22:26:35 +00:00
## Encode a string to base64
Given a sample.yml file of:
```yaml
coolData: a special string
```
then
```bash
yq '.coolData | @base64' sample.yml
```
will output
```yaml
YSBzcGVjaWFsIHN0cmluZw==
```
## 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:
```yaml
a: apple
```
then
```bash
yq '@yaml | @base64' sample.yml
```
will output
```yaml
YTogYXBwbGUK
```
2023-01-23 00:37:18 +00:00
## Encode a string to uri
Given a sample.yml file of:
```yaml
coolData: this has & special () characters *
```
then
```bash
yq '.coolData | @uri' sample.yml
```
will output
```yaml
this+has+%26+special+%28%29+characters+%2A
```
## Decode a URI to a string
Given a sample.yml file of:
```yaml
this+has+%26+special+%28%29+characters+%2A
```
then
```bash
yq '@urid' sample.yml
```
will output
```yaml
this has & special () characters *
```
2023-02-02 01:22:52 +00:00
## Encode a string to sh
Sh/Bash friendly string
Given a sample.yml file of:
```yaml
coolData: strings with spaces and a 'quote'
```
then
```bash
yq '.coolData | @sh' sample.yml
```
will output
```yaml
strings' with spaces and a '\'quote\'
2023-02-02 01:22:52 +00:00
```
2022-02-22 22:26:35 +00:00
## Decode a base64 encoded string
Decoded data is assumed to be a string.
Given a sample.yml file of:
```yaml
coolData: V29ya3Mgd2l0aCBVVEYtMTYg8J+Yig==
```
then
```bash
yq '.coolData | @base64d' sample.yml
```
will output
```yaml
Works with UTF-16 😊
```
## 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:
```yaml
coolData: YTogYXBwbGUK
```
then
```bash
yq '.coolData |= (@base64d | from_yaml)' sample.yml
```
will output
```yaml
coolData:
a: apple
```