Skip to main content
Inspiring
December 22, 2020
Question

JSON parse error on undefined object

  • December 22, 2020
  • 1 reply
  • 1990 views

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?

This topic has been closed for replies.

1 reply

Silly-V
Legend
December 22, 2020

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.

OhmsGAuthor
Inspiring
December 22, 2020

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?

Silly-V
Legend
December 22, 2020

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'");