Have you ever faced difficulty with conversions from JSON to CSV or CSV to JSON?
JSON makes a programmer’s life easy, while CSV makes an accountant’s life easy. But when we’re working across teams or systems, we often need both formats.
That’s where conversions come in.
Sure, there are plenty of online tools available. It’s convenient — but let’s pause for a second...
What about data privacy?
Is it really okay to upload sensitive or internal data to a public tool?
We’re developers — we can solve this ourselves.
And the best part?
You don’t need a full-blown runtime, environment, or server.
Just a simple Bash script + PHP (hey, PHP isn’t dead yet 😉).
JSON to CSV
php -r '$data = json_decode(file_get_contents("path/input.json"), true);$fp = fopen("output.csv", "w");fputcsv($fp, array_keys($data[0]));foreach ($data as $row) fputcsv($fp, $row);fclose($fp);'
CSV to JSON
php -r '$csv = array_map("str_getcsv", file("path/input.csv"));$headers = array_shift($csv);$data = array_map(fn($row) => array_combine($headers, $row), $csv);file_put_contents("path/output.json", json_encode($data, JSON_PRETTY_PRINT));'
➕Bonus: XLSX to JSON
We can’t convert .xlsx directly to JSON — first, we convert it to CSV using LibreOffice:
libreoffice --headless --convert-to csv input.xlsx --outdir . && mv input.csv desired_name.csv
Then reuse the CSV to JSON step above.
Top comments (0)