mirror of
https://github.com/mikefarah/yq.git
synced 2024-12-19 20:19:04 +00:00
1.4 KiB
1.4 KiB
Working with JSON
Yaml to Json
To convert output to json, use the --output-format=json
(or -o=j
) flag. You can change the json output format by using the indent flag.
Given a sample.yaml file of:
b:
c: 2
then
yq eval -o=j sample.yaml
will output
{
"b": {
"c": 2
}
}
To format the json:
yq eval -o=j -I=0 sample.yaml
will yield
{"b":{"c":2}}
Multiple matches
Each matching yaml node will be converted to json and printed out as a separate json doc. You may want to set the indent flags to 0 if you want a json doc per line.
Given a sample.yaml file of:
bob:
c: 2
bab:
c: 5
then
yq eval -o=j '.b*' sample.yaml
will output
{
"c": 2
}
{
"c": 5
}
Json to Yaml
To read in json, just pass in a json file instead of yaml, it will just work - as json is a subset of yaml. However, you will probably want to use the Style Operator or --prettyPrint/-P
flag to make look more like an idiomatic yaml document. This can be done by resetting the style of all elements.
e.g given a json file
{"a":"Easy! as one two three","b":{"c":2,"d":[3,4]}}
then
yq eval -P sample.json
will output
a: Easy! as one two three
b:
c: 2
d:
- 3
- 4