Yml code string in quotes

employee:
name: john
gender: male
age: 24
address:
city: edison
state: ‘new jersey’
country: ‘united states’

In above code why we have added the string in single quotes

city: edison
state: ‘new jersey’
country: ‘united states’

and not added

employee:
name: john
gender: male

what is the difference here

Hello ghorpade84

In this case, there is no difference because the YAML parser automatically converts ASCII characters into string. But if we don’t use quotes and the string is number, such as 3.14, the YAML parser parses as a number. This can confuse us so if we know the value, it’s good to use quotes to avoid mistakes.

I created some examples in the Online YAML Parser. So you can try by yourself.
Online YAML Parser

Also, there is a more detailed explanation on this blog post:
Strings in YAML - To Quote or not to Quote | tinita [blogs.perl.org]

(In YAML, the indent spaces are important so please use “formatted quote (</>)” feature above the text box.)

1 Like