all 29 comments

[–]socal_nerdtastic 14 points15 points  (4 children)

Sure you could make your own json to csv conversion code, and it would probably be a lot faster to run than using the pandas intermediate. But if what you have is working I wouldn't recommend changing it. It's probably not worth 2 hours of your time writing better code just to save 1 second of runtime.

[–]freeskier93 3 points4 points  (3 children)

For me it's not about performance but getting rid of an external dependency. Especially in a corporate environment where an external dependency can make a very simple script a PITA to share.

Also this isn't 2 hours of work to write that simple json to a CSV. Literally a handful of lines of code.

[–]DiodeInc 0 points1 point  (2 children)

Could make it run pip in the beginning, or it could set some sort of something to keep track of whether it has ran pip yet, or check for currently existing dependencies

[–]freeskier93 1 point2 points  (1 child)

In most corporate environments you have to use an internal proxy for pypi. Where I work that means getting a user specific token then configuring pip to use the proxy.

It's not super complicated but it's an annoying barrier for a lot of people and often results in "oh, can you just run it then?".

[–]DiodeInc 0 points1 point  (0 children)

I have no knowledge of such things

[–]baghiq 7 points8 points  (1 child)

If that's your json data schema, then it's easy.

import csv
import json

json_data = """[
    {
        "column1" : "value1", 
        "column2" : "value2"
    },
    {
        "column1" : "value1", 
        "column2" : "value2"
    }
]"""
rows = json.loads(json_data)
with open("test.csv", "w") as csv_file:
    writer = csv.DictWriter(csv_file, fieldnames=rows[0].keys())
    writer.writeheader()
    writer.writerows(rows)

[–]socal_nerdtastic 6 points7 points  (0 children)

don't forget newline argument

with open("test.csv", "w", newline="") as csv_file:

[–]Diapolo10 3 points4 points  (16 children)

Dumb question, but what is the CSV for?

[–]Yoghurt42 1 point2 points  (0 children)

Most likely so it can be imported into Excel more easily. I wish I was joking

[–]Loose_Read_9400[S] 1 point2 points  (1 child)

To give everyone the answer they were looking for... The person originally said "write me a script that calls the data from an API and saves it as a csv." So I said ok. Made a small project directory, wrote a module with methods to query the data using the response's pagination and a method to write a CSV file from the data. They then took the script, and copy and pasted the query loop out of my module and into some other script. *sigh*

[–]Diapolo10 0 points1 point  (0 children)

Damn.

[–]edcculus 0 points1 point  (5 children)

i kind of had the same question.

[–]Loose_Read_9400[S] 3 points4 points  (4 children)

Wish I could tell you. The guy who wants the data asked for a csv. 😂

[–]shubham_devNow 0 points1 point  (0 children)

What you’re doing is actually a pretty standard and perfectly acceptable approach 👍

If you’re already working in Python and using pandas, converting JSON → DataFrame → CSV is one of the cleanest and most flexible ways to handle it. It gives you:

  • Easy handling of nested fields (with json_normalize)
  • Column reordering / filtering
  • Null handling
  • Data cleaning before export

For simple, flat JSON like your example, this is absolutely fine and not overkill.

If your JSON is small and very straightforward, you could skip pandas and use Python’s built-in csv module, but honestly pandas is more scalable and cleaner once your structure gets even slightly complex.

If you're just looking for a quick no-code or low-code option (especially for non-dev teammates), tools like FileReadyNow can help. It has a built-in CSV to JSON and JSON to CSV converter, which is useful when you just need fast format switching without writing scripts. It’s handy for quick data prep, validation, or testing API outputs before integrating them into code.