MCPcopy Create free account
hub / github.com/codefuse-ai/codefuse-devops-eval / flatten_json

Function flatten_json

src/utils/json_utils.py:4–18  ·  view source on GitHub ↗

\n Flatten a nested JSON object\n

(nested_json, parent_key='', sep='_')

Source from the content-addressed store, hash-verified

2
3
4def flatten_json(nested_json, parent_key='', sep='_'):
5 """\n Flatten a nested JSON object\n """
6 items = []
7 for key, value in nested_json.items():
8 new_key = f"{parent_key}{sep}{key}" if parent_key else key
9 if isinstance(value, dict):
10 items.extend(flatten_json(value, new_key, sep=sep).items())
11 elif isinstance(value, list):
12 value_c = sorted(value)
13 for i, v in enumerate(value_c):
14 new_item = flatten_json(v, f"{new_key}{sep}{i}", sep=sep)
15 items.extend(new_item.items())
16 else:
17 items.append((new_key, value))
18 return dict(items)
19
20
21def read_json_file(filename):

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected