|
| 1 | +# CSV <3 JSON Parser / Reader |
| 2 | + |
| 3 | +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 |
| 4 | + |
| 5 | +* home :: [github.com/csvreader/csvjson](https://github.com/csvreader/csvjson) |
| 6 | +* bugs :: [github.com/csvreader/csvjson/issues](https://github.com/csvreader/csvjson/issues) |
| 7 | +* gem :: [rubygems.org/gems/csvjson](https://rubygems.org/gems/csvjson) |
| 8 | +* rdoc :: [rubydoc.info/gems/csvjson](http://rubydoc.info/gems/csvjson) |
| 9 | +* forum :: [wwwmake](http://groups.google.com/group/wwwmake) |
| 10 | + |
| 11 | + |
| 12 | + |
| 13 | +## What's CSV <3 JSON? |
| 14 | + |
| 15 | +CSV <3 JSON is a Comma-Separated Values (CSV) |
| 16 | +variant / format / dialect |
| 17 | +where the line-by-line records follow the |
| 18 | +JavaScript Object Notation (JSON) encoding rules. |
| 19 | +It's a modern (simple) tabular data format that |
| 20 | +includes arrays, numbers, booleans, nulls, nested structures, comments and more. |
| 21 | +Example: |
| 22 | + |
| 23 | + |
| 24 | +``` |
| 25 | +# "Vanilla" CSV <3 JSON |
| 26 | +
|
| 27 | +1,"John","12 Totem Rd. Aspen",true |
| 28 | +2,"Bob",null,false |
| 29 | +3,"Sue","Bigsby, 345 Carnival, WA 23009",false |
| 30 | +``` |
| 31 | + |
| 32 | +or |
| 33 | + |
| 34 | +``` |
| 35 | +# CSV <3 JSON with array values |
| 36 | +
|
| 37 | +1,"directions",["north","south","east","west"] |
| 38 | +2,"colors",["red","green","blue"] |
| 39 | +3,"drinks",["soda","water","tea","coffe"] |
| 40 | +4,"spells",[] |
| 41 | +``` |
| 42 | + |
| 43 | +For more see the [official CSV <3 JSON Format documentation »](https://github.com/csvspecs/csv-json) |
| 44 | + |
| 45 | + |
| 46 | + |
| 47 | +## Usage |
| 48 | + |
| 49 | +``` ruby |
| 50 | +txt <<=TXT |
| 51 | +# "Vanilla" CSV <3 JSON |
| 52 | + |
| 53 | +1,"John","12 Totem Rd. Aspen",true |
| 54 | +2,"Bob",null,false |
| 55 | +3,"Sue","Bigsby, 345 Carnival, WA 23009",false |
| 56 | +TXT |
| 57 | + |
| 58 | +records = CsvJson.parse( txt ) ## or CSV_JSON.parse or CSVJSON.parse |
| 59 | +pp records |
| 60 | +# => [[1,"John","12 Totem Rd. Aspen",true], |
| 61 | +# [2,"Bob",nil,false], |
| 62 | +# [3,"Sue","Bigsby, 345 Carnival, WA 23009",false]] |
| 63 | + |
| 64 | +# -or- |
| 65 | + |
| 66 | +records = CsvJson.read( "values.json.csv" ) ## or CSV_JSON.read or CSVJSON.read |
| 67 | +pp records |
| 68 | +# => [[1,"John","12 Totem Rd. Aspen",true], |
| 69 | +# [2,"Bob",nil,false], |
| 70 | +# [3,"Sue","Bigsby, 345 Carnival, WA 23009",false]] |
| 71 | + |
| 72 | +# -or- |
| 73 | + |
| 74 | +CsvJson.foreach( "values.json.csv" ) do |rec| ## or CSV_JSON.foreach or CSVJSON.foreach |
| 75 | + pp rec |
| 76 | +end |
| 77 | +# => [1,"John","12 Totem Rd. Aspen",true] |
| 78 | +# => [2,"Bob",nil,false] |
| 79 | +# => [3,"Sue","Bigsby, 345 Carnival, WA 23009",false] |
| 80 | +``` |
| 81 | + |
| 82 | + |
| 83 | + |
| 84 | +### What about Enumerable? |
| 85 | + |
| 86 | +Yes, the reader / parser includes `Enumerable` and runs on `each`. |
| 87 | +Use `new` or `open` without a block |
| 88 | +to get the enumerator (iterator). |
| 89 | +Example: |
| 90 | + |
| 91 | + |
| 92 | +``` ruby |
| 93 | +csv = CsvJson.new( "1,2,3" ) ## or CSV_JSON.new or CSVJSON.new |
| 94 | +it = csv.to_enum |
| 95 | +pp it.next |
| 96 | +# => [1,2,3] |
| 97 | + |
| 98 | +# -or- |
| 99 | + |
| 100 | +csv = CsvJson.open( "values.json.csv" ) ## or CSV_JSON.open or CSVJSON.open |
| 101 | +it = csv.to_enum |
| 102 | +pp it.next |
| 103 | +# => [1,"John","12 Totem Rd. Aspen",true] |
| 104 | +pp it.next |
| 105 | +# => [2,"Bob",nil,false] |
| 106 | +``` |
| 107 | + |
| 108 | + |
| 109 | + |
| 110 | +### What about headers? |
| 111 | + |
| 112 | +Yes, you can. Use the `CsvHash` |
| 113 | +from the csvreader library / gem |
| 114 | +if the first line is a header (or if missing pass in the headers |
| 115 | +as an array) and you want your records as hashes instead of arrays of strings. |
| 116 | +Example: |
| 117 | + |
| 118 | +``` ruby |
| 119 | +txt <<=TXT |
| 120 | +"id","name","address","regular" |
| 121 | +1,"John","12 Totem Rd. Aspen",true |
| 122 | +2,"Bob",null,false |
| 123 | +3,"Sue","Bigsby, 345 Carnival, WA 23009",false |
| 124 | +TXT |
| 125 | + |
| 126 | +records = CsvHash.json.parse( txt ) |
| 127 | +pp records |
| 128 | + |
| 129 | +# => [{"id": 1, |
| 130 | +# "name": "John", |
| 131 | +# "address": "12 Totem Rd. Aspen", |
| 132 | +# "regular": true}, |
| 133 | +# {"id": 2, |
| 134 | +# "name": "Bob", |
| 135 | +# "address": null, |
| 136 | +# "regular": false}, |
| 137 | +# ... ] |
| 138 | +``` |
| 139 | + |
| 140 | +For more see the [official CsvHash documentation in the csvreader library / gem »](https://github.com/csvreader/csvreader) |
| 141 | + |
| 142 | + |
| 143 | + |
| 144 | + |
| 145 | + |
| 146 | + |
| 147 | +## License |
| 148 | + |
| 149 | +The `csvjson` scripts are dedicated to the public domain. |
| 150 | +Use it as you please with no restrictions whatsoever. |
| 151 | + |
| 152 | + |
| 153 | +## Questions? Comments? |
| 154 | + |
| 155 | +Send them along to the [wwwmake forum](http://groups.google.com/group/wwwmake). |
| 156 | +Thanks! |
0 commit comments