Discussion:
csvjson library / gem v1.0 - read tabular data in the CSV <3 JSON format, that is, comma-separated values CSV (line-by-line) records with JSON encoding rules
Gerald Bauer
2018-10-15 13:20:40 UTC
Permalink
Hello,

I've published a new library / gem, that is, csvjon that lets you
read tabular data in the (new) CSV <3 JSON format, that is,
comma-separated values CSV (line-by-line) records with javascript
object notation (JSON) encoding rules.

What's CSV <3 JSON?

CSV <3 JSON is a Comma-Separated Values (CSV)
variant / format / dialect
where the line-by-line records follow the
JavaScript Object Notation (JSON) encoding rules.
It's a modern (simple) tabular data format that
includes arrays, numbers, booleans, nulls, nested structures, comments and more.
Example:

# "Vanilla" CSV <3 JSON

1,"John","12 Totem Rd. Aspen",true
2,"Bob",null,false
3,"Sue","Bigsby, 345 Carnival, WA 23009",false

or


# CSV <3 JSON with array values

1,"directions",["north","south","east","west"]
2,"colors",["red","green","blue"]
3,"drinks",["soda","water","tea","coffe"]
4,"spells",[]


And how to read / parse in ruby? Example:

txt <<=TXT
# "Vanilla" CSV <3 JSON

1,"John","12 Totem Rd. Aspen",true
2,"Bob",null,false
3,"Sue","Bigsby, 345 Carnival, WA 23009",false
TXT

records = CsvJson.parse( txt ) ## or CSV_JSON.parse or CSVJSON.parse
pp records
# => [[1,"John","12 Totem Rd. Aspen",true],
# [2,"Bob",nil,false],
# [3,"Sue","Bigsby, 345 Carnival, WA 23009",false]]

# -or-

records = CsvJson.read( "values.json.csv" ) ## or CSV_JSON.read or
CSVJSON.read
pp records
# => [[1,"John","12 Totem Rd. Aspen",true],
# [2,"Bob",nil,false],
# [3,"Sue","Bigsby, 345 Carnival, WA 23009",false]]

# -or-

CsvJson.foreach( "values.json.csv" ) do |rec| ## or
CSV_JSON.foreach or CSVJSON.foreach
pp rec
end
# => [1,"John","12 Totem Rd. Aspen",true]
# => [2,"Bob",nil,false]
# => [3,"Sue","Bigsby, 345 Carnival, WA 23009",false]

and so on.

Happy data wrangling with ruby (and csv and json). Cheers. Prost.

[1] https://github.com/csv11/csvjson - the csvjson library (reader / parser)
[2] https://github.com/csv11/csv-json - the CSV <3 JSON format

Unsubscribe: <mailto:ruby-talk-***@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>
Ivo Herweijer
2018-10-16 11:49:39 UTC
Permalink
Post by Gerald Bauer
Hello,
I've published a new library / gem, that is, csvjon that lets you
read tabular data in the (new) CSV <3 JSON format, that is,
comma-separated values CSV (line-by-line) records with javascript
object notation (JSON) encoding rules.
[1] https://github.com/csv11/csvjson - the csvjson library (reader / parser)
[2] https://github.com/csv11/csv-json - the CSV <3 JSON format
Didn't even know the csvjson format existed. I'm sure this will come in
handy in the future.

Had a quick glance at the code. Looks like this thing won't explode
memory wise when trying to read a huge csv file as long as you stick to
the foreach method.

Anyway, thanks thanks thanks for this gem.

Cheers,

Ivo


Unsubscribe: <mailto:ruby-talk-***@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>

Loading...