javascripttypesdata-formats

In which format to record/store numerical data for further JavaScript processing?


I'm planning to collect some data over a few months (daily) for further processing and representation in JavaScipt (probably using any js libraries, such as d3.js, etc. I don't know which one yet). The data will consist of:

Which file/data format would you recommend for recording data for subsequent work with JavaScript?


Solution

  • I think CSV would be more appropriate here because it sounds like it's just going to be a big long list of datapoints.

    JSON would work, just like XML or any other system would work, but as much as I love JSON, it is not necessarily well-suited to the job:

    CSV requires much less superfluous punctuation, and you can append a new line of data to the end of your file without needing to read and write the entire thing.

    Consider:

    JSON (not pretty):

    {"01012016":[42,0.8675309],"01022016":[12,9.87654321]}
    

    JSON (pretty):

    {
      "01012016":[
        42,
        0.8675309
      ],
      "01022016":[
        12,
        9.87654321
      ]
    }
    

    CSV:

    01012016,42,0.8675309
    01022016,12,9.87654321
    

    Javascript doesn't have a built-in CSV parser in the way it has JSON.parse... because parsing CSV is really easy! Here's just one of many ways of doing it:

    var splitByLine = myCSVString.split("\n");
    var splitByLineAndComma = [];
    splitByLine.forEach(function(line){
      splitByLineAndComma.push(line.split(","));
    });