I'm trying to do a programmatic modification of dicts. After floundering on my own, I tried lots of different prompts at ChatGPT and Bard, but they didn't generate anything the gets my desired output.
I think I have used glom for this kind of thing in the past, but can't find any of my snippets to build off of.
There are some similar questions on SO, but mine is different in that I want to modify scalar values, or each of the elements in a list value.
I have dicts like this:
{
"name": ["joe", "bob", "jane"],
"rank": "beginner",
"address": {
"street": {"direction": "east", "name": "main"},
"town": {"name": "boston", "state": "mass"},
"name": {"first": "joe", "last": "smith"},
},
"clubs": {
"art": {"name": "art club", "rank": "expert", "site": "art.com"},
},
"fake": {
"real": {"name": "fake"},
"fake": {"name": "fake"},
},
}
I'm trying to write a function that would recurse through the dict and apply a function to all scalar and list values of the keys in a list I specify.
For example, if my list was ["name", "rank"]
and the function was string capitalization, then I would expect the output to be
{
"name": ["JOE", "BOB", "JANE"],
"rank": "BEGINNER",
"address": {"street": {"direction": "east", "name": "MAIN"}},
"clubs": {"art": {"name": "ART CLUB", "rank": "EXPERT", "site": "art.com"}},
"fake": {"real": {"name": "FAKE"}, "fake": {"name": "FAKE"}},
}