yq/mkdocs/value_parsing.md
2020-01-20 08:35:03 +11:00

1.9 KiB

This describes 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 behaviour

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 field to override

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