[Solved]: How to escape indicator characters ( : , - or ?) in YAML? | Escaping colons in YAML
Problem Statement:
In a config file, I have a key to which I wish to assign a URL. The problem is that YAML interprets: and – characters as either creating mappings or lists, so it has a problem with the line
url: http://www.example-site.com/
(both because of the colon following HTTP and the hyphen in the middle)
Is there an explicit way to escape ‘:’ and ‘-‘? Or would it work to just put the whole thing in single quotes and call it a day?
Solution:
Plain scalars(value field) must never contain the “: ” and “ #” character combinations. Such combinations would cause ambiguity with mapping key: value pairs and comments.
In addition, inside flow collections, or when used as implicit keys, plain scalars must not contain the “[”, “]”, “{”, “}” and “,” characters. These characters would cause ambiguity with flow collection structures.
You can try these options:
YAML
- url: 'http://www.example-site.com/'- url: "http://www.example-site.com/"- url: http://www.example-site.com/- url: >- http://www.example-site.com/ #Recommended- url: |- http://www.example-site.com/ #Recommended
Equivalent JSON:
[ { "url": "http://www.example-site.com/" }, { "url": "http://www.example-site.com/" }, { "url": "http://www.example-site.com/" }, { "url": "http://www.example-site.com/" }, { "url": "http://www.example-site.com/" }]
if the key has indicator characters (:, – or ?) then put the key in brackets.
YAML
"8.11.32.120: 8000": GoogleMapsKeyforThisDomain"? exampledomain.comusers": GoogleMapsAPIKeyforThatDomain
JSON
{ "8.11.32.120: 8000": "GoogleMapsKeyforThisDomain", "? exampledomain.comusers": "GoogleMapsAPIKeyforThatDomain"}
References:
- http://yaml.org/spec/1.2/spec.html#id2788859
- https://stackoverflow.com/questions/11301650/how-to-escape-indicator-characters-i-e-or-in-yaml/22483116#22483116
- https://en.wikipedia.org/wiki/Delimiter#Delimiter_collision
- https://jekyllrb.com/docs/frontmatter/