Skip to main content
Inspiring
January 7, 2020
Answered

Parsing JSON in adobe illustrator extended panel

  • January 7, 2020
  • 1 reply
  • 4864 views

I currently have two files.

A js file "myjs.js" and a jsx file "myjsx.jsx"

I pass some data and call a method in myjsx.jsx file like so:

      const someData = ['john', 'bar', 'son', 'datto'];
      csiRun.evalScript(`setupPlateTemplate('${someData}')`);

I then can view this data in my jsx file.

function setupPlateTemplate(orderData) {

alert( JSON(orderData) );

} 

This alerts a full string if I try to do this: alert (JSON(orderData[0]))  I get the alert of '"' since it's reading it as a string I only get the first character which is ".

I try this alert (JSON.parse(orderData)) but nothing happens.  Nothing as in no error or anything.

I'm using this library: json2.jsx found here.

I tried json.stringify on the js before I send it and that doesn't work either.  Any idea what I'm doing wrong?

 

This topic has been closed for replies.
Correct answer Inventsable

This is the code from the previous thread I tried.

let root = decodeURI(csiRun.getSystemPath("extension")).replace(/file\:\/{1,}/, "");
const json2File = `${root}/jsx/json2.jsx`;
const productTemplateSetupFile = `${root}/jsx/productTemplateSetup.jsx`;
csiRun.evalScript(json2File);
csiRun.evalScript(productTemplateSetupFile);
const someData = ['john', 'bar', 'son', 'datto'];
csiRun.evalScript(`setupPlateTemplate('${JSON.stringify(someData)}')`)

This appears to not even get to the jsx since the try / catch doesn't fire.  

The data is pretty complex, it's a return from an api call so there will be different objects and arrays I need to manipulate.  

 


Hi, the code you've just quoted is incorrect. You've confused the $.evalFile with CSInterface.evalScript, you should be doing this instead:

 

let root = decodeURI(csiRun.getSystemPath("extension")).replace(/file\:\/{1,}/, "");
const json2File = `${root}/jsx/json2.jsx`;
const productTemplateSetupFile = `${root}/jsx/productTemplateSetup.jsx`;
csiRun.evalScript(`$.evalFile('${json2File}')`);
csiRun.evalScript(`$.evalFile('${productTemplateSetupFile}')`);
const someData = ['john', 'bar', 'son', 'datto'];
csiRun.evalScript(`setupPlateTemplate('${JSON.stringify(someData)}')`)

 

Notice that I'm using $.evalFile to the root path, otherwise you're just passing an arbitrary string (which happens to be a file path) into CSInterface.evalScript but you aren't calling any function on it so it has no effect.

1 reply

Inventsable
Legend
January 7, 2020

Hi, JSON is a class, not a method. Try csiRun.evalScript(`setupPlateTemplate('${JSON.stringify(someData)}')`) in JS, then in JSX:

 

function setupPlateTemplate(orderData) { alert( JSON.parse(orderData) ); }

 

OhmsGAuthor
Inspiring
January 7, 2020

This doesn't work or give any error.  Same thing nothing happens. @Inventsable

Inventsable
Legend
January 8, 2020

That is the correct syntax, so your problem is likely somewhere earlier in your setup. If JSON fails in JSX, it will cause the entire script to silently fail. I'd confirm with an alert the line below with just a string, like "hello world". If both alerts don't fire, there's an evalScript error. You may be able to use a try/catch statement to find it. Otherwise:

 

  1. Are you sure you've evaluated json2.jsx before your myjsx.jsx? And each before this function in myjs.js is fired?
  2. Is json2 a .js or .jsx file?
  3. For this to work, you must stringify it before passing to evalScript, then you must parse it at the beginning of the jsx function. If not, your use of single-quotes in both the array you send and in the evalScript parameter will likely throw false pairs. Do you have this on a Github repo?