How to read JSON file in Python

json

#1

Hey All,
I want to read JSON file, can someone tell what is wrong in the below code:

import numpy as np
import pandas as pd
import zipfile
import json
from pandas.io.json import json_normalize

JSON_COLUMNS = ['device', 'geoNetwork', 'totals', 'trafficSource']
df1 = pd.read_csv(zipfile.ZipFile('t2.zip').open('t2.csv'), 
                     converters={column: json.loads for column in JSON_COLUMNS},dtype={'fullVisitorId': 'str'})
for column in JSON_COLUMNS:
        column_as_df = json_normalize(df1[column])
        column_as_df.columns = [f"{column}.{subcolumn}" for subcolumn in column_as_df.columns]
        df1 = df1.drop(column, axis=1).merge(column_as_df, right_index=True, left_index=True)
df1.head()

#2

I suggest to first try to unzip the file and just use the default pandas JSON reader, if the file is already in JSON format.

import pandas as pd
DF = pd.read_json(‘Complete_file_name’)


#3

Hey Akash,

Thank you for your response.
I tried:

 zf = zipfile.ZipFile('Customer_Revenue.zip')
 DF = pd.read_json(zf.open('train.csv'))
 DF.head

and i got :

ValueError: Expected object or value.

Actually due to large file size, i zipped the file and upload on cloud and i want to access the file.