• Stars
    star
    167
  • Rank 226,635 (Top 5 %)
  • Language
    Ruby
  • License
    MIT License
  • Created almost 15 years ago
  • Updated almost 9 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A gem useful for converting CSV files to JSON from command-line

csv2json

Clients were sending me XLS files but my webs consume JSONs. So I needed to convert them to JSON easily from command-line...

Solution

  • export XLS as a CSV file (I use OpenOffice.org for this)
  • run csv2json file.csv > file.json (or json2csv file.json > file.csv)
  • there is no step 3

Sample

note: make sure your XLS table has the first row with column names, or use the -H option to provide some

This CSV file:

thumbpath,imgpath,imgsrc,width,height,thumbsrc,thumbwidth,thumbheight
thumbnails/,images/,paris_01.jpg,350,262,paris_01.jpg,75,56
thumbnails/,images/,paris_02.jpg,262,350,paris_02.jpg,75,56

gets turned into this JSON:

[
  {
    "thumbwidth": 75,
    "imgsrc": "paris_01.jpg",
    "thumbsrc": "paris_01.jpg",
    "height": 262,
    "imgpath": "images/",
    "thumbheight": 56,
    "thumbpath": "thumbnails/",
    "width": 350
  },
  {
    "thumbwidth": 75,
    "imgsrc": "paris_02.jpg",
    "thumbsrc": "paris_02.jpg",
    "height": 350,
    "imgpath": "images/",
    "thumbheight": 56,
    "thumbpath": "thumbnails/",
    "width": 262
  }
]

Installation

sudo gem install csv2json

Usage

Usage: csv2json [INPUT] [OPTIONS]

Specific options:
    -s, --separator SEP              Set separator character surrounded by single quotes (default is ',')
    -o, --output FILE                Write output to a file
    -p, --pretty                     Pretty-format JSON output
    -k, --skip-headers-from-file     Ignore the headers (first line) in the file; use with --headers to set your own
    -H, --headers HEADERS            Supply list of headers, where no headers exist in the file, or where you're using -k to ignore them
    -h, --help                       Show this message
    -v, --version                    Show version

We also provide json2csv, which converts in the opposite direction.

Usage: json2csv [INPUT] [OPTIONS]

Specific options:
    -s, --separator SEP              Set separator character surrounded by single quotes (default is ',')
    -o, --output FILE                Write output to a file
    -H, --headers HEADERS            Supply sorted list of headers, by which to order the columns in the CSV. These must match the key names in the JSON.
    -h, --help                       Show this message
    -v, --version                    Show version

Note: JSON field order is explicitly unsorted, so if you want predictable field order for CSV output, use the -H option.

Alternative usage

common usage is csv2json file.csv > file.json

csv2json should behave like proper unix command-line utility working with pipes, redirects, etc.

cat file.csv | csv2json | gzip > file.json.gz

Usage as a library

with files

require 'csv2json'

File.open('input.csv', 'r') do |input|
    File.open('output.json', 'w') do |output|
        CSV2JSON.parse(input, output)
    end
end

or in-memory

require 'csv2json'

input = StringIO.new(csv_string)
output = StringIO.new()
CSV2JSON.parse(input, output)

output.pos = 0
puts output.read

Authors

https://github.com/darwin/csv2json/graphs/contributors

More Repositories

1

cmx.js

[prototype] A library/markup for building xkcd-style comic strips
JavaScript
793
star
2

plastic

An experimental structural editor for ClojureScript
JavaScript
147
star
3

otx

git svn clone http://otx.osxninja.com/builds
Objective-C
59
star
4

lnd-auto-backup

A simple automatic backup service for your LND node
Shell
43
star
5

blender-clojure

[EXPERIMENTAL] Blender scripting with ClojureScript. Live-coding Blender environment using modern cljs tools.
Clojure
32
star
6

simverse

Need a LN simnet? This tool can generate one for you.
Shell
29
star
7

xrefresh-server

OSX filesystem monitor for XRefresh
Ruby
19
star
8

cmx.io

A comix editor using cmx.js
JavaScript
17
star
9

fbjqry

A jQuery-like library for Facebook FBJS
JavaScript
15
star
10

terraform

Terraforming static websites
JavaScript
15
star
11

faceboard

An online photo board for groups
JavaScript
13
star
12

hlink

hard-link directories on OSX (with HFS+)
C
11
star
13

move-wiki

GitHub wiki migration tasks
8
star
14

macruby-irb

Empty MacRuby application with IRB-like console
Ruby
8
star
15

trezor-mac-toolkit

A macOS toolkit for Trezor
Shell
8
star
16

shortcutrecorder

git svn clone http://shortcutrecorder.googlecode.com/svn/trunk/
Objective-C
7
star
17

firebug.lite

git svn clone http://fbug.googlecode.com/svn/lite/branches/firebug1.3
JavaScript
5
star
18

termite

Terminal output post-processor
Ruby
5
star
19

dino2

Neverending Story (a html5 game powered by impactjs)
JavaScript
5
star
20

inferno

Research on raytracing using modern GPUs (2004)
4
star
21

breakpad

git svn clone -s http://google-breakpad.googlecode.com/svn
C++
4
star
22

textyourmom

Never forget to Text Your Mom
Swift
4
star
23

simbl

Simple Bundle Loader
Objective-C
4
star
24

21days

ipo48
JavaScript
4
star
25

termite.pdb

Termite addon for python commandline debugger
Ruby
3
star
26

termite.textmate

Termite addon for textmate integration
Ruby
3
star
27

firecookie

git svn clone http://firecookie.googlecode.com/svn/trunk/ firecookie
JavaScript
3
star
28

dialogflow

JavaScript
3
star
29

pacwars2

A remake of old PacWars game
C++
3
star
30

upgradr

GreaseMonkey for IE (unfinished)
2
star
31

scraper

a simple web-scraper
Go
2
star
32

dino

Dino needs love (html5 game powered by impactjs)
JavaScript
2
star
33

shadow-popup-repro

HTML
2
star
34

termite.bash

Termite addon for bash
Ruby
2
star
35

silverstunts

Simple game prototype driven by SilverLight
JavaScript
2
star
36

miniboost

stripped down version of boost library for my projects
C++
1
star
37

antonin.hildebrand.cz

My OpenID
1
star
38

boost

My personal boost mirror to be submoduled by my projects
C++
1
star
39

darwin.github.com

Home, sweet home ...
HTML
1
star
40

cljsbuild-once-auto-repro

a repro case for problematic cljsbuild invocation
Clojure
1
star
41

marshals-web

a minimalist website for Marshals demogroup (Jekyll)
HTML
1
star