Copy link to clipboard
Copied
Hi, last days I'm diving into this awesome world of scripting.
I'm trying to guess the way to import, parse and play with a JSON file into Acrobat using Javascript.
I'm able to get the content using util.readFileIntoStream, but now, I don't know what to do with it... JSON.parse doesn't seem to work.
Thanks in advance!
JSON is JavaScript, so as Karl has shown, the JavaScript version is not important when it comes to parsing JavaScript code in string form. Just use the standard, built-in, "eval" function.
But Karl has only show the last part of the script, the whole script is
var oStream = util.readFileIntoStream(...path...);
var strJSON = util.stringFromStream(oStream);
var oData = eval("(" + strJSON + ")");
Pretty simple, and you could put it all in one line
var oData = eval("(" + util.stringFromStream(util.re
...Copy link to clipboard
Copied
JSON.parse is a new feature of JavaScript that may not be supported in yoru version of Acrobat. What version are you working with?
Copy link to clipboard
Copied
Hi lrosenth​
thanks for answering. I'm working with the 15.020 version. What is the best choice instead?
Copy link to clipboard
Copied
What's the full version number, with 5 more digits? This is more important than you might think as it tells us whether you have the updated subscription version or the old frozen permanent version.
Copy link to clipboard
Copied
Oh I see... The full version number is 15.020.20042
Copy link to clipboard
Copied
Hmm, that is pretty much the latest subscription version (close to it, not fully up to date). So I think you have the latest API. I found contradictory info, but it may be that JSON (not core ECMAScript) was not added. This page discusses an alternative: https://answers.acrobatusers.com/JSON-Object-AcrobatX-q3969.aspx
Copy link to clipboard
Copied
If all you want to do is to parse a JSON string and convert it to a JavaScript data structure, you can do this in a way that will work with all versions of Acrobat - and there is no need to install a JSON library. The trick is to use 'eval()' - but as you will quickly find out, you cannot just pass the JSON string to it, there is a little bit more involved: You need to wrap the code in parenthesis:
var json = '{"field1":"abc","field2":"def","record1":{"field3":"xyz","field4":"uvw"},"array1":[1,2,3,4]}';
var myData = eval( "(" + json + ")"); // this is where the magic happens
console.println(myData.field1);
So, all you need to do is to add a "(" at the beginning of your string and a ")" at the end, end then use eval to convert to a JavaScript object.
Copy link to clipboard
Copied
OMG - after seeing so many other sample scripts that use
result = eval( "{" + content + "}" );
and having it fail miserably in InDesign, the suggestion to use parenthesis was the answer I was looking for - thanks!
Copy link to clipboard
Copied
JSON is JavaScript, so as Karl has shown, the JavaScript version is not important when it comes to parsing JavaScript code in string form. Just use the standard, built-in, "eval" function.
But Karl has only show the last part of the script, the whole script is
var oStream = util.readFileIntoStream(...path...);
var strJSON = util.stringFromStream(oStream);
var oData = eval("(" + strJSON + ")");
Pretty simple, and you could put it all in one line
var oData = eval("(" + util.stringFromStream(util.readFileIntoStream(...path...)) + ")");
However, it is much easier to handle errors when each part is on it's own line.
Copy link to clipboard
Copied
https://forums.adobe.com/people/Thom+Parker wrote
However, it is much easier to handle errors when each part is on it's own line.
Not only that, it also makes your life a lot easier when you look at your code again six months or two years from now