Copy link to clipboard
Copied
Hi all
I always worked with XML files to collect informations to use in my scripts. Illustrator supports the XML object, so I can acess the nodes and collect the data I need from the XML.
But I recently received a JSON file. I looked into the "JavaScript Tools Guide.pdf" documentation and did not find any information about how to manipulate the JSON file like I use to do with my XML.
I asked to a friend and he told me perhaps we could use the Eval function....But I'm having no sucess. Do not know how to proceed...Initialy I tried:
var myFile = new File ("~/desktop/data.json");
alert(myFile.exists); //returns true!
myFile.open("r"); //sucess opening
var content = myFile.read(); //sucess reading
myFile.close(); //sucess closing
var x = eval(content); //CRASH!
But it did not work. It returns an error "Cannot execute script in target engine main"
How should I use eval to have an way to manipulate data in JSON?
Thank you very much
Gustavo.
Copy link to clipboard
Copied
use this snippet to perform a JSON.parse() on your file.
"object"!=typeof JSON&&(JSON={}),function(){"use strict";function f(t){return 10>t?"0"+t:t}function quote(t){
return escapable.lastIndex=0,escapable.test(t)?'"'+t.replace(escapable,function(t){var e=meta
; return"string"==typeof e?e:"\\u"+("0000"+t.charCodeAt(0).toString(16)).slice(-4)})+'"':'"'+t+'"'}
function str(t,e){var n,r,o,f,u,i=gap,p=e
;switch(p&&"object"==typeof p&&"function"==typeof p.toJSON&&(p=p.toJSON(t)), "function"==typeof rep&&(p=rep.call(e,t,p)),typeof p){case"string":return quote(p);case"number":return isFinite(p)?String(p):"null";
case"boolean":case"null":return String(p);case"object":if(!p)return"null";if(gap+=indent,u=[],"[object Array]"===Object.prototype.toString.apply(p)){
for(f=p.length,n=0;f>n;n+=1)u
=str(n,p)||"null";return o=0===u.length?"[]":gap?"[\n"+gap+u.join(",\n"+gap)+"\n"+i+"]":"["+u.join(",")+"]",gap=i,o} if(rep&&"object"==typeof rep)for(f=rep.length,n=0;f>n;n+=1)"string"==typeof rep
&&(r=rep ,o=str(r,p),o&&u.push(quote(r)+(gap?": ":":")+o)); else for(r in p)Object.prototype.hasOwnProperty.call(p,r)&&(o=str(r,p),o&&u.push(quote(r)+(gap?": ":":")+o));return o=0===u.length?"{}":gap?"{\n"+gap+
u.join(",\n"+gap)+"\n"+i+"}":"{"+u.join(",")+"}",gap=i,o}}"function"!=typeof Date.prototype.toJSON&&(Date.prototype.toJSON=function(){
return isFinite(this.valueOf())?this.getUTCFullYear()+"-"+f(this.getUTCMonth()+1)+"-"+f(this.getUTCDate())+"T"+f(this.getUTCHours())+":"+
f(this.getUTCMinutes())+":"+f(this.getUTCSeconds())+"Z":null},String.prototype.toJSON=Number.prototype.toJSON=Boolean.prototype.toJSON=function(){
return this.valueOf()});var cx,escapable,gap,indent,meta,rep;"function"!=typeof JSON.stringify&&
(escapable=/[\\\"\x00-\x1f\x7f-\x9f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,
meta={"\b":"\\b"," ":"\\t","\n":"\\n","\f":"\\f","\r":"\\r",'"':'\\"',"\\":"\\\\"},JSON.stringify=function(t,e,n){var r;
if(gap="",indent="","number"==typeof n)for(r=0;n>r;r+=1)indent+=" ";else"string"==typeof n&&(indent=n);if(rep=e,
e&&"function"!=typeof e&&("object"!=typeof e||"number"!=typeof e.length))throw new Error("JSON.stringify");return str("",{"":t})}),
"function"!=typeof JSON.parse&&(cx=/[\u0000\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,
JSON.parse=function(text,reviver){function walk(t,e){var n,r,o=t
;if(o&&"object"==typeof o)for(n in o)Object.prototype.hasOwnProperty.call(o,n)&& (r=walk(o,n),void 0!==r?o
=r:delete o );return reviver.call(t,e,o)}var j;if(text=String(text),cx.lastIndex=0,cx.test(text)&& (text=text.replace(cx,function(t){return"\\u"+("0000"+t.charCodeAt(0).toString(16)).slice(-4)})),
/^[\],:{}\s]*$/.test(text.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g,"@")
.replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g,"]")
.replace(/(?:^|:|,)(?:\s*\[)+/g,"")))return j=eval("("+text+")"),"function"==typeof reviver?walk({"":j},""):j;
throw new SyntaxError("JSON.parse")})}();
Copy link to clipboard
Copied
Clone/download this repo GitHub - douglascrockford/JSON-js: JSON in JavaScript and in your illustrator file do
#include "pathToJson2.js";
this way all JSON functionality, such as .stringify and .parse will be available in the scope of your script.
Copy link to clipboard
Copied
try this:
function test()
{
var myFile = new File ("~/desktop/data.json");
alert(myFile.exists); //returns true!
myFile.open("r"); //sucess opening
var content = "(" + myFile.read() + ")"; //sucess reading
myFile.close(); //sucess closing
var x = eval(content); //no more CRASH!
}
test();