...

/

Data types in YAML

Data types in YAML

Learn how to represent different data types in YAML.

We'll cover the following...

Data types in YAML

YAML supports most of the data types found in modern programming languages such as Perl, Python, PHP, Java, C#, Ruby, and JavaScript.

The following is the list of supported data types:

  • Boolean
  • Numbers
  • Strings
  • Dates
  • Timestamp
  • Arrays
  • Maps
  • Null

How YAML detects data types (auto-detection)

YAML automatically detects data types based on how values look. This makes YAML intuitive, but in certain situations it can lead to unexpected behavior.

If we have a thorough understanding of data types and their detection, we can prevent most configuration errors in our YAML files. We can avoid most of the configuration errors in our YAML files.

Specifying data types explicitly using tags

As we saw, YAML can auto-detect the data types, but users can also specify the type they need. To force a type, you can prefix the type with a !! symbol.

See its usage in the example below.

Example

# Only use explicit tags when needed (rare)
version: !!str 1.20   # Force "1.20" as string, not float

Scalars

A scalar is a single value (rather than a collection). In YAML, scalars are written as key-value pairs. The value can be a string, number, boolean, null, or timestamp.

See the example below about how to represent scalars in YAML.

Example

# YAML automatically detects types
message: "Hello" # String (auto-detected)
age: 23          # Integer (auto-detected)
price: 12.34.    # Float
...