2

I have multiple json files in a folder. I had implemented a way to catch only .json files in this folder. My problem is: I need to extract some information contained in each of those files but it didn't work the way I expected. I need to find a way to get this information and convert all into a pandas dataframe.

The variable jsons_data contains all .json files

jsons_data = json_import(path_to_json)

for index, js in enumerate(jsons_data):
    with open(os.path.join(path_to_json, js)) as json_file:
        data = json.loads(json_file)

print(data)

1 Answer 1

3

The problem in your code is that on every iteration you override the content of data.

I assume you want to create on big dataframe from all the files in that case you can do -

dataframes = []
for js in jsons_data:
    dataframes.append(pd.read_json(os.path.join(path_to_json, js)))
df = pd.concat(dataframes)

See documentation about read_json - https://pandas.pydata.org/pandas-docs/version/1.3.0rc1/reference/api/pandas.read_json.html

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

10 Comments

Yes, that is what i want to do but i tried you code and didn't worked, instead of this had returned an error "all arrays must be of the same length". Here it's a image showing the list o .json files i have imgur.com/a/mNpallS
Do all the json files have the same format?
Yes, all have the same format
It is hard for me to answer without having a look in the data itself but I tend to think that one of the files might have an extra column or something like that
|

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.