Skip to content

A Simple Go Program That Converts a Json File to a Csv File. It Reads a Json Array of Objects, Extracts Keys as Headers, and Writes the Data Into a Csv Format.

Notifications You must be signed in to change notification settings

MisaghMomeniB/JSON2CSV-Go

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

13 Commits
Β 
Β 
Β 
Β 

Repository files navigation

🧰 JSON2CSV-Go

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.


πŸ“‹ Table of Contents

  1. Overview
  2. Features
  3. Requirements
  4. Installation
  5. Usage
  6. Code Structure
  7. Error Handling
  8. Contributing
  9. License

πŸ’‘ Overview

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 .csv file
  • Supports both direct go run usage and go build for installing the binary :contentReference[oaicite:1]{index=1}

βœ… Features

  • πŸ” Auto-detection: Identifies the first .json file 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 .csv file 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}

🧾 Requirements

  • Go 1.18+ (modules enabled)
  • No external dependenciesβ€”uses Go standard encoding/json and encoding/csv

βš™οΈ Installation

Local build

git clone https://github.com/MisaghMomeniB/JSON2CSV-Go.git
cd JSON2CSV-Go/src
go build -o json2csv main.go

Run directly

go run src/main.go

πŸš€ Usage

Place a JSON file (array of objects) in your folder and execute:

# e.g. data.json β†’ data.csv
./json2csv

The tool reads *.json, converts it to CSV, and writes *.csv.


πŸ“ Code Structure

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

⚠️ Error Handling

  • Exits with a message if no JSON files found
  • Reports parsing errors for invalid JSON
  • Gracefully handles write failures

🀝 Contributing

Improvements welcome! Consider adding:

  • Support for nested JSON arrays or pointers
  • CLI flags (e.g., custom file paths, headers)
  • Batch file processing

To contribute:

  1. Fork the repo
  2. Create a feature branch
  3. Submit a PR with clear descriptions

πŸ“„ License

Distributed under the MIT License. See LICENSE for details.

About

A Simple Go Program That Converts a Json File to a Csv File. It Reads a Json Array of Objects, Extracts Keys as Headers, and Writes the Data Into a Csv Format.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages