I have a JSON string that reads:
[{
"id": "id2",
"index": "2",
"str": "str2",
"cent": "200",
"triplet": "222"
},
{
"id": "id3",
"index": "3",
"str": "str3",
"cent": "300",
"triplet": "333"
},
{
"id": "id4",
"index": "4",
"str": "str4",
"cent": "400",
"triplet": "444"
},
{
"id": "id5",
"index": "5",
"str": "str5",
"cent": "500",
"triplet": "555"
}]
The key-value pairs come dynamically from the server and I won't know beforehand what data to expect.
For a charting library that I use, I need the values in JSON to be numeric rather than string viz. "index":2
instead of "index":"2"
I am required to do this manipulation client-side using pure JS or jQuery.
This was my approach but it doesn't seem to work:
var temp = //some json that I receive
var jsonForChart = jQuery.extend(true, {}, temp);
$.each(temp, function(key, value) {
$.each(value, function(k, v) {
if(!isNaN(v)){
jsonForChart[key][k] = Number(v);
}
});
});