How to represent different basic data types in YAML

Introduction

YAML is a human-readable data serialization standard that can be used to represent different data types. In this shot, we learn to represent some of the most common data types in YAML.

Scalars

Scalars are the simplest data type in YAML and can represent basic types, including boolean, integers, and floating-point numbers. The following basic data types are supported by YAML:

  • Boolean

  • Integer

  • Float

  • String

  • Date

  • Timestamp

Examples

Here is an example of how to represent a string, an integer, a floating-point number, and a boolean value in YAML:

string: "Hello"
integer: 123
float: 12.345
boolean: No
Simple data types in YAML

As we can see, the format for each data type is very similar. To represent a string in YAML, we use a single quote (') or a double quote (") and the actual string value within these quotes. We use the standard numeric literals for integers and floating-point numbers. Boolean values can be represented in YAML using the keywords like true and false or Yes and No.

YAML can autodetect types. However, it is often necessary to explicitly specify the type using a tag. To force a type, we can prefix the type with a !! symbol. Here's an example:

age: !!float 23
married: !!str true
binary: !!int 0b101010
hexadecimal: !!int 0x1C7A
name: !!str "James"
Explicitly specify a type in YAML

Here is the table showing a list of tags supported in YAML along with its description:

Common tags supported in YAML

Tag

Description

!!bool

Denotes a boolean value

!!int

Denotes a integer value

!!float

Denotes a floating point number

!!str

Denotes a string

Conclusion

YAML is a very versatile data serialization standard that can be used to represent many different data types. In this shot, we learned to represent some of the most common data types in YAML.

  • To learn more about the syntax, data types, and various key concepts related to the YAML language, check Educative's Introduction to YAML course.

Unlock your potential: YAML basics series, all in one place!

To continue your exploration of YAML basics, check out our series of Answers below:

Free Resources