yq/usage/convert.md

133 lines
2.1 KiB
Markdown
Raw Normal View History

2022-01-22 00:49:12 +00:00
# JSON
2021-10-30 03:14:39 +00:00
2022-01-22 00:49:12 +00:00
Encode and decode to and from JSON. Note that YAML is a _superset_ of JSON - so `yq` can read any json file without doing anything special.
2021-10-30 03:14:39 +00:00
2022-01-22 00:49:12 +00:00
This means you don't need to 'convert' a JSON file to YAML - however if you want idiomatic YAML styling, then you can use the `-P/--prettyPrint` flag, see examples below.
2021-10-30 03:14:39 +00:00
2022-02-06 03:41:27 +00:00
{% hint style="warning" %}
Note that versions prior to 4.18 require the 'eval/e' command to be specified. 
`yq e <exp> <file>`
{% endhint %}
2022-01-22 00:49:12 +00:00
## Parse json: simple
JSON is a subset of yaml, so all you need to do is prettify the output
2021-10-30 03:14:39 +00:00
2022-01-22 00:49:12 +00:00
Given a sample.json file of:
```json
{"cat": "meow"}
```
then
```bash
2022-01-28 01:45:43 +00:00
yq -P '.' sample.json
2022-01-22 00:49:12 +00:00
```
will output
```yaml
cat: meow
```
## Parse json: complex
JSON is a subset of yaml, so all you need to do is prettify the output
Given a sample.json file of:
```json
{"a":"Easy! as one two three","b":{"c":2,"d":[3,4]}}
```
then
```bash
2022-01-28 01:45:43 +00:00
yq -P '.' sample.json
2022-01-22 00:49:12 +00:00
```
will output
2021-10-30 03:14:39 +00:00
```yaml
2022-01-22 00:49:12 +00:00
a: Easy! as one two three
2021-10-30 03:14:39 +00:00
b:
c: 2
2022-01-22 00:49:12 +00:00
d:
- 3
- 4
2021-10-30 03:14:39 +00:00
```
2022-01-22 00:49:12 +00:00
## Encode json: simple
Given a sample.yml file of:
```yaml
cat: meow
```
2021-10-30 03:14:39 +00:00
then
```bash
2022-01-28 01:45:43 +00:00
yq -o=json '.' sample.yml
2021-10-30 03:14:39 +00:00
```
will output
2022-01-22 00:49:12 +00:00
```json
2021-10-30 03:14:39 +00:00
{
2022-01-22 00:49:12 +00:00
"cat": "meow"
2021-10-30 03:14:39 +00:00
}
```
2022-01-22 00:49:12 +00:00
## Encode json: simple - in one line
Given a sample.yml file of:
2021-10-30 03:14:39 +00:00
```yaml
2022-01-22 00:49:12 +00:00
cat: meow # this is a comment, and it will be dropped.
```
then
```bash
2022-01-28 01:45:43 +00:00
yq -o=json -I=0 '.' sample.yml
2022-01-22 00:49:12 +00:00
```
will output
```json
{"cat":"meow"}
2021-10-30 03:14:39 +00:00
```
2022-01-22 00:49:12 +00:00
## Encode json: comments
Given a sample.yml file of:
2021-10-30 03:14:39 +00:00
```yaml
2022-01-22 00:49:12 +00:00
cat: meow # this is a comment, and it will be dropped.
```
then
```bash
2022-01-28 01:45:43 +00:00
yq -o=json '.' sample.yml
2022-01-22 00:49:12 +00:00
```
will output
```json
{
"cat": "meow"
}
2021-10-30 03:14:39 +00:00
```
2022-01-22 00:49:12 +00:00
## Encode json: anchors
Anchors are dereferenced
2021-10-30 03:14:39 +00:00
2022-01-22 00:49:12 +00:00
Given a sample.yml file of:
2021-10-30 03:14:39 +00:00
```yaml
2022-01-22 00:49:12 +00:00
cat: &ref meow
anotherCat: *ref
2021-10-30 03:14:39 +00:00
```
then
```bash
2022-01-28 01:45:43 +00:00
yq -o=json '.' sample.yml
2021-10-30 03:14:39 +00:00
```
will output
2022-01-22 00:49:12 +00:00
```json
2021-10-30 03:14:39 +00:00
{
2022-01-22 00:49:12 +00:00
"cat": "meow",
"anotherCat": "meow"
2021-10-30 03:14:39 +00:00
}
```
2022-01-22 00:49:12 +00:00
## Encode json: multiple results
Each matching node is converted into a json doc. This is best used with 0 indent (json document per line)
2021-10-30 03:14:39 +00:00
2022-01-22 00:49:12 +00:00
Given a sample.yml file of:
```yaml
things: [{stuff: cool}, {whatever: cat}]
2021-10-30 03:14:39 +00:00
```
then
```bash
2022-01-28 01:45:43 +00:00
yq -o=json -I=0 '.things[]' sample.yml
2021-10-30 03:14:39 +00:00
```
will output
2022-01-22 00:49:12 +00:00
```json
{"stuff":"cool"}
{"whatever":"cat"}
2021-10-30 03:14:39 +00:00
```
2022-01-22 00:49:12 +00:00