Created
September 2, 2018 02:04
-
-
Save AdamStormhardtGH/c6a4b721d9c8b910fc1ef3051f961fda to your computer and use it in GitHub Desktop.
After Effects Extendscript Read JSON
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//from https://github.com/fabianmoronzirfas/extendscript/wiki/Read-In-JSON-From-File-And-DONT-Eval | |
//For after effects Extendscript | |
//This function returns the json object for you to parse as needed | |
//usage: | |
// assume data.json looks like this: { "Cat":{"power":2, "affection": 4, "default_name":"Bagel"}} | |
// var myjson = getJSON("data.json"); | |
// var cat_power = myjson.cat.power | |
function getJSON(filename) { | |
var script_file = File($.fileName); // get the location of the script file | |
var script_file_path = script_file.path; // get the path from the script file | |
var file_to_read = File(script_file_path + "/"+ filename );// build the relative path to the JSON | |
var my_JSON_object = null; // create an empty variable | |
var content; // this will hold the String content from the file | |
if(file_to_read !== false){// if it is really there | |
file_to_read.open('r'); // open it | |
content = file_to_read.read(); // read it | |
my_JSON_object = JSON.parse(content);// now evaluate the string from the file | |
//$.writeln(my_JSON_object); //debug if the json object is being read | |
//$.writeln(my_JSON_object.toSource()); | |
//alert(my_JSON_object.toSource()); // if it all went fine we have now a JSON Object instead of a string call length | |
file_to_read.close(); // always close files after reading' | |
return my_JSON_object; //get the object so we can use it | |
}else{ | |
alert("Something has gone wrong. Make sure youre not trying to parse your json again"); // if something went wrong | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment