Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

csvjson

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

csvjson

  • 1.0.1
  • Rubygems
  • Socket score

Version published
Maintainers
1
Created
Source

CSV <3 JSON Parser / Reader

csvjson library / gem - read tabular data in the CSV <3 JSON format, that is, comma-separated values CSV (line-by-line) records with javascript object notation (JSON) encoding rules

  • home :: github.com/csvreader/csvjson
  • bugs :: github.com/csvreader/csvjson/issues
  • gem :: rubygems.org/gems/csvjson
  • rdoc :: rubydoc.info/gems/csvjson
  • forum :: wwwmake

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",[]

For more see the official CSV <3 JSON Format documentation »

Usage

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]

What about Enumerable?

Yes, the reader / parser includes Enumerable and runs on each. Use new or open without a block to get the enumerator (iterator). Example:

csv = CsvJson.new( "1,2,3" )   ## or CSV_JSON.new or CSVJSON.new
it  = csv.to_enum
pp it.next  
# => [1,2,3]

# -or-

csv = CsvJson.open( "values.json.csv" )  ## or CSV_JSON.open or CSVJSON.open
it  = csv.to_enum
pp it.next
# => [1,"John","12 Totem Rd. Aspen",true]
pp it.next
# => [2,"Bob",nil,false]

What about headers?

Yes, you can. Use the CsvHash from the csvreader library / gem if the first line is a header (or if missing pass in the headers as an array) and you want your records as hashes instead of arrays of strings. Example:

txt <<=TXT
"id","name","address","regular"
1,"John","12 Totem Rd. Aspen",true
2,"Bob",null,false
3,"Sue","Bigsby, 345 Carnival, WA 23009",false
TXT

records = CsvHash.json.parse( txt )
pp records

# => [{"id":      1,
#      "name":    "John",
#      "address": "12 Totem Rd. Aspen",
#      "regular": true},
#     {"id":      2,
#      "name":    "Bob",
#      "address": null,
#      "regular": false},
#    ... ]

For more see the official CsvHash documentation in the csvreader library / gem »

License

The csvjson scripts are dedicated to the public domain. Use it as you please with no restrictions whatsoever.

Questions? Comments?

Send them along to the wwwmake forum. Thanks!

FAQs

Package last updated on 26 Nov 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc