drorharari / csvjson Goto Github PK
View Code? Open in Web Editor NEWThe one CSV format to rule them all - simple, expressive, precise.
License: MIT License
The one CSV format to rule them all - simple, expressive, precise.
License: MIT License
Hello,
Thanks for the write-up and the fantastic idea. Your format inspired the new CSV <3 JSON format https://github.com/csvspecs/csv-json (it differs by including comments and by restricting headers and so on.
Anyways, here's a working CSVJSON parser in ruby (it's public domain), use it as you please:
require 'pp'
require 'json'
require 'stringio'
class CsvJson
include Enumerable
def initialize( data )
if data.is_a?( String )
@input = data # note: just needs each for each_line
else ## assume io
@input = data
end
end
def each( &block )
@input.each_line do |line|
## note: chomp('') if is an empty string,
## it will remove all trailing newlines from the string.
line = line.chomp( '' )
next if line.empty? ## skip blank lines
## note: auto-wrap in array e.g. with []
json = JSON.parse( "[#{line}]" )
block.call( json )
end
end
end # class CsvJson
And use like:
csv = CsvJson.new( <<TXT ) # from a string
1,"John","12 Totem Rd. Aspen",true
2,"Bob",null,false
3,"Sue","Bigsby, 345 Carnival, WA 23009",false
TXT
# -or-
csv = CsvJson.new( File.open( "test.csvj" ) ) # from a file (io)
table = csv.to_a
puts
puts "data:"
pp table
will print:
data:
[[1, "John", "12 Totem Rd. Aspen", true],
[2, "Bob", nil, false],
[3, "Sue", "Bigsby, 345 Carnival, WA 23009", false]]
All the best. Keep it up. Cheers. Prost.
Would be really great to get a few examples along with this spec.
Your last example has errors. First, the header are not strings (invalid JSON). Second, you have unescaped double quotes send last line, surrounding "Hi!".
This is valid:
"index","value1","value2"
"number",1,2
"boolean",false,true
"null",null,"non null"
"array of numbers",[1],[1,2]
"simple object",{"a": 1},{"a":1, "b":2}
"array mixed objects",[1,null,"ball"],[2,{"a": 10, "b": 20},"cube"]
"string with quotes","a\"b","alert(\"Hi!\")"
"string with bell&newlines","bell is \u0007","multi\nline\ntext"
Validated with: https://www.csvjson.com/csvjson2json
I really like this! Its not exactly what I am looking for but I thought you might want this which works with your examples:
https://gist.github.com/gecBurton/f0a05e2d1a66e8d3e5fee814cea8fd1e
Good luck!
George
It would be nice for you to document some of the situations where you use the CSVJSON format and provide some samples. Others could benefit from that.
When converting CSV or Tab-delimited files to JSON, empty rows at the end of a file cause a single entry of null-valued properties to be created, e.g. , { "name": null, "TITLE": null, "ARTIST": null, "COUNTRY": null, "COMPANY": null, "PRICE": null, "YEAR": null }
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.