yq
v3.x
v3.x
  • yq
  • Upgrading from V2
  • Commands
    • Read
    • Validate
    • Compare
    • Write
    • Create
    • Delete
    • Merge
    • Prefix
    • Shell Completion
  • Usage
    • Output format
    • Path Expressions
    • Value Parsing
    • Working with JSON
  • Github Page
Powered by GitBook
On this page
  • Yaml to Json
  • Multiple matches
  • Json to Yaml

Was this helpful?

  1. Usage

Working with JSON

PreviousValue Parsing

Last updated 4 years ago

Was this helpful?

Yaml to Json

To convert output to json, use the --tojson (or -j) flag. This is supported by all commands. You can change the json output format by using the or flags. Note that due to the implementation of the JSON marshaller in GO, object keys will be sorted on output ().

Given a sample.yaml file of:

b:
  c: 2

then

yq r -j sample.yaml

will output

{"b":{"c":2}}

To format the json:

yq r --prettyPrint -j sample.yaml

will yield

{
  "b": {
    "c": 2
  }
}

Multiple matches

Given a sample.yaml file of:

bob:
  c: 2
bab:
  c: 5

then

yq r -j sample.yaml b*

will output

{"c":2}
{"c":5}

Json to Yaml

e.g given a json file

{"a":"Easy! as one two three","b":{"c":2,"d":[3,4]}}

then

yq r --prettyPrint sample.json

will output

a: Easy! as one two three
b:
  c: 2
  d:
  - 3
  - 4

Each matching yaml node will be converted to json and printed out on a separate line. The and flags will still work too.

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 to look more like an idiomatic yaml document.

https://golang.org/pkg/encoding/json/#Marshal
pretty print
indent
prettyPrint
indent
pretty print the output