I have around 800 json files in a folder , i want to convert all those files to its csv formats.And these must be done without specifying the names of the file.My json files are very nested so need an idea to convert it to its csv formats.
Sample of json file
[
{
'Extract_date': '2019-06-04'
},
{
'lat,lon': '0,0'
},
{
'current': [
{
'source': 'sg',
'value': 99.04
},
{
'source': 'meto',
'value': 99.04
}
],
'swell': [
{
'source': 'sg',
'value': 192.28
},
{
'source': 'noaa',
'value': 201.69
},
],
'Height': [
{
'source': 'sg',
'value': 0.4
},
{
'source': 'noaa',
'value': 0.36
},
],
'time': '2019-06-04T00:00:00+00:00',
'wind': [
{
'source': 'sg',
'value': 153.43
},
{
'source': 'noaa',
'value': 156.89
},
{
'source': 'icon',
'value': 153.43
}
],
'Speed': [
{
'source': 'sg',
'value': 4.92
},
{
'source': 'noaa',
'value': 5.46
},
{
'source': 'icon',
'value': 4.92
}
]}]
The code should retrieve the json files from the folder and then generate the csv files for the json files that is i need the flattened structure.
the code which i tried is just for one file but it displays the data as just one row.
import os
import pandas as pd
import json
import numpy as np
data = []
os.chdir('/home/Documents/04.06.2019')
with open('new.json', encoding="utf8") as data_file:
for line in data_file:
data.append(json.loads(line))
dataframe = pd.DataFrame(data)
dataframe.to_csv("filename.csv", encoding='utf-8',index= False)