yq/usage/convert.md

252 lines
4.2 KiB
Markdown
Raw Normal View History

2022-01-22 00:49:12 +00:00
# JSON
2021-10-30 03:14:39 +00:00
2022-08-01 06:46:09 +00:00
Encode and decode to and from JSON. Supports multiple JSON documents in a single file (e.g. NDJSON).
Note that YAML is a superset of (single document) JSON - so you don't have to use the JSON parser to read JSON when there is only one JSON document in the input. You will probably want to pretty print the result in this case, to get idiomatic YAML styling.
2021-10-30 03:14:39 +00:00
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
2023-11-23 00:59:14 +00:00
yq -p=json 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
2023-11-23 00:59:14 +00:00
yq -p=json 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:
2023-11-23 00:59:14 +00:00
c: 2
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
## Roundtrip JSON Lines / NDJSON
2022-08-01 06:46:09 +00:00
Given a sample.json file of:
```json
{"this": "is a multidoc json file"}
{"each": ["line is a valid json document"]}
{"a number": 4}
```
then
```bash
yq -p=json -o=json -I=0 sample.json
```
will output
```yaml
{"this":"is a multidoc json file"}
{"each":["line is a valid json document"]}
{"a number":4}
```
## Roundtrip multi-document JSON
The parser can also handle multiple multi-line json documents in a single file (despite this not being in the JSON Lines / NDJSON spec). Typically you would have one entire JSON document per line, but the parser also supports multiple multi-line json documents
2022-08-01 06:46:09 +00:00
Given a sample.json file of:
```json
{
"this": "is a multidoc json file"
}
{
"it": [
"has",
"consecutive",
"json documents"
]
}
{
"a number": 4
}
```
then
```bash
yq -p=json -o=json -I=2 sample.json
```
will output
```yaml
{
"this": "is a multidoc json file"
}
{
"it": [
"has",
"consecutive",
"json documents"
]
}
{
"a number": 4
}
```
## Update a specific document in a multi-document json
Documents are indexed by the `documentIndex` or `di` operator.
Given a sample.json file of:
```json
{"this": "is a multidoc json file"}
{"each": ["line is a valid json document"]}
{"a number": 4}
```
then
```bash
yq -p=json -o=json -I=0 '(select(di == 1) | .each ) += "cool"' sample.json
```
will output
```yaml
{"this":"is a multidoc json file"}
{"each":["line is a valid json document","cool"]}
{"a number":4}
```
## Find and update a specific document in a multi-document json
Use expressions as you normally would.
Given a sample.json file of:
```json
{"this": "is a multidoc json file"}
{"each": ["line is a valid json document"]}
{"a number": 4}
```
then
```bash
yq -p=json -o=json -I=0 '(select(has("each")) | .each ) += "cool"' sample.json
```
will output
```yaml
{"this":"is a multidoc json file"}
{"each":["line is a valid json document","cool"]}
{"a number":4}
```
## Decode JSON Lines / NDJSON
2022-08-01 06:46:09 +00:00
Given a sample.json file of:
```json
{"this": "is a multidoc json file"}
{"each": ["line is a valid json document"]}
{"a number": 4}
```
then
```bash
yq -p=json sample.json
```
will output
```yaml
this: is a multidoc json file
---
each:
- line is a valid json document
---
a number: 4
```