1

My API generates a JSON file as output with the following structure:

[
{
    "Data": {
        "SensorId": "EF00BFC0",
        "SensorName": "DEVICE 05",
        "TimeStamp": 3877552823,
    },
    "ThresholdValue": {
        "SensorSubId": 1,
        "ThresholdMax": -1,
        "ThresholdMin": -1
    },
    "startUTC": 1668564000,
    "endUTC": 1668663000
},
{
    "Data": {
        "SensorId": "EF00BFC0",
        "SensorName": "DEVICE 05",
        "TimeStamp": 3877553446,
    },
    "ThresholdValue": {
        "SensorSubId": 1,
        "ThresholdMax": -1,
        "ThresholdMin": -1
    },
    "startUTC": 1668564000,
    "endUTC": 1668663000
}]

I want to generate a Pandas DataFrame with SensorID, SensorName, Timestamp, startUTC, and endUTC as columns. I tried it using the below code.

df_nested_list = pd.json_normalize(
    data, 
    record_path =['Data'], 
    meta=['startUTC', 'endUTC']
)

But it gives the column names as rows and generate the dataframe. How can I do this in pandas?

1
  • 3
    df=pd.json_normalize(data) ? Commented Dec 6, 2022 at 12:53

1 Answer 1

1

You can use pd.json_normalize() to get your desired output:

import pandas as pd

data = [
{
    "Data": {
        "SensorId": "EF00BFC0",
        "SensorName": "DEVICE 05",
        "TimeStamp": 3877552823,
    },
    "ThresholdValue": {
        "SensorSubId": 1,
        "ThresholdMax": -1,
        "ThresholdMin": -1
    },
    "startUTC": 1668564000,
    "endUTC": 1668663000
},
{
    "Data": {
        "SensorId": "EF00BFC0",
        "SensorName": "DEVICE 05",
        "TimeStamp": 3877553446,
    },
    "ThresholdValue": {
        "SensorSubId": 1,
        "ThresholdMax": -1,
        "ThresholdMin": -1
    },
    "startUTC": 1668564000,
    "endUTC": 1668663000
}]
df = pd.json_normalize(data)
df.columns = [c.replace('Data.', '') for c in df.columns]
cols_to_keep = [c for c in df.columns if 'ThresholdValue' not in c]
df = df[cols_to_keep]
df.head()

Output:

startUTC endUTC SensorId SensorName TimeStamp
1668564000 1668663000 EF00BFC0 DEVICE 05 3877552823
1668564000 1668663000 EF00BFC0 DEVICE 05 3877553446
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.