What is the use of the pipe symbol in YAML?

I am new to yaml, and I have a question about the pipe symbol (|) used for multiple lines. Does YAML have any syntax like the one below?

test: |6+

Of the two YAML files below, the first one is working and second is not. I do not know what is causing this.

First File

Name :
  -
   testing:
     val1
  -
   second:
     val2
  -
   third:
     val3
  -
   then
  - 
    final: |
     a
     aa
     aaa
     aaaa : 'test:'

Second File

Name :
  -
   testing:
     val1
  -
   second:
     val2
  -
   third:
     val3
  -
   then
  - 
    final: |6+
      a
      aa
      aaa
      aaaa : 'test:'

The second file is the customer's.

I am using XMLBeans and I get this error:

com.esotericsoftware.yamlbeans.parser.Parser$ParserException: Line 17, column 12: Expected a 'block end' but found: block mapping start".


Solution 1:

The pipe symbol at the end of a line in YAML signifies that any indented text that follows should be interpreted as a multi-line scalar value. See the YAML spec.

Specifically, the pipe indicates that (except for the indentation) the scalar value should be interpreted literally in such a way that preserves newlines. Conversely, the > character indicates that multi-line "folded" scalar follows, meaning that newlines are converted to spaces. For example:

>>> import yaml
>>> yaml.load("""
... |
...  This is a multi-line
...  literal style scalar.
... """)
'This is a multi-line\nliteral style scalar.\n'
>>> yaml.load("""
... >
...  This is a multi-line
...  folded scalar; new lines are folded into
...  spaces.
... """)
'This is a multi-line folded scalar; new lines are folded into spaces.\n'

The 6+ part is the indentation indicator (an explicit specification of how many spaces of indentation should be used) with the "chomping indicator" + which controls how extra whitespace at the end of the scalar literal should be handled.

The error you're getting is a tricky one: It's because indentation should be relative to the current block-level element. So in this case it should be 2+ instead of 6+ because the last block-level element is the mapping final: and the literal is indented 2 from it. Updated with correction from @bramvi.

Solution 2:

The pipe is used when you want newlines to be kept as newlines.

For more information : https://yaml-multiline.info/