Skip to content

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

 
 
 

Contributors

Languages