you can't have functions in JSON - in JSON, values can only be ... object, array, string, number, null, true or false
that's not to say what you want to do is unachievable - you'll just have to think outside the box ... e.g. on the server send { "callback": "fn1" } ... in the client, process the parsed JSON (i,e, the javascript Object you now have) and look for callback key, and changing the value accordingly ... "fn1" to function(.... etc
so, if you change your server code to send something like
{
"type": "line",
"data": {
"datasets": [
]
},
"options": {
"scales": {
"xAxes": [{
"type": "logarithmic",
"position": "bottom",
"ticks": {
"beginAtZero": false,
"min": 0.0,
"max": "NaN",
"stepSize": "NaN",
"callback": "fn1"
},
}
],
"yAxes": [{
"type": "logarithmic",
"position": "left",
"ticks": {
"beginAtZero": false,
"min": 0.0,
"max": "NaN",
"stepSize": "NaN",
"callback": "fn1arrow"
},
}
]
},
}
}
Then you can write a function like
function fixCallbacks(obj) {
const fns = {
fn1: function(value, index, values) { return Number(value.toString());},
fn1arrow: (value, index, values) => Number(value.toString()),
};
Object.entries(obj).forEach(([k, v]) => {
if (typeof v === 'object' || typeof v === 'array') {
fixCallbacks(v);
} else if (k === 'callback') {
obj[k] = fns[v] || obj[k];
}
});
}
Note: fn1 and fn1arrow do the same thing, just showing you can use functions and arrow functions as a solution
And it should work - like this
const data = {
"type": "line",
"data": {
"datasets": [
]
},
"options": {
"scales": {
"xAxes": [{
"type": "logarithmic",
"position": "bottom",
"ticks": {
"beginAtZero": false,
"min": 0.0,
"max": "NaN",
"stepSize": "NaN",
"callback": "fn1"
},
}
],
"yAxes": [{
"type": "logarithmic",
"position": "left",
"ticks": {
"beginAtZero": false,
"min": 0.0,
"max": "NaN",
"stepSize": "NaN",
"callback": "fn1arrow"
},
}
]
},
}
};
function fixCallbacks(obj) {
const fns = {
fn1: function(value, index, values) { return Number(value.toString());},
fn1arrow: (value, index, values) => Number(value.toString()),
};
Object.entries(obj).forEach(([k, v]) => {
if (typeof v === 'object' || typeof v === 'array') {
fixCallbacks(v);
} else if (k === 'callback') {
obj[k] = fns[v] || obj[k];
}
});
}
// here we "fix" the data
fixCallbacks(data);
//
console.log(data);