JSON parse error on undefined object

Participant ,
Dec 22, 2020

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?

TOPICS
Scripting

Views

77

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

JSON parse error on undefined object

Participant ,
Dec 22, 2020

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?

TOPICS
Scripting

Views

78

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Dec 22, 2020 0
Adobe Community Professional ,
Dec 22, 2020

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.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Dec 22, 2020 0
Participant ,
Dec 22, 2020

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?

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Dec 22, 2020 0
Adobe Community Professional ,
Dec 22, 2020

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

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Dec 22, 2020 0
Participant ,
Dec 22, 2020

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?

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Dec 22, 2020 0
OhmsG LATEST
Participant ,
Dec 23, 2020

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?

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Dec 23, 2020 0
Resources