YAML (.yaml, .yml)
Background
-
- MIME type: application/yaml, application/openapi+yaml
- Configuration file format.
- Human-readable data serialization format.
- Associates keys to values.
- Maps unambiguously to a hash table.
- Plain text format.
Import & Export
- Import["file.yaml"] returns a representation of a YAML file as an Association.
- Import["file","YAML"] returns a representation of a file as a YAML file.
- Import["file.yaml",elem] returns the specified data representation of a YAML file.
- Import["file",{"YAML",elem}] returns the specified data representation of a file.
- Export["file.yaml",expr] creates a YAML file from expr.
- See the following reference pages for full general information:
-
Import, Export import from or export to a file CloudImport, CloudExport import from or export to a cloud object ImportString, ExportString import from or export to a string ImportByteArray, ExportByteArray import from or export to a byte array
Import Elements
- General Import elements:
-
"Elements" list of elements and options available in this file "Summary" summary of the file "Rules" list of rules for all available elements - Data representation elements:
-
"Data" data as an association - The YAML format exports elements of an association or a list.
- YAML treats an association as a list of sections. The keys specify the section names; the values specify the elements of a section as associations.
Import Options
- Import options include:
-
ByteOrdering Automatic ordering of bytes CharacterEncoding "UTF8" raw character encoding used in the file
Export Options
- Export options include:
-
"ExpressionFormattingFunction" Automatic how expressions not supported by YAML are converted to strings "IndentSize" 2 number of spaces used for indentation "TerseAssociationFormatting" False whether to use terse representation of associations "TerseListFormatting" False whether to use terse representation of lists - "ExpressionFormattingFunction" can be set to the following values:
-
Automatic default conversion to string form any form supported by Format such as InputForm f arbitrary function that converts an expression to a string
Examples
open all close allBasic Examples (2)
Scope (1)
Import Elements (2)
"Data" (1)
Import Options (1)
CharacterEncoding (1)
The character encoding can be set to any value from $CharacterEncodings:
input = "é";ImportString[input, "YAML", CharacterEncoding -> "UTF8"]ImportString[input, "YAML", CharacterEncoding -> "ISOLatin1"]Export Options (4)
"ExpressionFormattingFunction" (1)
By default, Export uses different conversion to string depending on type of expression:
ExportString[<|"image" -> RandomImage[], "quantity" -> Quantity[100, "Kilometers"/"Hours"] |>, "YAML"]Use "ExpressionFormattingFunction"->InputForm to get string versions of expressions suitable for input to the Wolfram Language:
ExportString[<|"image" -> RandomImage[], "quantity" -> Quantity[100, "Kilometers"/"Hours"] |>, "YAML", "ExpressionFormattingFunction" -> InputForm]//ShortUse a function that converts expressions to strings:
ExportString[<|"image" -> RandomImage[], "quantity" -> Quantity[100, "Kilometers"/"Hours"] |>, "YAML", "ExpressionFormattingFunction" -> SpokenString]"IndentSize" (1)
By default, Export indents using two spaces:
ExportString[<|"table" -> <|"a" -> "b", "subtable" -> <|"c" -> "d"|>|>|>, "YAML"]ExportString[<|"table" -> <|"a" -> "b", "subtable" -> <|"c" -> "d"|>|>|>, "YAML", "IndentSize" -> 4]"TerseAssociationFormatting" (1)
By default, Export does not use terse formatting of associations:
ExportString[<|"a" -> 1, "b" -> 2|>, "YAML"]Use "TerseAssociationFormatting"->True to create YAML with terse representation of associations:
ExportString[<|"a" -> 1, "b" -> 2|>, "YAML", "TerseAssociationFormatting" -> True]"TerseListFormatting" (1)
By default, Export does not use terse formatting of lists:
ExportString[<|"array" -> {1, 2, 3}|>, "YAML"]Use "TerseListFormatting"->True to create YAML with terse representation of lists:
ExportString[<|"array" -> {1, 2, 3}|>, "YAML", "TerseListFormatting" -> True]Related Guides
History
Introduced in 2026 (15.0)