2020-01-13 05:58:11 +00:00
|
|
|
## Yaml to Json
|
|
|
|
To convert output to json, use the --tojson (or -j) flag. This is supported by all commands.
|
|
|
|
|
|
|
|
Each matching yaml node will be converted to json and printed out on a separate line.
|
2017-04-13 05:36:59 +00:00
|
|
|
|
2017-04-19 05:45:45 +00:00
|
|
|
Given a sample.yaml file of:
|
|
|
|
```yaml
|
|
|
|
b:
|
|
|
|
c: 2
|
|
|
|
```
|
|
|
|
then
|
|
|
|
```bash
|
2020-01-13 05:58:11 +00:00
|
|
|
yq r -j sample.yaml
|
2017-04-19 05:45:45 +00:00
|
|
|
```
|
|
|
|
|
|
|
|
will output
|
|
|
|
```json
|
|
|
|
{"b":{"c":2}}
|
|
|
|
```
|
|
|
|
|
2020-01-13 05:58:11 +00:00
|
|
|
Given a sample.yaml file of:
|
|
|
|
```yaml
|
|
|
|
bob:
|
|
|
|
c: 2
|
|
|
|
bab:
|
|
|
|
c: 5
|
|
|
|
```
|
|
|
|
then
|
|
|
|
```bash
|
|
|
|
yq r -j sample.yaml b*
|
|
|
|
```
|
|
|
|
|
|
|
|
will output
|
|
|
|
```json
|
|
|
|
{"c":2}
|
|
|
|
{"c":5}
|
|
|
|
```
|
|
|
|
|
|
|
|
## Json to Yaml
|
2017-04-19 05:45:45 +00:00
|
|
|
To read in json, just pass in a json file instead of yaml, it will just work :)
|
|
|
|
|
|
|
|
e.g given a json file
|
|
|
|
|
|
|
|
```json
|
|
|
|
{"a":"Easy! as one two three","b":{"c":2,"d":[3,4]}}
|
|
|
|
```
|
|
|
|
then
|
|
|
|
```bash
|
2017-12-17 22:11:08 +00:00
|
|
|
yq r sample.json
|
2017-04-19 05:45:45 +00:00
|
|
|
```
|
|
|
|
will output
|
|
|
|
```yaml
|
|
|
|
a: Easy! as one two three
|
|
|
|
b:
|
|
|
|
c: 2
|
|
|
|
d:
|
|
|
|
- 3
|
|
|
|
- 4
|
|
|
|
```
|
|
|
|
|