-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathsample_data_transformations.py
More file actions
65 lines (62 loc) · 2.26 KB
/
sample_data_transformations.py
File metadata and controls
65 lines (62 loc) · 2.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
from aladdinsdk.common.datatransformation import json_to_pandas
import json
json_raw = [{
"id": "0001",
"type": "donut",
"name": "Cake",
"ppu": 0.55,
"batters":
{
"batter":
[
{"id": "1001", "type": "Regular", "testleafarr": [1, 2, 3]},
{"id": "1002", "type": "Chocolate", "testleafarr": [1, 2, 3]},
{"id": "1003", "type": "Blueberry", "testleafarr": [1, 2, 3]},
{"id": "1004", "type": "Devil's Food", "testleafarr": [1, 2, 3]},
{"id": "1005", "type": "Vanilla", "testleafarr": [1, 2, 3]}
]
},
"topping":
[
{"id": "5001", "type": "None"},
{"id": "5002", "type": "Glazed"},
{"id": "5005", "type": "Sugar"},
{"id": "5007", "type": "Powdered Sugar"},
{"id": "5006", "type": "Chocolate with Sprinkles"},
{"id": "5003", "type": "Chocolate"},
{"id": "5004", "type": "Maple"}
]
}, {
"id": "0002",
"type": "donut2",
"name": "Cake2",
"ppu": 0.55,
"batters":
{
"batter":
[
{"id": "2001", "type": "2Regular", "testleafarr": [1, 2, 3]},
{"id": "2002", "type": "2Chocolate", "testleafarr": [1, 2, 3]},
{"id": "2003", "type": "2Blueberry", "testleafarr": [1, 2, 3]},
{"id": "2004", "type": "2Devil's Food", "testleafarr": [1, 2, 3]}
]
},
"topping":
[
{"id": "5001", "type": "None"},
{"id": "5002", "type": "Glazed"},
{"id": "5005", "type": "Sugar"},
{"id": "5007", "type": "Powdered Sugar"},
{"id": "5006", "type": "Chocolate with Sprinkles"},
{"id": "5003", "type": "Chocolate"},
{"id": "5004", "type": "Maple"}
]
}
]
# Flatten at testleafarr
df_resp = json_to_pandas.convert(json.dumps(json_raw), "[*].batters.batter.[*].testleafarr.[*]")
print(df_resp)
# Get more options to flatten on
flatten_options = json_to_pandas.conversion_options(json.dumps(json_raw))
print(f'Flatten on: {flatten_options[4]}')
print(json_to_pandas.convert(json.dumps(json_raw), flatten_options[4]))