I have a string that could be parsed as a JSON or dict object. My string variable looks like this :
my_string_variable = """{
"a":1,
"b":{
"b1":1,
"b2":2
},
"b": {
"b1":3,
"b2":2,
"b4":8
}
}"""
When I do json.loads(my_string_variable)
, I have a dict but only the second value of the key "b"
is kept, which is normal because a dict can't contain duplicate keys.
What would be the best way to have some sort of defaultdict
like this :
result = {
"a": 1,
"b": [{"b1": 1, "b2": 2}, {"b1": 3, "b2": 2, "b4": 8}],
}
I have already looked for similar questions but they all deal with dicts or lists as an input and then create defaultdict
s to handle the duplicate keys.
In my case I have a string variable and I would want to know if there is a simple way to achieve this.