Skip to content Skip to sidebar Skip to footer

Saving Json Files Into One Single Csv

I have 100s of similar json files and I want to save the contents of these json files into one single csv file. This is the code I wrote for the same. But it's not doing what I wan

Solution 1:

Your solution is quite close to the desired output, you just need to transpose the imported json:

import glob
directory = "your/path/to/jsons/*.json"
df = pd.concat([pd.read_json(f, orient="index").T for f in glob.glob(directory)], ignore_index=True)

Aferwards you can save the df using df.to_csv("tweets.csv")
Hopefully that helps you!


Solution 2:

list_=['politifact13565', 'politifact13601'] 
for i in list_:
    with open("{}/news content.json".format(i)) as json_input:
        json_data = json.load(json_input, strict=False)
        mydict = {}
        mydict["url"] = json_data["url"]
        mydict["text"] = json_data["text"]
        mydict["images"]=json_data["images"]
        mydict["title"]=json_data["title"]
        df = pd.DataFrame.from_dict(mydict, orient='index')
        df = df.T
        df.append(df, ignore_index=True)
        df.to_csv('out.csv', mode='a', header=False)
        print(df)

Post a Comment for "Saving Json Files Into One Single Csv"