A lightweight and efficient JSON to CSV converter written in Go. It reads a JSON array of objects, dynamically extracts headers, and writes correct tabular CSV outputβideal for data processing workflows and automation scripts.
- Overview
- Features
- Requirements
- Installation
- Usage
- Code Structure
- Error Handling
- Contributing
- License
This Go-based CLI toolβcomplete with reusable package codeβautomatically:
- Finds the first JSON file in the working directory
- Parses it into a slice of Go maps
- Extracts dynamic headers from keys
- Converts and exports as a
.csvfile - Supports both direct
go runusage andgo buildfor installing the binary :contentReference[oaicite:1]{index=1}
- π Auto-detection: Identifies the first
.jsonfile in the directory :contentReference[oaicite:2]{index=2} - π§© Dynamic Headers: Builds CSV headers based on JSON keys from first entry :contentReference[oaicite:3]{index=3}
- πΎ CSV Export: Writes a
.csvfile with the same base name as input :contentReference[oaicite:4]{index=4} - π« Handles Missing Fields: Missing keys render as empty CSV values :contentReference[oaicite:5]{index=5}
- π¨ Error Reporting: Simple messages for missing files or malformed JSON :contentReference[oaicite:6]{index=6}
- Go 1.18+ (modules enabled)
- No external dependenciesβuses Go standard
encoding/jsonandencoding/csv
git clone https://github.com/MisaghMomeniB/JSON2CSV-Go.git
cd JSON2CSV-Go/src
go build -o json2csv main.gogo run src/main.goPlace a JSON file (array of objects) in your folder and execute:
# e.g. data.json β data.csv
./json2csvThe tool reads *.json, converts it to CSV, and writes *.csv.
JSON2CSV-Go/
βββ src/
β βββ main.go # CLI entrypoint and conversion logic
βββ README.md # This file
βββ LICENSE # MIT License
-
main.go:- Finds JSON file via
filepath.Glob("*.json") - Loads JSON into
[]map[string]interface{} - Extracts keys from first element
- Outputs CSV using
encoding/csv
- Finds JSON file via
- Exits with a message if no JSON files found
- Reports parsing errors for invalid JSON
- Gracefully handles write failures
Improvements welcome! Consider adding:
- Support for nested JSON arrays or pointers
- CLI flags (e.g., custom file paths, headers)
- Batch file processing
To contribute:
- Fork the repo
- Create a feature branch
- Submit a PR with clear descriptions
Distributed under the MIT License. See LICENSE for details.