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
  • Default behavior
  • Integers
  • Float
  • Booleans
  • Nulls
  • Strings
  • Using the tag flag to cast
  • Casting booleans
  • Casting nulls
  • Custom types
  • The style flag
  • Single
  • Double
  • Folded:
  • Flow:
  • Literal
  • Tagged

Was this helpful?

  1. Usage

Value Parsing

How values are parsed from the CLI to commands that create/update yaml (e.g. new/write).

yq attempts to parse values intelligently, e.g. when a number is passed it - it will assume it's a number as opposed to a string. yq will not alter the representation of what you give. So if you pass '03.0' in, it will assume it's a number and keep the value formatted as it was passed in, that is '03.0'.

The --tag flag can be used to override the tag type to force particular tags.

Default behavior

Integers

Given

yq new key 3

results in

key: 3

Given a formatted number

yq new key 03

results in

key: 03

yq keeps the number formatted as it was passed in.

Float

Given

yq new key "3.1"

results in

key: 3.1

Note that quoting the number does not make a difference.

Given a formatted decimal number

yq new key 03.0

results in

key: 03.0

yq keeps the number formatted as it was passed in

Booleans

yq new key true

results in

key: true

Nulls

yq new key null

results in

key: null
yq new key '~'

results in

key: ~
yq new key ''

results in

key:

Strings

yq new key whatever

results in

key: whatever
yq new key ' whatever '

results in

key: ' whatever '

Using the tag flag to cast

Previous versions of yq required double quoting to force values to be strings, this no longer works - instead use the --tag flag.

Casting booleans

yq new --tag '!!str' key true

results in

key: "true"

Casting nulls

yq new --tag '!!str' key null

results in

key: "null"

Custom types

yq new --tag '!!farah' key gold

results in

key: !!farah gold

The style flag

The --style flag can be used to specify the quote or block style of the node value. Valid values are

  • single

  • double

  • folded

  • flow

  • literal

  • tagged

For example, given:

MULTILINE=$(cat <<END
    This is line one.
    This is line two.
END
)

SINGLE="only one line"

Single

yq n --style single things "$MULTILINE"
things: 'This is line one.

  This is line two.'

Double

things: "This is line one.\nThis is line two."

Folded:

things: >-
  This is line one.

  This is line two.

Folded single line:

things: >-
  only one line

Flow:

things: |-
  This is line one.
  This is line two.

Flow single line:

things: only one line

Literal

things: |-
  This is line one.
  This is line two.

Literal single line

things: |-
  only one line

Tagged

Always show the tag, note - you must also pass in --tag='!!str'

things: !!str |-
  This is line one.
  This is line two.

Tagged single line

things: !!str only one line

PreviousPath ExpressionsNextWorking with JSON

Last updated 4 years ago

Was this helpful?

Note that yq supports yaml spec 1.2 - which means the values yes/no are no longer parsed as booleans, but as strings see and for more information.

https://yaml.org/spec/1.2/spec.html
https://github.com/go-yaml/yaml/tree/v3