13

I'd like to convert a OmegaConf/Hydra config to a nested dictionary/list. How can I do this?

Rylan Schaeffer
  • 1,945
  • 2
  • 28
  • 50
  • 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 Answers2

20

See OmegaConf.to_container().

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

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