Search code examples
pythonjsonpandasdataframejsonlines

Read JSON file with multiple objects inside Python


I am trying to read a json file in Python and convert it into a dataframe. The problem is that my json file has several json objects inside. The structure of my json is like this:

{"Temp":"2,3", "OutsideTemp" : "3,4",...}
{"Temp":"3,2", "OutsideTemp" : "4,4",...}
{"Temp":"2,8", "OutsideTemp" : "3,7",...}
...

I've tried using json lines and pandas.read_json but only got errors. (I'm noob at python as you can see, help me!)


Solution

  • import pandas as pd
    
    df = pd.read_json('data.txt', lines=True)
    df = df.apply(lambda x:x.str.replace(',','.')).astype(float)
    print(df.info())
    df
    
    <class 'pandas.core.frame.DataFrame'>
    RangeIndex: 3 entries, 0 to 2
    Data columns (total 2 columns):
    Temp           3 non-null float64
    OutsideTemp    3 non-null float64
    dtypes: float64(2)
    memory usage: 176.0 bytes
    None
    

    Output