-2

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

1
  • 1
    What are the errors? Please paste the full traceback. Commented Nov 17, 2021 at 22:10

1 Answer 1

-1

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!

Sign up to request clarification or add additional context in comments.

1 Comment

It worked just fine.

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.