4 Replies Latest reply on Sep 20, 2016 6:44 AM by Heimdaal

    Open JSON file and parse it? (ExtendScript)

    Heimdaal Level 1

      I know that JSON isn't built into ExtendScript but even with the downloaded lib I do not understand how you are supposed to use this?

      If I try and use #include in my jsx-file the entire script comes to a halt (nothing gets loaded)



      #include "../js/libs/json2.js"
      alert("this does not trigger! - script halted on line above");



      So what's the approach here? Send the file path from JS to JSX, create a file object and read every line in JSX, returing the file string back to JS and then parse it?

        • 1. Re: Open JSON file and parse it? (ExtendScript)
          DBarranca Level 4

          I understand that you're referring to an HTML Panel context, am I correct?

          If this is the case, yes, include will not work. You have to send the extension's path down to the JSX from the JS, then evaluate json2.js with $.evalFile().

          From that point onwards, the JSON object should be available to your extendscript context.


          Davide Barranca

          • 2. Re: Open JSON file and parse it? (ExtendScript)
            CodeARoni Level 1

            If you want to parse JSON in extendscript, you can use a polyfill. Here's an easy example you can copy/paste: generator-gizmo/rootscript.jsx at master · codearoni/generator-gizmo · GitHub


            Just paste that at the top of you extendscript file and you should be good to go.

            • 3. Re: Open JSON file and parse it? (ExtendScript)
              Heimdaal Level 1

              I moved the parsing to the JS file instead but it appears that you can't even use JSON there! JSON is undefined -_-
              Why did Adobe make things so bloody complicated?



              I put the JSON.parse() inside a try-catch and I'm getting syntax errors (which is weird as I've used several online validators on my JSON code)

              Exception:SyntaxError: Unexpected token :



                  "lolk": [
                          "icon": "/img/picA.png",
                          "path": "/scripts/file.a",
                          "text": "somethingA"
                          "icon": "/img/picB.png",
                          "path": "/scripts/file.b",
                          "text": "somethingB"
                          "icon": "/img/picC.png",
                          "path": "/scripts/file.c",
                          "text": "somethingC"



              JSON.parse() seems to be completely broken?

                  var lol = JSON.parse("{'lolk':'stuff'}"); // Exception:SyntaxError: Unexpected token '
              } catch(e) {
                  alert("Exception:" + e + "\nCould not run JSON.parse()");
              1 person found this helpful
              • 4. Re: Open JSON file and parse it? (ExtendScript)
                Heimdaal Level 1


                -The JSON -library json2.js can be "included" in the main.js file by running evalFile(pathToFile). I assume this also works in JSX. Thanks goes to Davide Barranca for showing us the way!

                -You cannot use #include in the JSX file for Photoshop as this will completely break the execution

                -When reading the file, you have to do it all in one go - not line-by-line (see below).



                I read the JSON-file incorrectly, which broke JSON.parse()

                For some reason I had setup the function that did the reading, to read the file row-by-row, like this:



                    file = new File(filePath);
                    file.open("r", "TEXT");
                    var fileString = "";
                    while (!file.eof){
                        var line = file.readln();
                        if (fileString.indexOf(line) == -1){
                            fileString += line;
                    return fileString;


                Which seems to add characters that the parser does not like.


                I changed it so that it just reads the entire file


                    var scriptFile = File(filePath);
                    var content = scriptFile.read();