|
1 | 1 | import json
|
2 | 2 | import pandas as pd
|
3 |
| -from pandas import json_normalize |
4 | 3 |
|
5 |
| -# Load JSON from file or string |
| 4 | +# Recursive function to flatten nested JSON |
| 5 | +def flatten(obj, parent_key='', sep='.'): |
| 6 | + items = [] |
| 7 | + if isinstance(obj, dict): |
| 8 | + for k, v in obj.items(): |
| 9 | + new_key = f"{parent_key}{sep}{k}" if parent_key else k |
| 10 | + items.extend(flatten(v, new_key, sep=sep).items()) |
| 11 | + elif isinstance(obj, list): |
| 12 | + for i, v in enumerate(obj): |
| 13 | + new_key = f"{parent_key}[{i}]" |
| 14 | + items.extend(flatten(v, new_key, sep=sep).items()) |
| 15 | + else: |
| 16 | + items.append((parent_key, obj)) |
| 17 | + return dict(items) |
| 18 | + |
| 19 | +# Load JSON file |
6 | 20 | with open("input.json") as f:
|
7 |
| - data = json.load(f) |
| 21 | + raw_data = json.load(f) |
8 | 22 |
|
9 |
| -# Flatten JSON |
10 |
| -def flatten_json(json_data): |
11 |
| - if isinstance(json_data, list): |
12 |
| - df = json_normalize(json_data) |
13 |
| - elif isinstance(json_data, dict): |
14 |
| - df = json_normalize([json_data]) |
15 |
| - else: |
16 |
| - raise ValueError("Unsupported JSON format") |
17 |
| - return df |
| 23 | +# Handle list of records or single record |
| 24 | +if isinstance(raw_data, list): |
| 25 | + flat_data = [flatten(entry) for entry in raw_data] |
| 26 | +else: |
| 27 | + flat_data = [flatten(raw_data)] |
18 | 28 |
|
19 |
| -# Unnest and convert to CSV |
20 |
| -df = flatten_json(data) |
| 29 | +# Convert to DataFrame |
| 30 | +df = pd.DataFrame(flat_data) |
21 | 31 |
|
22 | 32 | # Save to CSV
|
23 | 33 | df.to_csv("output.csv", index=False)
|
0 commit comments