Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

152
Views
Can't #include in illustrator script code

I am trying to read a JSON file, in order to make changes in an illustrator document according to the data in that JSON file.

I tried this code:

#include 'libs/json2.js';
    
function readJSONFile(file) {

  file.open("r");
  var data = file.read();
  file.close();
  data = JSON.parse(data);
  for(var i in data) {
    $.writeln(i + "=" + data[i]);
  }
}
function getCorrections()
{
  const corrections = readJSONFile(File('C:/corrections.json'));
  alert(corrections);
}

getCorrections();

but when I run the script in Illustrator, it gives me an error saying "JSON is undefined"

so, I tried to use #import as I've seen in other Stackoverflow threads, but it seems like Illustrator don't accept that syntax.

#include 'libs/json2.js';

I get this error:

Error 23: ) does not not have a value.
Line: 1
-> ()

I would be glad to get some help importing an external library to my script, thank you.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Try this:

// @include 'libs/json2.js'

But actually both of the variants work for me. #includes is a valid option for Illustrator.

Perhaps the problem is in your data. I could try to parse it if you provide the sample of your date.

Here is full code that works for me:

#include 'libs/json2.js';

var file = File('d:/stackoverflow/jsx/json/test.json'); // <-- a full path!!!
readJSONFile(file);

function readJSONFile(file) {
    file.open("r");
    var data = file.read();
    file.close();
    data = JSON.parse(data);
    for (var i in data) {
        $.writeln(i + "=" + data[i]);
    }
}

test.json file:

{
    "patches": [
        {
            "patchName": "N0",
            "offsetL": "1",
            "offsetA": "-1",
            "offsetB": "2"
        },
        {
            "patchName": "N1",
            "offsetL": "1",
            "offsetA": "-1",
            "offsetB": "2"
        }
    ]
}

Everything works fine:

enter image description here

Check the path to your json file.

You can get the folder that contains a current script this way:

var script_folder = File($.fileName).parent;

alert(script_folder); // 'd/stackoverflow/jsx/json' in my case
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!