json-cfg icon indicating copy to clipboard operation
json-cfg copied to clipboard

Accessing parameters using string only

Open adrianohrl opened this issue 4 years ago • 0 comments

Is it possible to access the parameters of a config file via string only?

Consider a config file with the content below:

{
  "level_0": {
    "level_1": {
      "param_0": "value_0"
    }
  }
}

The param_0 parameter could be accessed as bellow:

config = jsoncfg.load_config('config.json')

param_0 = config.get('level_0.level_1.param_0', 'default_value')

# or
if 'level_0.level_1.param_0' in config:
  param_0 = config['level_0.level_1.param_0']

print(param_0)

This would be a dict-like interface. However, one pair of brackets only would be necessary.

adrianohrl avatar May 30 '20 23:05 adrianohrl