@@ -33,26 +33,24 @@ def walk_and_apply_json(json_obj: Dict[Any, Any], apply) -> Dict[Any, Any]:
33
33
"""Recursively walks a json object and applies a given function to the keys."""
34
34
35
35
def _walk_and_apply_json (json_obj , new ):
36
- if isinstance (json_obj , dict ):
37
- if isinstance (new , dict ):
38
- for key , value in json_obj .items ():
39
- new_key = apply (key )
40
- if isinstance (value , dict ):
41
- new [new_key ] = {}
42
- _walk_and_apply_json (value , new = new [new_key ])
43
- elif isinstance (value , list ):
44
- new [new_key ] = []
45
- for item in value :
46
- _walk_and_apply_json (item , new = new [new_key ])
47
- else :
48
- new [new_key ] = value
49
- elif isinstance (new , list ):
50
- new .append (_walk_and_apply_json (json_obj , new = {}))
51
- elif isinstance (json_obj , list ):
52
- if isinstance (new , dict ):
53
- new .update (json_obj )
54
- elif isinstance (new , list ):
55
- new .append (json_obj )
36
+ if isinstance (json_obj , dict ) and isinstance (new , dict ):
37
+ for key , value in json_obj .items ():
38
+ new_key = apply (key )
39
+ if isinstance (value , dict ):
40
+ new [new_key ] = {}
41
+ _walk_and_apply_json (value , new = new [new_key ])
42
+ elif isinstance (value , list ):
43
+ new [new_key ] = []
44
+ for item in value :
45
+ _walk_and_apply_json (item , new = new [new_key ])
46
+ else :
47
+ new [new_key ] = value
48
+ elif isinstance (json_obj , dict ) and isinstance (new , list ):
49
+ new .append (_walk_and_apply_json (json_obj , new = {}))
50
+ elif isinstance (json_obj , list ) and isinstance (new , dict ):
51
+ new .update (json_obj )
52
+ elif isinstance (json_obj , list ) and isinstance (new , list ):
53
+ new .append (json_obj )
56
54
return new
57
55
58
56
return _walk_and_apply_json (json_obj , new = {})
0 commit comments