This how I parsed multiple json files in a single list
base_dir = 'jsons_final_folder/'
data_list = []
for file in os.listdir(base_dir):
if 'json' in file:
json_path = os.path.join(base_dir, file)
json_data = pd.read_json(json_path, lines=True)
data_list.append(json_data)
And I got a list that look like this
print(data_list)
output:
[ 0
0 {"general":{"key":"value","q":"..., 0
0 {"general":{"key":"value","q":"..., 0
0 {"general":{"key":"value","q":"..., 0
0 {"general":{"key":"value","q":"..., 0
0 {"general":{"key":"value","q":"..., 0
0 {"general":{"key":"value","q":"..., 0
0 {"general":{"key":"value","q":"...,] 0
So this is my code to convert df
with open("f.csv","w") as f:
wr = csv.writer(f)
wr.writerow(data_list)
But I get a df that type pandas.core.frame.DataFrame like this:
{"general":{"key":"value","q":"…, | {"general":{"key":"value","q":"…, | {"general":{"key":"value","q":"…, | {"general":{"key":"value","q":"…, |
---|
with shape of n columns and 0 rows
What I am trying to do here is to make a df out of this list that contains only jsons with specific queries but i don’t what’s the problem.
I also tried to add dilimiter
I wanted the final shape be look like this
json |
---|
{"general":{"key":"value","q":"…, |
{"general":{"key":"value","q":"…, |
Thank you
2
Answers
Guys I found the solution from This video
function to return files
Here we read each file and import it into list
here we convert it into a df
And then save list to csv
Thanks for helping out
Have you tried
df = pd.DataFrame({'json': data_list})
?