JSON parse error on undefined object
Copy link to clipboard
Copied
I'm currently attempting to pass a json string to a jsx file so that I could parse it and do something with it in the script.
I evaluate json2.js like so:
const json2File = `${root}/js/json2.js`;
csiRun.evalScript(`$.evalFile('${json2File}')`);
I evaluate a script like so:
$('#create-html').click(function() {
csiRun.evalScript(`setupArtTemplate()`, jsxResult => {
csiRun.evalScript(`main('${jsxResult}')`);
});
})
In my script I then try to parse the data like so:
function main(layoutDetails) {
try {
var detailsParsed = JSON.parse(layoutDetails);
} catch (error) {
alert(layoutDetails);
alert('Cannot parse data, error: ' + error)
}
}
The error I receive is
Cannot parse data, error: TypeError: undefined is not an object
However as you can see I alert layoutDetails which does show the object as a json string. But it doesn't parse. Any idea what I'm doing wrong?
Explore related tutorials & articles
Copy link to clipboard
Copied
Does csiRun.evalScript keep all execution variables in the JSX memory?
If not, it may be necessary to move the code for the JSON object into your main() function.
Copy link to clipboard
Copied
I thought about that so I tried putting
#include "./json2.js"
in the file where main is but it didn't work. Are you suggesting putting the entire json2 code in the file?
Copy link to clipboard
Copied
I looked through my code for a CEP extension and saw I was using eval() in conjunction with the #include line.
(Of course you can also paste the JSON object, but no need)
Here is what mine looks like:
eval("#include 'someScript.jsx'");
Copy link to clipboard
Copied
I put this in the file where the json.parse is going and it didn't work.
eval("#include './json2.js'");
Also tried
eval("#include 'json2.js'");
Do you think I'm missing something?
Copy link to clipboard
Copied
I verified that the JSON library is loading. I've tested two and I still get the error:
Cannot parse data, error: TypeError: undefined is not an object
Do you have any idea what coudl be happening?

