Skip to main content
Participant
September 4, 2018
Answered

Bug WRT Javascript JSON in Acrobat Pro DC

  • September 4, 2018
  • 2 replies
  • 1295 views

Does anybody else see this same behavior?

Script:

var ary = [];

ary["sam"] = "test string";

console.println("The array value is " +ary["sam"]);

console.println("JSON says: " +JSON.stringify(ary));

Output:

The array value is test string

JSON says: []

This topic has been closed for replies.
Correct answer Joel Geraci

Acrobat DC JavaScript includes the JSON object.

However, your JSON code is incorrect. In JavaScript you can only add elements to an array using an index if the index is an integer. You either want...

var ary = [];

ary[0] = "test string";

... which creates an empty array and then inserts a value at index 0 or...

var obj = {};

obj["sam"] = "test string";

... which creates a JSON object and assigns the value "test string" to the property named "sam".

2 replies

Loic.Aigon
Legend
September 5, 2018

Plus I can remember some case where we use arrays for storing values through indeces and props per properties. Can't remember why my colleague did this at the time but we certainly did.

Joel Geraci
Community Expert
Community Expert
September 5, 2018

Yes... an array is an object in JavaScript... but in JSON, it's defined as "an ordered list of values" where the array begins with [ (left bracket) and ends with ] (right bracket) and values are separated by, (comma). Because JSON is a lightweight data-interchange format, it needs to conform to the least common denominator for languages which means... unfortunately, you can't get too fancy with it.

try67
Community Expert
Community Expert
September 4, 2018

Acrobat JavaScript does not include JSON. Did you install it on your own?

Joel Geraci
Community Expert
Joel GeraciCommunity ExpertCorrect answer
Community Expert
September 4, 2018

Acrobat DC JavaScript includes the JSON object.

However, your JSON code is incorrect. In JavaScript you can only add elements to an array using an index if the index is an integer. You either want...

var ary = [];

ary[0] = "test string";

... which creates an empty array and then inserts a value at index 0 or...

var obj = {};

obj["sam"] = "test string";

... which creates a JSON object and assigns the value "test string" to the property named "sam".

try67
Community Expert
Community Expert
September 4, 2018

Oh wow, I didn't realize that... Is that a new addition?