Skip to main content

YAML Flow Style

YAML Flow Style

The YAML Flow Style is an extension of JSON.

Flow style is less readable for humans, but sometimes it is better for the computer that processes our YAML.

An example of flow style:

host: tutorialreference-vm
datacenter: {location: europe, cab: 123}
animals: [lion, elephant, crocodile]

where:

  • the first line shows a key-value mapping. (key is host and value is tutorialreference-vm)
  • The second line shows is a key-value mapping whose value is a set of curly brackets.
  • The animals key shows an array defined into square brackets.

Flow Representation

Now let us discuss how the flow style behaves in different situations:

  • Alias Nodes
  • Empty Nodes
  • Flow Scalar styles
  • Flow collection styles
  • Flow nodes

Alias Nodes

Example of alias nodes:

%YAML 1.2
---
!!map {
? !!str "First occurrence"
: &A !!str "Foo",
? !!str "Override anchor"
: &B !!str "Bar",
? !!str "Second occurrence"
: *A,
? !!str "Reuse anchor"
: *B,
}

The JSON output of the given YAML is as follows:

{
"First occurrence": "Foo",
"Second occurrence": "Foo",
"Override anchor": "Bar",
"Reuse anchor": "Bar"
}

Empty Nodes

Nodes with empty content are considered as empty nodes.

%YAML 1.2
---
!!map {
? !!str "foo" : !!str "",
? !!str "" : !!str "bar",
}

The JSON output of the given YAML is as follows:

{
"": "bar",
"foo": ""
}

Flow Scalar Styles

Flow scalar styles include double-quoted, single-quoted and plain types.

%YAML 1.2
---
!!map {
? !!str "implicit block key"
: !!seq [
!!map {
? !!str "implicit flow key"
: !!str "value",
}
]
}

The JSON output of the given YAML is as follows:

{
"implicit block key":[
{
"implicit flow key":"value"
}
]
}

Flow Collection Styles

Flow collection in YAML is nested with a block collection within another flow collection.

Flow collection entries are terminated with comma (,) indicator.

%YAML 1.2
---
!!seq [
!!seq [
!!str "one",
!!str "two",
],
!!seq [
!!str "three",
!!str "four",
],
]

The JSON output of the given YAML is as follows:

[
[
"one",
"two"
],
[
"three",
"four"
]
]

Flow Nodes Styles

Flow styles like JSON include start and end indicators. The only flow style that does not have any property is the plain scalar.

%YAML 1.2
---
!!seq [
!!seq [ !!str "a", !!str "b" ],
!!map { ? !!str "a" : !!str "b" },
!!str "a",
!!str "b",
!!str "c",]

The JSON output of the given YAML is as follows:

[
[
"a",
"b"
],

{
"a": "b"
},

"a",
"b",
"c"
]