jQuery.ajax({
url: ".csv",
type: 'get',
dataType: 'json',
success: function(data) {
console.log(data);
},
error: function(jqXHR, textStatus, errorThrow){
alert("Error: " + jqXHR['responseText']);
}
});
I need the output as formatted json but It's going to error, basically trying to parse this
.csv
UPDATE
They've changed the link
.csv
jQuery.ajax({
url: "https://raw.githubusercontent./CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_19-covid-Confirmed.csv",
type: 'get',
dataType: 'json',
success: function(data) {
console.log(data);
},
error: function(jqXHR, textStatus, errorThrow){
alert("Error: " + jqXHR['responseText']);
}
});
I need the output as formatted json but It's going to error, basically trying to parse this
https://raw.githubusercontent./CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_19-covid-Confirmed.csv
UPDATE
They've changed the link
https://raw.githubusercontent./CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_confirmed_global.csv
Share Improve this question edited Apr 10, 2020 at 2:40 rob.m asked Mar 8, 2020 at 0:06 rob.mrob.m 10.6k21 gold badges88 silver badges175 bronze badges 10- 1 dataType: "text/csv", – EugenSunic Commented Mar 8, 2020 at 0:16
- Look at the source code from : lucidar.me/en/covid-19/global-prediction – Fifi Commented Mar 25, 2020 at 10:30
- Clicking your link yields 404: Not Found. – skomisa Commented Mar 26, 2020 at 6:09
- @Fifi thanks but ended with my own cor using php in order not to ping git everytime – rob.m Commented Mar 26, 2020 at 9:06
- OK, great. I realized suddenly you expected JS, not Python ! @skomisa The link should work now. – Fifi Commented Mar 26, 2020 at 9:46
2 Answers
Reset to default 6Parsing CSV is not always as simple as doing "...".split(',')
. And the file here is a perfect example of that. Some fields contain a ,
, and thus are wrapped in quotes.
I suggest using Papa Parse which will handle that for you. I've used it many times before, it saved me a lot of headaches!
$.ajax({
url: "https://raw.githubusercontent./CSSEGISandData/COVID-19/6eae5b65a32b679efacf95a2867648330f83a871/csse_covid_19_data/csse_covid_19_time_series/time_series_19-covid-Confirmed.csv",
success: function(csv) {
const output = Papa.parse(csv, {
header: true, // Convert rows to Objects using headers as properties
});
if (output.data) {
console.log(output.data);
} else {
console.log(output.errors);
}
},
error: function(jqXHR, textStatus, errorThrow){
console.log(textStatus);
}
});
<script src="https://cdnjs.cloudflare./ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare./ajax/libs/PapaParse/5.1.0/papaparse.min.js"></script>
You have to use dataType
as text
and then run split
with double for loop to get the data as an array of JS objects:
jQuery.ajax({
url: "https://raw.githubusercontent./CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_19-covid-Confirmed.csv",
type: 'get',
dataType: 'text',
success: function(data) {
let lines = data.split('\n');
let fields = lines[0].split(',');
let output = [];
for(let i = 1; i < lines.length; i++){
let current = lines[i].split(',');
let doc = {};
for(let j = 0; j < fields.length; j++){
doc[fields[j]] = current[j];
}
output.push(doc);
}
console.log(output);
},
error: function(jqXHR, textStatus, errorThrow){
console.log(textStatus);
}
});
<script src="https://cdnjs.cloudflare./ajax/libs/jquery/3.3.1/jquery.min.js"></script>