I have a json file in this format,
{
"details": {
"hawk_branch": {
"tandem": {
"value": "4210bnd72"
}
},
"uclif_branch": {
"tandem": {
"value": "e2nc712nma89",
"value": "23s24212",
"value": "12338cm82",
}
}
}
}
The problem is, I need to keep all the value
, however when i use json.load
to load this file i only get one value
, which make sense since dict
can keep only unique keys
.
Here is the expected output,
{ "hawk_branch": ["4210bnd72"] }
{ "uclif_branch": ["e2nc712nma89" , "23s24212", "12338cm82"] }
I have read this answer, Python json parser allow duplicate keys to use object_pairs_hook
like this,
def parse_object_pairs(pairs):
return pairs
# f is file
json.load(f, object_pairs_hook=parse_object_pairs)
but it returns entire json file as list
.
I think its possible to do it using lambda
as object_pairs_hook
but i can't understand how can I use it.
Can someone please guide me