How to represent arrays in YAML

Arrays

Arrays are available in almost all programming languages, including Perl, Python, PHP, Java, C#, Ruby, and JavaScript. They are linear data types used to represent a list of items.

%0 node_1 red node_2 green node_3 blue node_1627442199807 organge
Array or list of colors

In YAML data serialization format, you can represent arrays using two different styles, as explained below.

Block sequence

The block sequence style of YAML uses hyphens or dashes to (-) to represent arrays. A hyphen (-) followed by white space ( ) represents an element of an array. When you enter the dashes, you need to ensure that all items are at the same indentation level.

See the code example below to understand this better.

Example

colors: 
  - red
  - green
  - blue
  - orange

Flow sequence

The flow sequence style of YAML uses brackets and commas to represent arrays.

See the code example below to understand this better.

Example

colors: [red, green, blue, orange]

Learn more about syntax, data types, and critical concepts related to YAML language through this mini-course.

Copyright ©2024 Educative, Inc. All rights reserved