mirror of
https://github.com/mikefarah/yq.git
synced 2024-11-14 23:28:06 +00:00
111 lines
1.5 KiB
Markdown
111 lines
1.5 KiB
Markdown
|
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.
|
||
|
|
||
|
## Encode value as yaml string
|
||
|
Given a sample.yml file of:
|
||
|
```yaml
|
||
|
a:
|
||
|
cool: thing
|
||
|
```
|
||
|
then
|
||
|
```bash
|
||
|
yq eval '.b = (.a | to_yaml)' sample.yml
|
||
|
```
|
||
|
will output
|
||
|
```yaml
|
||
|
a:
|
||
|
cool: thing
|
||
|
b: |
|
||
|
cool: thing
|
||
|
```
|
||
|
|
||
|
## Encode value as yaml string, using toyaml
|
||
|
Does the same thing as to_yaml, matching jq naming convention.
|
||
|
|
||
|
Given a sample.yml file of:
|
||
|
```yaml
|
||
|
a:
|
||
|
cool: thing
|
||
|
```
|
||
|
then
|
||
|
```bash
|
||
|
yq eval '.b = (.a | to_yaml)' sample.yml
|
||
|
```
|
||
|
will output
|
||
|
```yaml
|
||
|
a:
|
||
|
cool: thing
|
||
|
b: |
|
||
|
cool: thing
|
||
|
```
|
||
|
|
||
|
## Encode value as json string
|
||
|
Given a sample.yml file of:
|
||
|
```yaml
|
||
|
a:
|
||
|
cool: thing
|
||
|
```
|
||
|
then
|
||
|
```bash
|
||
|
yq eval '.b = (.a | to_json)' sample.yml
|
||
|
```
|
||
|
will output
|
||
|
```yaml
|
||
|
a:
|
||
|
cool: thing
|
||
|
b: |
|
||
|
{
|
||
|
"cool": "thing"
|
||
|
}
|
||
|
```
|
||
|
|
||
|
## Encode value as props string
|
||
|
Given a sample.yml file of:
|
||
|
```yaml
|
||
|
a:
|
||
|
cool: thing
|
||
|
```
|
||
|
then
|
||
|
```bash
|
||
|
yq eval '.b = (.a | to_props)' sample.yml
|
||
|
```
|
||
|
will output
|
||
|
```yaml
|
||
|
a:
|
||
|
cool: thing
|
||
|
b: |
|
||
|
cool = thing
|
||
|
```
|
||
|
|
||
|
## Decode a yaml encoded string
|
||
|
Given a sample.yml file of:
|
||
|
```yaml
|
||
|
a: 'foo: bar'
|
||
|
```
|
||
|
then
|
||
|
```bash
|
||
|
yq eval '.b = (.a | from_yaml)' sample.yml
|
||
|
```
|
||
|
will output
|
||
|
```yaml
|
||
|
a: 'foo: bar'
|
||
|
b:
|
||
|
foo: bar
|
||
|
```
|
||
|
|
||
|
## Update an encoded yaml string
|
||
|
Given a sample.yml file of:
|
||
|
```yaml
|
||
|
a: |
|
||
|
foo: bar
|
||
|
```
|
||
|
then
|
||
|
```bash
|
||
|
yq eval '.a |= (from_yaml | .foo = "cat" | to_yaml)' sample.yml
|
||
|
```
|
||
|
will output
|
||
|
```yaml
|
||
|
a: |
|
||
|
foo: cat
|
||
|
```
|
||
|
|