Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Syntax for empty dictionary in YAML

Tags:

yaml

How do I denote an empty dictionary in YAML? I.e. it should be semantically equivalent to the empty json-object {}.

like image 843
Betamos Avatar asked Nov 03 '15 22:11

Betamos


People also ask

How specify empty value in YAML?

You can use ~ or null . Show activity on this post. If you want an empty string, rather than a null value, you can use two single quotes.

Can YAML have empty values?

Representing null values in YAML There are several ways to represent null values in YAML. Empty values are also considered to be null values.

How declare empty array in YAML?

Thus [] works for an empty sequence, "" works for an empty string, and {} works for an empty mapping. Many parsers are still on YAML 1.1; this is probably what Wikipedia is talking about.


2 Answers

Short answer: use {}

There are two ways to denote mappings (dictionaries) in yaml; flow mappings and block mappings:

block_mapping:     name:  foo     id:    bar flow_mapping: { name: foo, id: bar } empty_flow_mapping: {} 

The flow mapping style is thus suitable for representing empty mappings.

like image 98
Betamos Avatar answered Sep 18 '22 15:09

Betamos


General technique for answering this type of question, to supplement Betamos’s correct answer: use irb.

$ irb 2.2.0 :001 > require 'yaml'  => true  2.2.0 :002 > puts({}.to_yaml)   # original question --- {}  => nil  2.2.0 :003 > puts({ mixed_types: [{}, "string", :symbol, {symbol: "value"}, nil, 3] }.to_yaml) --- :mixed_types: - {} - string - :symbol - :symbol: value -  - 3  => nil 

I use this whenever I’m unsure how to encode something.

like image 35
Paul Cantrell Avatar answered Sep 20 '22 15:09

Paul Cantrell