I'm building a site that, based on a user's input, sorts through JSON data and prints a schedule for them into an html table. I want to give it the functionality that once the their table is created they can export the data to a CSV/Excel file so we don't have to store their credentials (logins & schedules in a database). Is this possible? If so, how can I do it using python preferably?
Asked
Active
Viewed 126 times
0
-
2you could use pandas. https://pandas.pydata.org/docs/reference/api/pandas.read_json.html, and then export to csv: https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.to_csv.html?highlight=csv#pandas.DataFrame.to_csv – Je Je Apr 02 '21 at 21:17
-
Does this answer your question? [Read json file as pandas dataframe?](https://stackoverflow.com/questions/48614158/read-json-file-as-pandas-dataframe) Also [Writing a pandas DataFrame to CSV file](https://stackoverflow.com/q/16923281/843953) Please take the [tour], and read [ask] and [what's on-topic here](/help/on-topic). [Asking on Stack Overflow is not a substitute for doing your own research.](//meta.stackoverflow.com/a/261593/843953) – Pranav Hosangadi Apr 02 '21 at 21:23
-
`pandas` is VERY user-friendly and efficient; But, it is a very bulky library and not advisable if you are short on resources/time. prefer `csv` in that case – shripal mehta Apr 02 '21 at 21:27
2 Answers
0
This is not the exact answer but rather steps for you to follow in order to get a solution:
1 Read data from json. some_dict = json.loads(json_string)
2 Appropriate code to get the data from dictionary (sort/ conditions etc) and get that data in a 2D array (list)
3 Save that list as csv: https://realpython.com/python-csv/

hrsh
- 108
- 5
-
refer official documentations from https://docs.python.org/3/library/csv.html#csv.writer – shripal mehta Apr 02 '21 at 21:22
0
I'm pretty lazy and like to utilize pandas
for things like this. It would be something along the lines of
import pandas as pd
file = 'data.json'
with open(file) as j:
json_data = json.load(j)
df = pd.DataFrame.from_dict(j, orient='index')
df.to_csv("data.csv")

Teejay Bruno
- 1,716
- 1
- 4
- 11
-
1Why not simply [`pd.read_json()`](https://pandas.pydata.org/docs/reference/api/pandas.read_json.html)? – Pranav Hosangadi Apr 02 '21 at 21:22
-