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.
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.
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”.
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.
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.
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")
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" ] } ] ] } } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With