Loading functions stored in an array from a JSON file is coming up "undefined"
-
I made a JSON file with info that I need to use in a data table.
I am storing sort functions in an array inside the JSON.
The sort functions can be seen in the JSON itself:
When I load the JSON, all of my other data that was stored in the file will load properly,
but the sort functions just come up as "undefined":I don't quite understand what is going on.
Ideas? -
@VirtualVirgin you can‘t serialize a function, JSON is a data only format.
-
@Christoph-Hart said in Loading functions stored in an array from a JSON file is coming up "undefined":
@VirtualVirgin you can‘t serialize a function, JSON is a data only format.
Thanks, although I don't quite understand the premise.
If the functions as code appear in the JSON file that was dumped, how does that same code not appear when the JSON is loaded?
Is it changing format somehow?
Conceptually I'm just behind.If I store the functions as const variables in an external .js file, then put those const variable references inside my array in the JSON file, would that then load correctly?
-
@VirtualVirgin you‘re confusing JSON and Javascript. Javascript can define objects with functions as values but JSON can‘t. If you‘re storing and object somewhere it uses the JSON format.
What you need to do is to define your functions in a Javascript object in your script and then just store the function name in the JSON that you export.
-
@Christoph-Hart said in Loading functions stored in an array from a JSON file is coming up "undefined":
define your functions in a Javascript object in your script and then just store the function name in the JSON that you export.
Thanks! I got it working now :)