Code Monkey home page Code Monkey logo

csvjson's People

Contributors

drorharari avatar martindrapeau avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

csvjson's Issues

CSVJSON Parser in Ruby (in the public domain)

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.

Your last example has typos

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

Document some use cases

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.