You could a reviver function of JSON.parse and convert numerical values to numbers.
Using the reviver parameter
If a
reviveris specified, the value computed by parsing is transformed before being returned. Specifically, the computed value and all its properties (beginning with the most nested properties and proceeding to the original value itself) are individually run through thereviver. Then it is called, with the object containing the property being processed asthis, and with the property name as a string, and the property value as arguments. If thereviverfunction returnsundefined(or returns no value, for example, if execution falls off the end of the function), the property is deleted from the object. Otherwise, the property is redefined to be the return value.If the
reviveronly transforms some values and not others, be certain to return all untransformed values as-is, otherwise they will be deleted from the resulting object.
var json = '[{"key":"Idle Time","values":[{"x":"SADSA","y":"4.0"},{"x":"FDDG","y":"6.0"},{"x":"FF","y":"4.15"}]},{"key":"Operational Time","values":[{"x":"SADSA","y":"20.0"},{"x":"FDDG","y":"18.0"},{"x":"FF","y":"19.45"}]}]',
data = JSON.parse(json, (key, value) => !isNaN(value) ? +value : value);
console.log(data);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Run code snippetEdit code snippet Hide Results Copy to answer Expand
Answer from Nina Scholz on Stack OverflowDoes the String to JSON converter validate JSON?
What does the String to JSON converter do?
Is the String to JSON converter beginner-friendly?
You could a reviver function of JSON.parse and convert numerical values to numbers.
Using the reviver parameter
If a
reviveris specified, the value computed by parsing is transformed before being returned. Specifically, the computed value and all its properties (beginning with the most nested properties and proceeding to the original value itself) are individually run through thereviver. Then it is called, with the object containing the property being processed asthis, and with the property name as a string, and the property value as arguments. If thereviverfunction returnsundefined(or returns no value, for example, if execution falls off the end of the function), the property is deleted from the object. Otherwise, the property is redefined to be the return value.If the
reviveronly transforms some values and not others, be certain to return all untransformed values as-is, otherwise they will be deleted from the resulting object.
var json = '[{"key":"Idle Time","values":[{"x":"SADSA","y":"4.0"},{"x":"FDDG","y":"6.0"},{"x":"FF","y":"4.15"}]},{"key":"Operational Time","values":[{"x":"SADSA","y":"20.0"},{"x":"FDDG","y":"18.0"},{"x":"FF","y":"19.45"}]}]',
data = JSON.parse(json, (key, value) => !isNaN(value) ? +value : value);
console.log(data);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Run code snippetEdit code snippet Hide Results Copy to answer Expand
The following code can also work for you:
var strJson = '[{"key":"Idle Time","values":[{"x":"SADSA","y":"4.0"},{"x":"FDDG","y":"6.0"},{"x":"FF","y":"4.15"}]},{"key":"Operational Time","values":[{"x":"SADSA","y":"20.0"},{"x":"FDDG","y":"18.0"},{"x":"FF","y":"19.45"}]}]';
var parsedJSON = JSON.parse(strJson);
var res = [];
parsedJSON.forEach((obj) => {
obj.values.forEach((innerObj) => {
innerObj.y = parseFloat(innerObj.y);
});
res.push(obj);
});
console.log(res);
Run code snippetEdit code snippet Hide Results Copy to answer Expand