I'd like to convert a OmegaConf/Hydra config to a nested dictionary/list. How can I do this?
Asked
Active
Viewed 9,487 times
13
-
Does this answer your question? [Converting a YAML file to Python JSON object](https://stackoverflow.com/questions/50846431/converting-a-yaml-file-to-python-json-object) – Marat Oct 12 '21 at 20:20
2 Answers
20
Usage snippet:
>>> conf = OmegaConf.create({"foo": "bar", "foo2": "${foo}"})
>>> assert type(conf) == DictConfig
>>> primitive = OmegaConf.to_container(conf)
>>> show(primitive)
type: dict, value: {'foo': 'bar', 'foo2': '${foo}'}
>>> resolved = OmegaConf.to_container(conf, resolve=True)
>>> show(resolved)
type: dict, value: {'foo': 'bar', 'foo2': 'bar'}

Omry Yadan
- 31,280
- 18
- 64
- 87
-
1
-
-
-
resolving interpolations is a destructive operation that is changing the semantics of the config. e.g. modifying foo in the original config will change both foo and foo2 but it does not hold the same way after you resolve. – Omry Yadan May 03 '23 at 03:37
1
One can simply convert from omegaconf to python dictionary by dict(). Follow the example given below:
>>> type(config)
<class 'omegaconf.dictconfig.DictConfig'>
>>> config
{'host': '0.0.0.0', 'port': 8000, 'app': 'main:app', 'reload': False, 'debug': False}
>>> dict(config)
{'host': '0.0.0.0', 'port': 8000, 'app': 'main:app', 'reload': False, 'debug': False}
>>> type(dict(config))
<class 'dict'>

Gaurav Koradiya
- 338
- 4
- 8