0

I'm relatively new to javascript but I want to get some data from a csv file that is saved online and gets updated each hour.

The data should be displayed on a table later on but I have some problems with saving it to an array. The csv file is comma seperated, has 9 columns, over 6000 rows and is a long string of text, so no linebreaks. The first row contains usernames and each username with special characters is conclosed with quotation marks.

I've tried several codes over the past few days, but none worked. Can I parse a online CSV file into an array at all? Is there an alternative like with SQL or saving the file to my server?

Remember: The file gets updated each hour..

NOTE: There are not really problems with the codes I've found, all of these were tested by others and seemed to work. But only for local files, not actual URLs!

Innerwolf
  • 57
  • 1
  • 7

2 Answers2

1

You can use this https://code.google.com/p/jquery-csv/ plugin and it is possible to convert multi-line csv into 2D-array using $.csv.toArrays(csv) or to an object using $.csv.toObjects(csv). Check this post or this one for more info

$.ajax({
    url: "urlto/filename.csv",
    success: function (data) {
        var arr = $.csvtoArray(data);
        _oncomplete(arr);
    },
    dataType: "text",
    });

_oncomplete: function (arr) {
    //Your array here
    }
Community
  • 1
  • 1
meteor
  • 2,518
  • 4
  • 38
  • 52
0

You can have a look at papaparse for a solid and full-featured CSV parsing library.

setInterval javascript function lets you update the data every hour, in case you decide to develop this part on the client.

Is there an alternative like with SQL or saving the file to my server?

Yes there are alternatives, the right architecture depends on your use case. How many visitors will go to your web page and view the results, how critical your application is, how reliable the data source is, etc. If you're not sure about these you should talk to a web developer with more experience around these questions.

You may want to parse the CSV file every hour on the server and store a copy of the data there, to serve to your visitors. This way, if the upstream data source is unavailable, you still have a copy of the data from the past.

P.S.:

I've tried several codes over the past few days, but none worked

stackOverflow is about this: getting help about specific problems in your code, rather than asking general questions (answer to those can be found easily using a search engine).

Mehdi
  • 7,204
  • 1
  • 32
  • 44
  • The data is not critical at all and the source is very reliable, so saving it to my server is not really necessary. There are not really problems with the codes I've found, all of these were tested by others and seemed to work. But only for local files, not actual URLs! – Innerwolf Jun 23 '15 at 08:42