I have to convert json files as I said, here is the code:enter image description here
def AnalysisJson():
file_path = 'my_file'
for root,dirs,files in os.walk(file_path):
for file in files:
InputPath = open(file_path '\\' file, encoding="utf-8")
for i in files:
df = json.load(InputPath)
demo = pd.json_normalize(df,record_path = 'label_annotations')
demo.to_csv('files.csv')
JSONDecodeError: Expecting value: line 1 column 1 (char 0)
I want to convert these files, if the code is surely hard to run, I wish someone will give me an advice, thanks!
CodePudding user response:
I am not sure that I understand correctly what you want, but here is an answer based on my interpretation of your question.
import json
import os
from glob import glob
import pandas as pd
def json_to_csv(dir_path: str) -> None:
for file_path in glob(os.path.join(dir_path, '*.json')):
with open(file_path, encoding='utf-8') as f:
data = json.load(f)
df = pd.json_normalize(data, record_path='label_annotations')
df.to_csv(file_path.replace('.json', '.csv'), index=False)