Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

JSON parse error on undefined object

Contributor ,
Dec 22, 2020 Dec 22, 2020

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
1.6K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe
Valorous Hero ,
Dec 22, 2020 Dec 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Dec 22, 2020 Dec 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?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Dec 22, 2020 Dec 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'");

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Dec 22, 2020 Dec 22, 2020

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?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Dec 23, 2020 Dec 23, 2020
LATEST

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?

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