View on GitHub

samrat.github.io

Cases when YAML will not work if not properly declared

Wrong Interpretation

Expected behaviour when yaml is read. Since YAML considers everything as a string.

{ 
	'countries': ['IN', 'SA', 'NO'], 
	'name': 
		{
			'first_name': 'Christopher', 
			'second_name': 'Null'
		}
}

Actual result:

{ 
	'countries': ['IN', 'SA', False], 
	'name': 
		{
			'first_name': 'Christopher', 
			'second_name': None
		}
}

NO is converted to False which was supposed to be short for Norway

Null is converted to None which was supposed to be second name

Data Type Mismatch

Actual

{
'versions':
	{
		'postgres' : 9.3
		'rabbitmq' : '3.18.9'
	}
}

Since 9.3 is a floating value is is converted as floating value. We need to be extra careful in the parsing logic

Learning

Code snippet

import os
from yaml import safe_load

with open('sample.yaml') as file:
    data = safe_load(file) 

print(data)
countries:
  - IN
  - SA
  - NO

name:
  first_name: Christopher
  second_name: Null

versions:
  postgres: 9.3
  rabbitmq: 3.18.9