Home > Software engineering >  How to convert JSON file into EXCEL file in python
How to convert JSON file into EXCEL file in python

Time:01-21

I have 2 questions

  1. How to convert and extract JSON file into EXCEL file in python
  2. How to combine all json file into one file?

Now, I have 30 json files. I would like to extract them all into EXCEL file (In readable format).

Lastly, I need to combine all of the result into one excel file. So, curious on how to do that too.

CodePudding user response:

You can try to use this library,

https://pypi.org/project/tablib/0.9.3/

It provides a lot of features that can help you on this.

CodePudding user response:

Converting JSON into EXCEL;

import pandas as pd

df = pd.read_json('./file1.json')
df.to_excel('./file1.xlsx')

Combining multiple EXCELs (two files are combined in the example);

import glob
import pandas as pd
  
excl_list_path = ["./file1.xlsx", "./file2.xlsx"]
excl_list = []

for file in excl_list_path:
    excl_list.append(pd.read_excel(file))

excl_merged = pd.DataFrame()
  
for excl_file in excl_list:
    excl_merged = excl_merged.append(
      excl_file, ignore_index=True)
  
excl_merged.to_excel('file1-file2-merged.xlsx', index=False)

Note; Your specific JSON file structure is important for these examples...

CodePudding user response:

And I have the perfect function just for that

import pandas as pd

def save_to_excel(json_file, filename):
        df = pd.read_json(json_file).T
        df.to_excel(filename)

json_data = {"a": "data A", "b": "data B"}
save_to_excel(json_data, "json_data.xlsx")

More info here

  •  Tags:  
  • Related