Skip to main content
Participant
January 8, 2024
Answered

JSON file name as a variable

  • January 8, 2024
  • 2 replies
  • 1200 views

Hi,

 

I’ve been using After effects and JSON files just for the sole purpose of displaying students grades as an animation. Because the name of the file has always been grades.json for every student, all I had to do was right click and replace the file to that of the correspoing student.

 

Now the JSON files will have unique names such as Alice-grades.json and Bob-grades.json but the code references grades.json, and I can’t right click and replace the file anymore unless I manually change the file name in the code per every student, every student is an After Effects project.

 

How can I have the JSON file name as a variable, so I would have a similar process of right click ,replace file. For example right clicking and replacing file Alice-grades.json to Bob-grades.json without having to modify the code.

 

Since there will only be one JSON file per project I can get away by just having the code to use whatever JSON file is in the current After Effects Project.

 

Am I taking the right aproach or there is something else I'm missing?

 

Thank you!

 

This topic has been closed for replies.
Correct answer Aaron Cobb

Have you considered, instead of referencing the footage through footage(), adding the JSON footage to your comp as a layer? Then you can reference the file via the layer's source property. If you rename the layer, you can still reference the layer by name, but it will be uncoupled from the footage name, so your code won't break when you replace its source (though AE's autofix feature might just update the references for you anyway, I prefer to make things foolproof).


So, supposing you add your JSON file to your comp as a layer and rename the layer "JSON file", you would replace 

footage("grades.json").sourceText

with

thisComp.layer("JSON file").source.sourceText

 

2 replies

Participant
January 9, 2024

(Edit: added code example)

The following is an example something that was working for me, aslong as I replace the JSON file with another one that has the same name, but now every single student has its JSON file name as their name, instead of each having a file name grades.json

 

eval("var valuefromjson=" +footage("grades.json").sourceText);
dra=valuefromjson[0].subjectmathnovember;
[value[0],dra]

 

I would need to change  "grades.json" to Alice-grades.json or Bob-grades.json, to get it to work, but since ther are close to 60 references to "grades.json" is not a viable process.

 

I would like to have a global variable like

var gradesjson = "filename"

That way I only need to change the file name once per project and insert the JSON file into the project

 

Where can I have the global variable? in empty text? empty layer? or somewhere else

 

My main concern is how and what is the most important things I should keep in mind when handling data from JSON files which names might change

 

the following image shows the old and new way the json file are named per every student.

 

 

 

Aaron CobbCorrect answer
Inspiring
January 9, 2024

Have you considered, instead of referencing the footage through footage(), adding the JSON footage to your comp as a layer? Then you can reference the file via the layer's source property. If you rename the layer, you can still reference the layer by name, but it will be uncoupled from the footage name, so your code won't break when you replace its source (though AE's autofix feature might just update the references for you anyway, I prefer to make things foolproof).


So, supposing you add your JSON file to your comp as a layer and rename the layer "JSON file", you would replace 

footage("grades.json").sourceText

with

thisComp.layer("JSON file").source.sourceText

 

Participant
January 9, 2024

Thank you!

Inspiring
January 9, 2024

Are you referencing the file from an expression or from a script? It might help if you post some of the code you are using, since the solutions will depend on exactly what you are trying to accomplish.

Participant
January 9, 2024

The following is an example something that was working for me, aslong as I replace the JSON file with another one that has the same name, but now every single student has its JSON file name as their name, instead of each having a file name grades.json

 

eval("var valuefromjson=" +footage("grades.json").sourceText);
dra=valuefromjson[0].subjectmathnovember;
[value[0],dra]

 

I would need to change  "grades.json" to Alice-grades.json or Bob-grades.json, to get it to work, but since ther are close to 60 references to "grades.json" is not a viable process.

 

I would like to have a global variable like

var gradesjson = "filename"

That way I only need to change the file name once per project and insert the JSON file into the project

 

Where can I have the global variable? in empty text? empty layer? or somewhere else

 

Thank you!