Using CSV to fill DataTable

Using CSV to fill DataTable

epschroederepschroeder Posts: 5Questions: 0Answers: 0
edited July 2011 in General
Hello all,

I have searched the forum but have not found an answer yet so I hope someone can help me.
I want to use a csv file to fill my DataTable. I have read that I have to convert it to JSON but have not found out how.
I have no experience with JSON so please help me out!

Replies

  • fbasfbas Posts: 1,094Questions: 4Answers: 0
    I was actually writing up plans for an app or wizard, part of which would covert between csv, json, html, or database tables. This would be the first step in the wizard for customizing a fast deployment of a DataTables page or set of pages.

    I'd be a little surprised if you couldn't find something out there on the internet that does this. I found a site that will convert csv to an HTML table - I was planning on using this in my app: http://area23.brightbyte.de/csv2wp.php there were several others, but this was the best that I had seen, and is downloadable to run on your own site.

    Just a google search away are tools to convert csv to json: http://www.zenovations.com/misc/convertcsv/
  • epschroederepschroeder Posts: 5Questions: 0Answers: 0
    Hi fbas,

    thank you for your reply.
    I did find the tools on the internet but they are not what I'm looking for.
    What I want DataTables to do is convert csv to JSON on the fly.
    The client I am writing this webapp for can only upload .csv files from their software automatically so a conversion tool won't be a solution.

    Hope you or someone else can still help me out.
  • fbasfbas Posts: 1,094Questions: 4Answers: 0
    some of those links include javascript code for turning csv into json. you can use that code or at least get ideas from it.
  • GregPGregP Posts: 500Questions: 10Answers: 0
    You can probably also find tonnes of sample code (or even native methods) in server-side languages for converting CSV to JSON. It's an extra leg of travel (user ups the CSV to the server, which returns the JSON instead of just converting it in the browser) but it should still be a lightweight process.
  • epschroederepschroeder Posts: 5Questions: 0Answers: 0
    Thank you for the tips guys.
    But I can;t find a good example code without uploading a file. I want it to happen on the fly and my coding skills don't go that far.

    Can anyone help me out with the code needed to convert a csv to json and load it in a datatable?
    Much appreciated!
  • epschroederepschroeder Posts: 5Questions: 0Answers: 0
    Hey all,

    I succeeded in getting JSON from my csv file inside a variable. What I still need to do is use this variable (called jsonText) to fill my datatable. Can someone help me this?
  • epschroederepschroeder Posts: 5Questions: 0Answers: 0
    edited July 2011
    Here is an alert with the JSON I get. Maybe it helps in answering my question.

    http://www.egbertschroeder.com/alert_jsonText.jpg
  • GregPGregP Posts: 500Questions: 10Answers: 0
    edited July 2011
    Looks like you're off to a good start!

    DataTables expects it to have an aaData key. At minimum it requires something like the following:

    [code]
    { "aaData": [
    ["Trident","Internet Explorer 4.0","Win 95+","4","X"],
    ["Trident","Internet Explorer 5.0","Win 95+","5","C"],
    ["Other browsers","All others","-","-","U"]
    ] }
    [/code]

    (yoinked from DT example pages)

    There's flexibility in how the JSON is nested, but I'm fairly confident that after conversion it needs to at least be framed in the {"aaData": [ ] } pattern.
This discussion has been closed.