yq
v4.x
v4.x
  • yq
  • How It Works
  • Recipes
  • Upgrading from V3
  • Commands
    • Evaluate
    • Evaluate All
    • Shell Completion
  • Operators
    • Add
    • Alternative (Default value)
    • Anchor and Alias Operators
    • Array to Map
    • Assign (Update)
    • Boolean Operators
    • Collect into Array
    • Column
    • Comment Operators
    • Compare Operators
    • Contains
    • Create, Collect into Object
    • Date Time
    • Delete
    • Divide
    • Document Index
    • Encode / Decode
    • Entries
    • Env Variable Operators
    • Equals
    • Eval
    • File Operators
    • Filter Operator
    • Flatten
    • Group By
    • Has
    • Keys
    • Kind
    • Length
    • Line
    • Load
    • Min
    • Map
    • Max
    • Modulo
    • Multiply (Merge)
    • Omit
    • Parent
    • Path
    • Pick
    • Pipe
    • Pivot
    • Recursive Descent (Glob)
    • Reduce
    • Reverse
    • Select
    • Shuffle
    • Slice Array
    • Sort
    • Sort Keys
    • Split into Documents
    • String Operators
    • Style
    • Subtract
    • Tag
    • To Number
    • Traverse (Read)
    • Union
    • Unique
    • Variable Operators
    • With
  • Usage
    • Output format
    • Working with CSV, TSV
    • Working with JSON
    • Working with Properties
    • Working with XML
    • Working with LUA
    • Working with TOML
    • Working with Shell Output
    • Front Matter
    • Split into multiple files
    • GitHub Action
    • Tips, Tricks, Troubleshooting
  • Github Page
Powered by GitBook
On this page
  • Parse json: simple
  • Parse json: complex
  • Encode json: simple
  • Encode json: simple - in one line
  • Encode json: comments
  • Encode json: anchors
  • Encode json: multiple results
  • Roundtrip JSON Lines / NDJSON
  • Roundtrip multi-document JSON
  • Update a specific document in a multi-document json
  • Find and update a specific document in a multi-document json
  • Decode JSON Lines / NDJSON

Was this helpful?

  1. Usage

Working with JSON

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.

Parse json: simple

JSON is a subset of yaml, so all you need to do is prettify the output

Given a sample.json file of:

{"cat": "meow"}

then

yq -p=json sample.json

will output

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:

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

then

yq -p=json sample.json

will output

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

Encode json: simple

Given a sample.yml file of:

cat: meow

then

yq -o=json '.' sample.yml

will output

{
  "cat": "meow"
}

Encode json: simple - in one line

Given a sample.yml file of:

cat: meow # this is a comment, and it will be dropped.

then

yq -o=json -I=0 '.' sample.yml

will output

{"cat":"meow"}

Encode json: comments

Given a sample.yml file of:

cat: meow # this is a comment, and it will be dropped.

then

yq -o=json '.' sample.yml

will output

{
  "cat": "meow"
}

Encode json: anchors

Anchors are dereferenced

Given a sample.yml file of:

cat: &ref meow
anotherCat: *ref

then

yq -o=json '.' sample.yml

will output

{
  "cat": "meow",
  "anotherCat": "meow"
}

Encode json: multiple results

Each matching node is converted into a json doc. This is best used with 0 indent (json document per line)

Given a sample.yml file of:

things: [{stuff: cool}, {whatever: cat}]

then

yq -o=json -I=0 '.things[]' sample.yml

will output

{"stuff":"cool"}
{"whatever":"cat"}

Roundtrip JSON Lines / NDJSON

Given a sample.json file of:

{"this": "is a multidoc json file"}
{"each": ["line is a valid json document"]}
{"a number": 4}

then

yq -p=json -o=json -I=0 sample.json

will output

{"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

Given a sample.json file of:

{
	"this": "is a multidoc json file"
}
{
	"it": [
		"has",
		"consecutive",
		"json documents"
	]
}
{
	"a number": 4
}

then

yq -p=json -o=json -I=2 sample.json

will output

{
  "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:

{"this": "is a multidoc json file"}
{"each": ["line is a valid json document"]}
{"a number": 4}

then

yq -p=json -o=json -I=0 '(select(di == 1) | .each ) += "cool"' sample.json

will output

{"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:

{"this": "is a multidoc json file"}
{"each": ["line is a valid json document"]}
{"a number": 4}

then

yq -p=json -o=json -I=0 '(select(has("each")) | .each ) += "cool"' sample.json

will output

{"this":"is a multidoc json file"}
{"each":["line is a valid json document","cool"]}
{"a number":4}

Decode JSON Lines / NDJSON

Given a sample.json file of:

{"this": "is a multidoc json file"}
{"each": ["line is a valid json document"]}
{"a number": 4}

then

yq -p=json sample.json

will output

this: is a multidoc json file
---
each:
  - line is a valid json document
---
a number: 4
PreviousWorking with CSV, TSVNextWorking with Properties

Last updated 1 year ago

Was this helpful?