I need to get the below Json data into a pandas DataFrame. This is a case of nested JSON which consists of multiple lists and dicts .
JavaScript
x
29
29
1
{
2
"status" : "success",
3
"data" : {
4
"resultType" : "vector",
5
"result" : [
6
{
7
"metric" : {
8
"__name__":"request_time_summary_count",
9
"api":"USSD",
10
"instance":"10.104.3.50:8080",
11
"job":"service-endpoints",
12
"operation":"MO"
13
},
14
"value": [ 1660136610.587, "3" ]
15
},
16
{
17
"metric" : {
18
"__name__":"request_time_summary_count",
19
"api":"USSD",
20
"instance":"service.default.svc:8080",
21
"job":"ETD-ussd",
22
"operation":"MO"
23
},
24
"value" : [ 1660136610.587, "4" ]
25
}
26
]
27
}
28
}
29
Expected format as follows: enter image description here
Advertisement
Answer
Try:
JavaScript
1
33
33
1
data = {
2
"status": "success",
3
"data": {
4
"resultType": "vector",
5
"result": [
6
{
7
"metric": {
8
"__name__": "request_time_summary_count",
9
"api": "USSD",
10
"instance": "10.104.3.50:8080",
11
"job": "service-endpoints",
12
"operation": "MO",
13
},
14
"value": [1660136610.587, "3"],
15
},
16
{
17
"metric": {
18
"__name__": "request_time_summary_count",
19
"api": "USSD",
20
"instance": "service.default.svc:8080",
21
"job": "ETD-ussd",
22
"operation": "MO",
23
},
24
"value": [1660136610.587, "4"],
25
},
26
],
27
},
28
}
29
30
df = pd.DataFrame(data["data"]["result"])
31
df = pd.concat([df.pop("metric").apply(pd.Series), df], axis=1)
32
print(df)
33
Prints:
JavaScript
1
4
1
__name__ api instance job operation value
2
0 request_time_summary_count USSD 10.104.3.50:8080 service-endpoints MO [1660136610.587, 3]
3
1 request_time_summary_count USSD service.default.svc:8080 ETD-ussd MO [1660136610.587, 4]
4