Skip to main content
Premiopolis
Inspiring
September 22, 2016
Answered

ExtendScripts JSON object is suddenly undefined?

  • September 22, 2016
  • 1 reply
  • 3333 views

I've been passing data pretty fluently back and forth between a custom HTML5 panel and ExtendScript, making use of JSON.stringify

All of a sudden, today the JSON object is coming up as undefined in the ExtendScript context.

I know it has worked, and now it doesn't.  Why on earth?!

This topic has been closed for replies.
Correct answer Premiopolis

Was considering the manually-implemented stringify, but being able to use JSON.parse & .stringify was enormously convenient.

Found Douglas Crockford's very handy JS library: GitHub - douglascrockford/JSON-js: JSON in JavaScript​ via this forum thread: Re: HTML5 Panel passing JSON data to javascript

The Crockford file generates a JSON obj when it doesn't exists.  So far working great.

1 reply

Thomas_Szabo
Inspiring
September 22, 2016

I´m not sure since which PPro version the JSON object is supported in Extendscript but I never trusted it. I definitely recommend to stringify your data manually.

I used to do it this way:

var obj = '{';

  obj += '"type":"bin",';

  obj += '"age":"'+current_age+'",';

  obj += '"name":"'+encodeURIComponent(item.name)+'"';

  obj += '}';

For arrays it´s much simpler:

var arr = ["one", "two", "three"];

var string = '['+arr.join(",")+']';

Definitely encode strings that you don´t have control over.

Also, please check if you can implement a listener coding pattern which uses CSXSEvents. On Extendscript you pass it a stringified object and get a parsed object back on the HTML5 listener side. This avoids JSON.parse() on the HTML side.

I hope this helps.

Thomas

Premiopolis
PremiopolisAuthorCorrect answer
Inspiring
September 22, 2016

Was considering the manually-implemented stringify, but being able to use JSON.parse & .stringify was enormously convenient.

Found Douglas Crockford's very handy JS library: GitHub - douglascrockford/JSON-js: JSON in JavaScript​ via this forum thread: Re: HTML5 Panel passing JSON data to javascript

The Crockford file generates a JSON obj when it doesn't exists.  So far working great.

Bruce Bullis
Community Manager
Community Manager
September 23, 2016

JSON isn't reliably available in ExtendScript, because it's provided by another panel.

Also, ExtendScript is NOT NOT NOT JavaScript, but if Crockford's code works, great!