Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nested lists in yaml

Tags:

yaml

How can I create nested lists in YAML? I want to get:

 {"Hello": ["as", ["http://", ["cat"]]]} 

Here's my YAML that doesn't work (with pyYaml):

  Hello:     - "as"       - "http://"         - cat 

What am I doing wrong?

Specifically, I'm trying to generate the following JSON from YAML:

"URL" : {   "Description" : "URL of the website",   "Value" :  { "Fn::Join" : [ "", [ "http://", { "Fn::GetAtt" : [ "ElasticLoadBalancer", "DNSName" ]}]]} } 

Here's the closest YAML I've got working, but it doesn't give quite what I need.

YAML is:

  Outputs:     URL:       Description: URL of the website       Value:         "Fn::Join":         - ""         - "http://"         - "Fn::GetAtt":           - ElasticLoadBalancer           - DNSName 

This results in:

    "URL": {         "Description": "URL of the website",          "Value": {             "Fn::Join": [                 "",                  "http://",                  {                     "Fn::GetAtt": [                         "ElasticLoadBalancer",                          "DNSName"                     ]                 }             ]         }     } 

This is almost correct, but after "" there should be a nested list, not just another list item. How can I fix this?

This is going to be fed into an API, so the output must match completely.

like image 817
user1491250 Avatar asked May 02 '13 08:05

user1491250


People also ask

Does YAML support nested?

YAML uses Python-style indentation to indicate nesting. There are no strict requirements on how many spaces to use for the indentation in YAML, but there are two basic rules: Data elements at the same level in the hierarchy must have the same indentation.

What is list in YAML?

YAML Basics For Ansible, nearly every YAML file starts with a list. Each item in the list is a list of key/value pairs, commonly called a “hash” or a “dictionary”.

How do you represent an array of objects in YAML?

An array is a group of similar values with a single name. In YAML, Array represents a single key mapped to multiple values. Each value starts with a hyphen - symbol followed by space. In a single line, write the same thing above using 'square brackets syntax.

What is key value pair in YAML?

Example. As we can see, the format is very simple. Each key is followed by a colon and then the value. We can also use quotation marks ( ' or " ) around the key-value pair if we want to include special characters or whitespace.


2 Answers

And the answer is:

URL:   Description: URL of the website   Value:     "Fn::Join":       - ""       - - "http://"         - "Fn::GetAtt":             - ElasticLoadBalancer             - DNSName 

(see http://pyyaml.org/wiki/PyYAMLDocumentation#YAMLsyntax - "block sequences can be nested")

like image 148
user1491250 Avatar answered Sep 21 '22 10:09

user1491250


Start a nested list from a new line. Using this approach it is easy to figure out.

Read this and this article. They have a lot of examples.

Try like this:

YAML

Value:     "Fn::Join":       - ""       -          - "http://"          - "Fn::GetAtt":               - ElasticLoadBalancer               - DNSName 

Equivalent JSON:

{   "URL": {     "Description": "URL of the website",     "Value": {       "Fn::Join": [         "",         [           "http://",           {             "Fn::GetAtt": [               "ElasticLoadBalancer",               "DNSName"             ]           }         ]       ]     }   } } 
like image 38
Arayan Singh Avatar answered Sep 17 '22 10:09

Arayan Singh