I have a JSON file in the below format and I want to convert it into a pandas dataframe using a function. The fuction should return the dataframe and the sensorID.
sensor_data_df, sensorID = ImporterFunction( json_str )
• Input 1: json_str
o Type: String
• Output 1: sensor_data_df
o Type: Pandas dataframe
• Output 2: SensorID
o Type: stirng
{
"SensorId": "B22EA071",
"startUTC": 1671490577,
"endUTC": 1671516101,
"Data": [
{
"TimeStamp": 1671490584,
"V1": 21.1,
},
{
"TimeStamp": 1671490666,
"V1": 21,
}]
The dataframe should be like this.
startUTC | endUTC | Timestamp | V1 |
---|---|---|---|
1671490577 | 1671516101 | 1671490584 | 21.1 |
1671490577 | 1671516101 | 1671490666 | 21 |
How can I do this in python?
2
Answers
If I am not misunderstood your requirements then this is what you need, use
json_normalize
to make dict to dfOutput:
This Should work. Also notice that your json dump has missing ending ‘}’. I see someone also answered which I think is more elegant (use of json_normalize).
`
`