• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Import values from JSON or similar into PDF

Community Beginner ,
Mar 13, 2017 Mar 13, 2017

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!

TOPICS
Acrobat SDK and JavaScript

Views

10.6K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Mar 14, 2017 Mar 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.re

...

Votes

Translate

Translate
Adobe Employee ,
Mar 13, 2017 Mar 13, 2017

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?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Mar 14, 2017 Mar 14, 2017

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Mar 14, 2017 Mar 14, 2017

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Mar 14, 2017 Mar 14, 2017

Copy link to clipboard

Copied

Oh I see... The full version number is 15.020.20042

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Mar 14, 2017 Mar 14, 2017

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 14, 2017 Mar 14, 2017

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Mar 22, 2019 Mar 22, 2019

Copy link to clipboard

Copied

LATEST

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!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 14, 2017 Mar 14, 2017

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.

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 14, 2017 Mar 14, 2017

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines