YAML's most basic syntax

Source: Internet
Author: User
Tags json

As Yaml represents, Yaml Ain ' t Markup Language,yaml is a concise, non-markup language. YAML is data-centric, uses whitespace, indents, and organizes data into branches, making the presentation more concise and readable.

While learning the rules, you can practice the basic rules in the online demo-yaml conversion JSON Web page.

YAML has the following basic rules:
1, Case sensitive
2. Use indentation to represent hierarchical relationships
3, prohibit the use of tab indentation, you can only use the space key
4. There is no limit to the indentation length, as long as the element alignment indicates that the elements belong to a hierarchy.
5. Use # to indicate comments
6. String can be used without quotation marks three kinds of data structure

1, map, hash list
Use a colon (:) to denote a key-value pair, and all key-value pairs of the same indentation belong to a map, for example:

# Yaml means
age:12
Name:huang

# Corresponds to the JSON representation
{' age ': ', ' name ': ' Huang '}

You can also write a map on one line:

# Yaml means
{age:12,name:huang}

# corresponds to the JSON representation
{' age ': ', ' name ': ' Huang '}

2, List, array
Use a hyphen (-) to indicate:

# Yaml means
-a
-B
-

# corresponds
to the JSON representation [' A ', ' B ', 12]

It can also be written in one line:

# Yaml means
[a,b,c]

# Corresponds to JSON representation
[' A ', ' B ', ' C ']

3, scalar, pure quantity
The smallest unit of data can no longer be split. Data Structure Nesting

The elements of map and list can be another map or list or a pure quantity. There are 4 common nesting of data:
1. Map nested map

# YAML means
websites:
 YAML:yaml.org 
 ruby:ruby-lang.org 
 Python:python.org 
 Perl:use.perl.org 

# corresponds to the JSON representation
{websites: 
   {YAML: ' yaml.org ',
     Ruby: ' ruby-lang.org ',
     Python: ' python.org ',
     Perl: ' use.perl.org '}

2. Map nested list

# Yaml means
languages:
 -Ruby
 -Perl
 -Python 
 -C

# corresponds to JSON notation
{languages: [' Ruby ', ' Perl ', ' Python ', ' C ']}

3. List nested list

# Yaml means
--
  Ruby
  -Perl
  -Python 
- 
  -C
  -C + +
  -Java

# corresponds to the JSON representation
[[' Ruby ', ' Perl ', ' Python '], [' C ', ' C + + ', ' Java ']

In addition, you can also indicate the structure as follows

# method 2
--Ruby
  -Perl
  -Python- 
-C
  -C + +
  -Java

# Method 3
-[Ruby,perl,python]
-[C,c++, Java

4. List nested map

# YAML Representation
-
  id:1
  name:huang
-
  id:2
  name:liao

# Corresponds to JSON representation
[{id:1, name: ' Huang '}, {id:2, Name: ' Liao '}]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.