You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: pkg/yqlib/doc/operators/encode-decode.md
+66Lines changed: 66 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -15,13 +15,16 @@ These operators are useful to process yaml documents that have stringified embed
15
15
| CSV || to_csv/@csv|
16
16
| TSV || to_tsv/@tsv|
17
17
| XML | from_xml | to_xml(i)/@xml|
18
+
| Base64 |@base64d|@base64|
18
19
19
20
20
21
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).
21
22
22
23
XML uses the `--xml-attribute-prefix` and `xml-content-name` flags to identify attributes and content fields.
23
24
24
25
26
+
Base64 assumes [rfc4648](https://rfc-editor.org/rfc/rfc4648.html) encoding. Encoding and decoding both assume that the content is a string.
27
+
25
28
{% hint style="warning" %}
26
29
Note that versions prior to 4.18 require the 'eval/e' command to be specified. 
27
30
@@ -354,3 +357,66 @@ b:
354
357
foo: bar
355
358
```
356
359
360
+
## Encode a string to base64
361
+
Given a sample.yml file of:
362
+
```yaml
363
+
coolData: a special string
364
+
```
365
+
then
366
+
```bash
367
+
yq '.coolData | @base64' sample.yml
368
+
```
369
+
will output
370
+
```yaml
371
+
YSBzcGVjaWFsIHN0cmluZw==
372
+
```
373
+
374
+
## Encode a yaml document to base64
375
+
Pipe through @yaml first to convert to a string, then use @base64 to encode it.
376
+
377
+
Given a sample.yml file of:
378
+
```yaml
379
+
a: apple
380
+
```
381
+
then
382
+
```bash
383
+
yq '@yaml | @base64' sample.yml
384
+
```
385
+
will output
386
+
```yaml
387
+
YTogYXBwbGUK
388
+
```
389
+
390
+
## Decode a base64 encoded string
391
+
Decoded data is assumed to be a string.
392
+
393
+
Given a sample.yml file of:
394
+
```yaml
395
+
coolData: V29ya3Mgd2l0aCBVVEYtMTYg8J+Yig==
396
+
```
397
+
then
398
+
```bash
399
+
yq '.coolData | @base64d' sample.yml
400
+
```
401
+
will output
402
+
```yaml
403
+
Works with UTF-16 😊
404
+
```
405
+
406
+
## Decode a base64 encoded yaml document
407
+
Pipe through `from_yaml` to parse the decoded base64 string as a yaml document.
Copy file name to clipboardExpand all lines: pkg/yqlib/doc/operators/headers/encode-decode.md
+3Lines changed: 3 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -15,9 +15,12 @@ These operators are useful to process yaml documents that have stringified embed
15
15
| CSV || to_csv/@csv|
16
16
| TSV || to_tsv/@tsv|
17
17
| XML | from_xml | to_xml(i)/@xml|
18
+
| Base64 |@base64d|@base64|
18
19
19
20
20
21
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).
21
22
22
23
XML uses the `--xml-attribute-prefix` and `xml-content-name` flags to identify attributes and content fields.
23
24
25
+
26
+
Base64 assumes [rfc4648](https://rfc-editor.org/rfc/rfc4648.html) encoding. Encoding and decoding both assume that the content is a string.
returnfmt.Errorf("cannot encode %v as base64, can only operate on strings. Please first pipe through another encoding operator to convert the value to a string", node.Tag)
0 commit comments