illcrx illcrx - 28 days ago 13
Javascript Question

JSON data types - numbers are converting to strings when it gets to the server

I am loading a

CSV
file, parsing it into a
JSON
object, then I am converting those strings into numbers. They show as numbers in the browser console, then using AJAX I am sending the data to the server. When I console.log the data everything is strings? I though
JSON
could accept a variety of data types, so why is it going down the line as strings?

Here is the code.

for(var i = 0; i < input.files.length; i++){
var files = input.files[i];

Papa.parse(files, {
header:false,
dynamictyping:true,
complete: function(results){
var input = results.data;

if(input[0][0] === 'Symbol' || input[0][0] === 'symbol'){
input.shift();
}
input.forEach(function(input){
jsonData.theData = theData;

var singleEntry = {
"symbol" : input[0],
"date" : input[1],
"open" : Number(input[2]),
"high" : Number(input[3]),
"low" : Number(input[4]),
"close" : Number(input[5]),
"volume" : Number(input[6])
};


// Here we will try to do the daily computations of what is needed for data
// such as percentage closed in the day and what not.

var open = singleEntry.open;
var high = singleEntry.high;
var low = singleEntry.low;
var close = singleEntry.close;
/*
console.log(open);
console.log(high);
console.log(low);
console.log(close); */

//Get the Math variables for close percentage
var spread = high - low;
var closeDiff = close - low;
var answer = closeDiff / spread;

console.log(answer);
//Adding day closes to object
if (singleEntry.volume === 0){
singleEntry["supportDay"] = false;

} else {

if(answer <= .3999){

singleEntry["percentClose"] = answer;
singleEntry["supportDay"] = false;
console.log("answer <= .39999");

} else if (answer > .95) {

singleEntry["percentClose"] = answer;
singleEntry["supportDay"] = true;
singleEntry["peakClose"] = true;
console.log("answer > .95");

} else {

singleEntry["percentClose"] = answer;
singleEntry["supportDay"] = true;

}
}

jsonData.theData.push(singleEntry);
console.log(singleEntry.supportDay);

return jsonData;
}); // End forEach loop

document.getElementById("editor").innerHTML = JSON.stringify(jsonData.theData[0]);

} // End Callback Complete
}); // End PapaParse
} // End for loop
});


So as you can see I can work with the objects and here is the console.log output on the browser, all that code is in the browser.

enter image description here

Now here is the console.log to the server:

enter image description here

This data gets imported straight into the database there is no working with the data in Node. In my database it imports as strings as well.

Thoughts? What am I missing?

Answer

With JSON, you're inputs can be of various data types.However, it gets serialized as one big string and sent down the wire to your server as such. On the server side, the string needs to be parsed into an object again. That step will vary depending on what sort of language your server operates in.