Skip to content

flatten method should return a line for each element in a list #29

@Loreton

Description

@Loreton

I need that flatten method return {"key_path": value} also for elements in a list in a dictionary.
For the following dict:

test:
  d10:
    d11: single
    d12:
      crontab: ["cron_01", "cron_02"]
      minidlna: ["mini_01", "mini_02"]
    d13:
      - dict01: ciao_01
      - dict02: ciao_02

dict.flatten() return:

{
    "test/d10/d11": "single",
    "test/d10/d12/crontab": ["cron_01", "cron_02"],
    "test/d10/d12/minidlna": ["mini_01", "mini_02"],
    "test/d10/d13": [{"dict01": "ciao_01"}, {"dict02": "ciao_02"} ]
}

I would like to get something like:

{
    "test/d10/d11": "single",
    "test/d10/d12/crontab/[0]": "cron_01",
    "test/d10/d12/crontab/[1]": "cron_02",
    "test/d10/d12/minidlna/[0]": "mini_01",
    "test/d10/d12/minidlna/[1]": "mini_02",
    "test/d10/d13/[0]/dict01": "ciao_01",
    "test/d10/d13/[1]/dict02": "ciao_02"
}

where the list-index could be '[i]' or just 'ì' or anything easily identifiable.
Attached is a sample of flatten module, modified from the original, to return the above output if explode_lists=True is passed as argument.
Do you think this can be done?

My wish would go further and ask if benedict could be able to use the returned path to point to the value of the single element of list.

Regards
Loreto

flatten.zip

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Projects

Status

Todo

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions