Skip to main content
Participant
March 13, 2017
Answered

Import values from JSON or similar into PDF

  • March 13, 2017
  • 4 replies
  • 13971 views

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!

This topic has been closed for replies.
Correct answer Thom Parker

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.

4 replies

Thom Parker
Community Expert
Thom ParkerCommunity ExpertCorrect answer
Community Expert
March 14, 2017

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.

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
Karl Heinz  Kremer
Community Expert
Community Expert
March 14, 2017

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

Karl Heinz  Kremer
Community Expert
Community Expert
March 14, 2017

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.

Participant
March 22, 2019

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!

Legend
March 14, 2017

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

lrosenth
Adobe Employee
Adobe Employee
March 14, 2017

JSON.parse is a new feature of JavaScript that may not be supported in yoru version of Acrobat. What version are you working with?

anfucaAuthor
Participant
March 14, 2017

Hi lrosenth
thanks for answering. I'm working with the 15.020 version. What is the best choice instead?

Legend
March 14, 2017

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.