Colon in the Front: Yaml Syntax

Colon in the front: YAML syntax

YAML keys starting with a colon are transformed into symbolized keys in Ruby when parsed, whereas keys without a colon will result in stringified keys:

require 'yaml'

string =<<-END_OF_YAML
:concurrency: 5
:pidfile: /tmp/pids/sidekiq.pid
:logfile: log/sidekiq.log
staging:
:concurrency: 10
production:
:concurrency: 20
queues:
- default
END_OF_YAML

YAML.load(string)
# {
# :concurrency => 5,
# :pidfile => "/tmp/pids/sidekiq.pid",
# :logfile => "log/sidekiq.log",
# "staging" => {
# :concurrency => 10
# },
# "production" => {
# :concurrency => 20
# },
# "queues" => [
# [0] "default"
# ]
# }

Note: When a gem depends on symbolized keys, then a stringified key will not override its defaults.

How to escape indicator characters (colon and hyphen) in YAML

Quotes:

"url: http://www.some-site.example/"

To clarify, I meant “quote the value” and originally thought the entire thing was the value. If http://www.some-site.example/ is the value, just quote it like so:

url: "http://www.some-site.example/"

In sidekiq, what does the colon before variable mean?

If you load the above file in console, you're output will look something like this

For keys with colon prefixed

:concurrency: 25 will look like :concurrency => 25

For keys without colon prefixed

staging: will look like "staging"

Also, converting to symbol is very specific to Ruby only as the
interpreter takes care of it.

There was a YAML syntax error... mapping values are not allowed in this context

Instead of the issue being in the _config.yml file, it was in a completely different file altogether. So, if you get this error message from building a github-pages Jekyll page, don't assume that it is in the only "YAML" file. Search any new file that you have added for a Line 3 Column 22 and that will track down the error.

In my case, it was a title for a post in the front matter had a colon with a space after it - which I needed to put double-quotes around.

Grunt: Access YAML symbols in external file

Use external_config[':bar'] within the template.

Explanation: Grunt is just plain JavaScript, so you can access an object's properties by using the bracket notation.

Escaping a dot in a Map key in Yaml in Spring Boot

A slight revision of @fivetenwill's answer, which works for me on Spring Boot 1.4.3.RELEASE:

foo:
"[bar.com]":
a: b
"[baz.com]":
a: c

You need the brackets to be inside quotes, otherwise the YAML parser basically discards them before they get to Spring, and they don't make it into the property name.

Allowed characters in map key identifier in YAML?

Any character (if properly quoted by either single quotes 'example' or double quotes "example"). Please be aware that the key does not have to be a scalar ('example'). It can be a list or a map.

YAML_FILE_ERROR Message: Expected Commands[0] to be of string type:

Based on the comment, the issue was the use of colon in phase : $build.

yaml has some problems when it encounters a space and : as indicated in the following GitHub issue:

  • YAML syntax error when string contains a colon + space

Build simple hierarchy in YAML

Thanks for your help. I was not getting my head around YAML and creating entity groups without any name. @KeepCalmAndCarryOn: Your structure resulted in an array of nodes with an id item and a branches item at the same level (the branches item is an array of the branches, each branch being a dict of the properties. This works but doesn't display very nicely and is harder to work with (but certainly contains all the information from my example).

I prefer a strict hierarchy. I hand-coded json. Then, parsed the json to a nested julia object: an array of dicts like this:

Dict{String,Any} with 5 entries:
"4" => Dict{String,Any}("(9,6)"=>Any[Dict{String,Any}("tocond"=>6,"next"=>Any…
"1" => Dict{String,Any}("(9,6)"=>Any[Dict{String,Any}("tocond"=>6,"next"=>Any…
"5" => Dict{String,Any}("(9,6)"=>Any[Dict{String,Any}("tocond"=>6,"next"=>Any…
"2" => Dict{String,Any}("(9,6)"=>Any[Dict{String,Any}("tocond"=>6,"next"=>Any…
"3" => Dict{String,Any}("(9,6)"=>Any[Dict{String,Any}("tocond"=>6,"next"=>Any…

The output is truncated but you can see each agegroup as a key--with a dict of nodes as its value. Each dict of nodes shows the first key (a node) with an array of branches as its value. Each item in the branches array is a dict of the fields for each property of a branch.

One agegrp looks like this:

Dict{String,Any} with 8 entries:
"(9,6)" => Any[Dict{String,Any}("tocond"=>6,"next"=>Any[14, 6],"pr"=>1.0)]
"(9,5)" => Any[Dict{String,Any}("tocond"=>3,"next"=>Any[0, 0],"pr"=>0.8), Di…
"(14,6)" => Any[Dict{String,Any}("tocond"=>3,"next"=>Any[0, 0],"pr"=>1.0)]
"(9,7)" => Any[Dict{String,Any}("tocond"=>7,"next"=>Any[14, 7],"pr"=>0.85), …
"(5,5)" => Any[Dict{String,Any}("tocond"=>5,"next"=>Any[9, 5],"pr"=>0.2), Di…
"(14,8)" => Any[Dict{String,Any}("tocond"=>3,"next"=>Any[0, 0],"pr"=>0.45), D…
"(14,7)" => Any[Dict{String,Any}("tocond"=>3,"next"=>Any[0, 0],"pr"=>0.85), D…
"(19,8)" => Any[Dict{String,Any}("tocond"=>3,"next"=>Any[0, 0],"pr"=>0.9), Di…

Then, I took this Julia nested object (a bit gnarly, but with clearer structure--at least to me) and put it through YAML to create valid YAML output.

This YAML is just what I really wanted, even if it seems nasty-ish to someone who uses YAML properly. Here is just one agegrp (the others are identical in structure and follow sequentially in the YAML document):

4:                     # the id of an agegrp
(9,6): # the id of a node
- tocond: 6 # first property of a branch
next:
- 14
- 6
pr: 1.0
(9,5):
- tocond: 3
next:
- 0
- 0
pr: 0.8
- tocond: 7
next:
- 14
- 7
pr: 0.2
(14,6):
- tocond: 3
next:
- 0
- 0
pr: 1.0
(9,7):
- tocond: 7
next:
- 14
- 7
pr: 0.85
- tocond: 8
next:
- 14
- 8
pr: 0.15
<rest of output truncated...>

This follows the hierarchy I wanted:

top: an agegroup id
then a node (with more following after the indented children)
then a branch
then an array of the branches properties

I really messed myself up by mixing the name of the entities (agegrp, node, branch) with actual values of specific instances of each entity--like I was trying to label each thing with its type. You wouldn't do that in a Dict. I am using Julia here, but Dicts are essentially the same as in Python.

So, a bit of a frustrating foray into YAML. But, it will a lot easier to enter than the CSV I started with and much easier than hand authoring JSON. So, a successful foray!



Related Topics



Leave a reply



Submit