Copy link to clipboard
Copied
Just because someone has asked this question on another forum and I'm intrigued about this idea myself. When you put your JSON in the project panel. Is there a way to access the JSON data via script? So rather than using expressions, or a script to add an expression to a layer. Using something on the lines of app.project.item(index).sourceData.Name I've tried this and I know it doesn't work, but something on those lines to access that data via script. Is this possible?
Mathias,
Cheers for all your help. I don't know why, but it didn't like eval(jsonString), but if you use JSON.parse(jsonString). Worked first time.
So to clarify. To access the data, unless anyone has any better ways of doing it.
...var w = new Window("palette","Test",undefined,{resizeable:true});
var btn2 = w.add("button",undefined,"submit");
var file = app.project.selection[0].file;
file.open("r");
var jsonString = file.read();
file.close();
var Obj = JSON.parse(jsonString);
btn2.onClic
Copy link to clipboard
Copied
Yes, my free tool mamoworldJSON both reads and even creates such JSON files in the project panel:
mamoworldJSON - aescripts + aeplugins - aescripts.com
Something like that:
var file = of app.project.item(index).file;
file.open("r");
var jsonString = file.read();
file.close();
Copy link to clipboard
Copied
Sorry Mathias. I had it throw back an error as you had an of in yours, but I might be doing something wrong. Could you quickly glance and tell me why it's coming back with undefined. It's 2nd object in my panel.
So I changed that and in my Json, my first line is "Name": "Mexico", For testing purposes I just want to see it alert the text so I know it's working before I spend my time applying it to something else. I know it'll be something stupid.
var w = new Window("palette","Test",undefined,{resizeable:true});
var btn2 = w.add("button",undefined,"submit");
var file = app.project.item(2).file;
file.open("r");
var jsonString = file.read();
file.close();
btn2.onClick = function(){
alert(jsonString.Name);
};
w.show();
Copy link to clipboard
Copied
If you select the json file in the project panel, does it work with
app.project.selection[0].file
?
Then you can find the correct id with
app.project.selection[0].id
Copy link to clipboard
Copied
It came back with the number 12. I have no idea what that means hahahaha
Copy link to clipboard
Copied
but using (jsonString[0].Name). It still comes back with undefined
Copy link to clipboard
Copied
jsonString is just a string. To turn it into a json object you need to parse it.
var jsonObject = eval(jsonString);
Copy link to clipboard
Copied
I'm really sorry Mathias. I'm still not quite understanding this I get what you mean about converting the string into an object. Looking at other forums quickly. It seems to make sense. But
Adapting what you're suggesting I tried
- var w = new Window("palette","Test",undefined,{resizeable:true});
- var btn2 = w.add("button",undefined,"submit");
- var file = app.project.item(2).file;
- file.open("r");
- var jsonString = file.read();
- file.close();
- var var jsonObject = eval(jsonString);
- btn2.onClick = function(){
- alert(jsonObject[0].Name); (I also tried (jsonObject.Name)
- };
- w.show();
This didn't work. It came back with an error disliking my Json (Although I know the Json is fine)
Then I adapted something that works if you put the Json in the script. It worked with the str in the script. But this didn't work either.
var w = new Window("palette","Test",undefined,{resizeable:true});
var btn2 = w.add("button",undefined,"submit");
var str = app.project.selection[0].file;
var jsonObject = eval(str);
btn2.onClick = function(){
alert(jsonObject.Name);
};
var jsonObject = eval(str);
btn2.onClick = function(){
alert(jsonObject.Name);
};
w.show();
It one of those things that makes 75% sense, but the 25% is what I'm failing to understand.
Copy link to clipboard
Copied
Mathias,
Cheers for all your help. I don't know why, but it didn't like eval(jsonString), but if you use JSON.parse(jsonString). Worked first time.
So to clarify. To access the data, unless anyone has any better ways of doing it.
var w = new Window("palette","Test",undefined,{resizeable:true});
var btn2 = w.add("button",undefined,"submit");
var file = app.project.selection[0].file;
file.open("r");
var jsonString = file.read();
file.close();
var Obj = JSON.parse(jsonString);
btn2.onClick = function(){
alert(Obj.Name);
};
w.show();