Skip to content

Bank-safe, standard-library Python CLI that converts JSON → CSV with schema auto-detection—built for trading support & FO/MO data ops.

Notifications You must be signed in to change notification settings

kgkswapan/json-to-csv-converter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

json-to-csv (standard library only)

Convert JSON to CSV with sane defaults. Built for locked-down enterprise desktops (no third-party deps).

Why this exists

  • Bank-friendly: 100% Python standard library (argparse, json, csv, logging).
  • Practical: Handles both dict-of-dicts and list-of-dicts JSON.
  • Usable: Good CLI UX, helpful errors, stable field inference, optional --fields.

Quick start

python json_to_csv.py -h

usage: json_to_csv.py [-h] [--output OUTPUT] [--fields FIELDS]
                      [--encoding ENCODING] [--delimiter DELIMITER] [--verbose]
                      input_json

Basic usage

Auto-writes next to input as data.csv

python json_to_csv.py data.json

Explicit output path

python json_to_csv.py data.json -o result.csv

Force field order (columns)

python json_to_csv.py data.json --fields id,name,role

Verbose logs

python json_to_csv.py data.json -v

Windows/Excel users: in some locales Excel expects ; as separator

python json_to_csv.py data.json --delimiter ";"

Supported input shapes

Dict of Dicts

{
  "1": {"name": "Alice", "role": "Admin"},
  "2": {"name": "Bob",   "role": "User" }
}

# output
id,name,role
1,Alice,Admin
2,Bob,User

List of Dicts

[
  {"id": "1", "name": "Alice", "role": "Admin"},
  {"id": "2", "name": "Bob",   "role": "User" }
]

Options

  • output, -o Path to output CSV. Defaults to .csv.
  • fields Comma-separated list of columns (order enforced). Extra JSON keys are ignored.
  • encoding Text encoding (default: utf-8).
  • delimiter CSV delimiter (default: ,).
  • verbose, -v Verbose logging.

Logging & exit codes

  • INFO/DEBUG logs go to stdout/stderr depending on level. Use -v for debugging.
  • Exit codes:
Code Meaning
0 Success
1 OS errors (file not found/permissions)
2 Data/usage errors (bad JSON, bad shape)
99 Unexpected exception

Design Notes

  • No third-party modules. Safe for restricted environments.
  • Deterministic columns. If --fields is omitted, columns are inferred in a stable order.
  • Windows-safe CSV. Uses newline='' to avoid blank lines.
  • Performance: Loads JSON into memory. For very large JSON files, consider chunking upstream or NDJSON (future enhancement).

Examples

Force a specific column set (drops others silently):

python json_to_csv.py data.json --fields id,name,role

Change delimiter to semicolon (common in EU Excel):

python json_to_csv.py data.json --delimiter ";"

Custom encoding:

python json_to_csv.py data.json --encoding cp1252

Security & compliance

  • Pure read → transform → write. No network calls, no eval/exec.
  • Predictable I/O and explicit error messages for auditability.

License

MIT

About

Bank-safe, standard-library Python CLI that converts JSON → CSV with schema auto-detection—built for trading support & FO/MO data ops.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages