Home > database >  Read a JSON file using pandas
Read a JSON file using pandas

Time:11-18

I'm trying to convert a JSON file into a data frame and save it to a CSV file at the end. Well, I am able to do it using Jupyter or Colab, but when I tried on my local python compiler I get many errors.

Here is my code that works on Colab

import pandas as pd
import datetime

# reading json file
df = pd.read_json(path_or_buf=CONFIG_PROPERTIES)

# normalizing json file
df_items_normalized = pd.json_normalize(data=df.orders, sep='_', record_path='items', meta=['error', 'file','order_id'])

# define parameters to save in csv
today = datetime.datetime.today().strftime('%Y_%m_%d')
path = "/output/pedidos_weedu_"   today   ".csv"

# saving to csv
df_items_normalized.to_csv(path, index=False)

And here is my code when I tried through Pycharm

import pandas as pd
import datetime
import json
import os

CWD = os.getcwd()
JSON_CONFIG_FILE_PATH = '%s/%s' % (CWD, '12-11-2021.json')

CONFIG_PROPERTIES = {}

try:
    with open(JSON_CONFIG_FILE_PATH) as data_file:
        CONFIG_PROPERTIES = json.load(data_file)
except IOError as e:
    print(e)
    print('IOError: Unable to open config.json.')
    exit(1)

print(CONFIG_PROPERTIES)

# reading json file
df = pd.read_json(path_or_buf=CONFIG_PROPERTIES)

# normalizing json file
df_items_normalized = pd.json_normalize(data=df.orders, sep='_', record_path='items', meta=['error', 'file','order_id'])

# define parameters to save in csv
today = datetime.datetime.today().strftime('%Y_%m_%d')
path = "/output/pedidos_weedu_"   today   ".csv"

# saving to csv
df_items_normalized.to_csv(path, index=False)

Here is the JSON file I'm working on

CodePudding user response:

I have changed your code a little:

import pandas as pd
import datetime
import json
import os
from pandas.io.json import json_normalize as jn

CWD = os.getcwd()
JSON_CONFIG_FILE_PATH = "%s/%s" % (CWD, "12-11-2021.json")

print(JSON_CONFIG_FILE_PATH)
CONFIG_PROPERTIES = {}

try:
    with open(JSON_CONFIG_FILE_PATH) as data_file:
        CONFIG_PROPERTIES = json.load(data_file)
except IOError as e:
    print(e)
    print("IOError: Unable to open config.json.")
    exit(1)
print(CONFIG_PROPERTIES)

df = pd.read_json(path_or_buf=JSON_CONFIG_FILE_PATH)
df_items_normalized = jn(
    data=df.orders, sep="_", record_path="items", meta=["error", "file", "order_id"]
)

# define parameters to save in csv
today = datetime.datetime.today().strftime("%Y_%m_%d")
path = "./"   today   ".csv"

# saving to csv
df_items_normalized.to_csv(path, index=False)

this line from pandas.io.json import json_normalize as jn from this answer.

I really do not understant why this answer got a negative point!

  • Related