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

Open JSON file and parse it? (ExtendScript)

Contributor ,
Sep 19, 2016 Sep 19, 2016

Copy link to clipboard

Copied

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)

main.jsx

#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?

TOPICS
Actions and scripting

Views

21.8K

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

Contributor , Sep 20, 2016 Sep 20, 2016

Summary

-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, t

...

Votes

Translate

Translate
Adobe
Advocate ,
Sep 19, 2016 Sep 19, 2016

Copy link to clipboard

Copied

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

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
Contributor ,
Sep 20, 2016 Sep 20, 2016

Copy link to clipboard

Copied

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?

EDIT

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 :

JSON

{

    "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"

        }

    ]

}

EDIT2
JSON.parse() seems to be completely broken?

try{

    var lol = JSON.parse("{'lolk':'stuff'}"); // Exception:SyntaxError: Unexpected token '

    alert(lol);

} catch(e) {

    alert("Exception:" + e + "\nCould not run JSON.parse()");

}

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
Explorer ,
Sep 19, 2016 Sep 19, 2016

Copy link to clipboard

Copied

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.

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
Contributor ,
Sep 20, 2016 Sep 20, 2016

Copy link to clipboard

Copied

LATEST

Summary

-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:

JSX

    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

JSX

    var scriptFile = File(filePath);

    scriptFile.open('r');

    var content = scriptFile.read();

    scriptFile.close();

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