Skip to content Skip to sidebar Skip to footer

How To Read Json File Using Python Pandas?

I want to read json file using python pandas. Each line of the file is a complete object in JSON. I'm using below versions- python : 2.7.6 pandas: 1.19.1 json file- {'id':'111','p_

Solution 1:

first check if it's a valid json file or not using JSON validator site

once the file is in valid json format you can use the below code to read it as dataframe

with open("training.json") as datafile:
    data = json.load(datafile)
dataframe = pd.DataFrame(data)

hope this helps.

Solution 2:

read_json() can't work because of the new line after "pqr". You can either try and fix that line or try and format the whole thing into valid JSON. I'm doing the latter here by adding commas after new lines and surrounding the whole thing with brackets to form a proper JSON array:

withopen('temp.txt') as f:
    content = f.read()

pd.read_json('[' + content.replace('}\n', '},') + ']')

Post a Comment for "How To Read Json File Using Python Pandas?"