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: 123float: 12.345boolean: No
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 23married: !!str truebinary: !!int 0b101010hexadecimal: !!int 0x1C7Aname: !!str "James"
Here is the table showing a list of tags supported in YAML along with its description:
Common tags supported in YAML
Tag | Description |
| Denotes a boolean value |
| Denotes a integer value |
| Denotes a floating point number |
| 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:
What is a YAML file?
Understand what YAML files are and the different styles they use.What is block style in YAML?
Learn the block style format for structuring data in YAML.What is flow style in YAML?
Discover how flow style differs from block style and when to use it.How to represent different basic data types in YAML
Explore how YAML handles various basic data types, such as strings, integers, and booleans.How to represent strings in YAML
Discover how YAML handles string values and different formatting options.How to represent arrays in YAML
Explore how YAML represents arrays, with each element preceded by a hyphen (-).How to represent key-value pairs in YAML
Learn how to effectively represent key-value pairs within YAML syntax.How to represent maps in YAML
Master how to structure key-value pairs as maps in YAML.How to represent sequence in YAML?
Understand how to represent sequences in YAML files.How to represent dictionaries in YAML
Explore how to represent complex data structures like dictionaries in YAML.How to represent null values in YAML
Learn how to properly represent null or missing values in YAML.How to write comments in YAML
Learn how to add comments in YAML to improve readability.What are the advantages of using YAML over other data formats?
Understand why YAML is often preferred over JSON and XML for data serialization.