Parsing a .json File in Expressions
Copy link to clipboard
Copied
Greetings from Boise!
I'm animating a couple of dials in AE using an imported .json file. The file is from a weather station data logger and contains a time, a wind direction, and a wind velocity number at regular intervals recorded every several seconds (among a few other parameters).
What I'd like to do is-for example-is to have the Expression poll the .json file every few seconds and update the dial graphic (rotation) or a text box during rendering.
Does anyone have experience in coding such a thing? Or is anyone interested in the assignment? I am an experienced but out-of-practice .as coder with no time to work on this matter.
Thanks!
Copy link to clipboard
Copied
Reading the data into the project is easy: The After Effects 2018+ scripting engine has inbuilt support for JSON files as a project item, it automatically parses the contents into a data object. So you simply drop your JSON file into the project panel, then drag it to the New Comp button or into a new layer in an existing comp. Twirl open the layer and you'll see the data arranged as a tree. Here I added a text layer and pick-whipped the Source Text parameter to the "id" data object:
The JSON file just says:
{"menu": {
"id":"file",
"value":"whatever",
"popup": 2
}}
Of course if the file on disc changes, AE doesn't notice. You can right-click the JSON file in the project panel and "reload footage", or use scripted Scheduled Tasks to force the reload every few seconds, but none of that helps make sense of your statement
...to have the Expression poll the .json file every few seconds and update the dial graphic (rotation) or a text box during rendering.
After Effects isn't a live-streaming platform. YOU CANNOT CHANGE STUFF DURING A RENDER.
Copy link to clipboard
Copied
Thank you both for answering; I think I did a poor job of describing the matter, though.
I don't intend to live stream.
What I'm doing is collecting time lapse thermal video of structures as the sun moves across the sky. During that time lapse collection I also data log the microclimate around the structure using small, certified weather stations.
Back in the office I'll import the stills into PP and render the video. In AE I need to import the .json (converted from its native .xml) and display the vector dials over the video in such a way that as the Comp renders the weather dials updates indicating the weather parameters as the solar influences change.
No worries; I can certainly do this AN as I'm very comfortable with AS3 (and can use Greensock to make things a bit more interesting, visually), but it would be quite an elegant thing to do it in AE, especially since I know several folks who need the same functionality and would be happy to buy it. My problem is getting to it; just too busy...
Copy link to clipboard
Copied
If your JSON file is a pre-written array of data objects, then you import it and use an expression to get the value into whatever is controlling your graphics.
Unless you format your JSON into the special mograf format there's no automatic connection from an array of data objects to composition time, so you have to do it by hand. Suppose your imported JSON is just an array with one value per second, like this:
[
{ "mything": 0.01 },
{ "mything": 0.03 },
{ "mything": 0.41 },
{ "mything": 0.12 }
]
then you could put this in a text layer's Source text expression:
var timeval = Math.round(time);
footage("demo.json").dataValue([timeval,0]);
Unless we see your data file format and understand what it's going to drive on screen, that's as much help as we can provide.
Copy link to clipboard
Copied
Thank you, Dave. The data file (.json, from the original .xml) format is shown in this single node:
{
"FORMATTED DATE-TIME": "8/9/2018 16:28",
"Wind Dir": "354",
"Wind Spd": "7",
"Wind Spd": "0.8",
"Temp": "85.9",
"Rel Humid": "21.7",
"Heat Index": "81.9",
"Dewpoint": "42.4",
"Barometer": "30.05",
"Altitude": "2687",
"Density Altitude": "4961"
},
The video itself is just a series of time-lapse stills that I import into PP, let's say using 30 Frames Per Second in the Sequence, and each individual still displays for .2 second (depends on the intended playback speed, of course). I cut the time lapse video (I use Frame Blending) and import it into the Sequence (I don't use Dynamic Linking in these projects) and want AE to poll the .json (also imported as an Expression) every .2 seconds to update a few dials to show the Wind Direction, Wind Speed, and so on so that the video plays with synchronized weather display.
My apologies for delays in responding; am just overwhelmed with work...
Copy link to clipboard
Copied
OK, so you're saying that you have a single JSON file that some other application keeps overwriting, but it only ever has one set of values in it.
Then you're stuffed, because After Effects locks the timeline when rendering begins and has no way to combine data that no longer exists, with data that hasn't been written yet!
What you need is for your other application to APPEND to the JSON file, so you have a long (static) array of data objects, with exactly the same time interval between each one. Then you can use something like my previous reply to collect the "Nth" value, where N is worked out from the elapsed time and the interval between objects.
Copy link to clipboard
Copied
Well, you'll have to forgive me as I haven't explained things well enough. I do have a series of images taken over time; a time lapse. Let's just say 500 images. I have a .json collected over time, let's say there are 100 entries. So, there is a .json entry for every 5 images, temporally. I want to animate dials and text fields every 5 images so that the wind direction and speed are correct for the 5 images displaying as the video is rendered. I have a video pumping out at 15 frames per second, so AE needs to animate the dials 3 times per second by querying the .json file.
No worries. I know how to do this in ActionScript; I can simply animate the dials, cut an .avi, and import it into PP. I think it would be more elegant to do it in AE, of course. If I get a chance to do the coding I'll post it to this thread so that the intent is better indicated...although I am completely overwhelmed with work still and more than happy to pay a .js wonk to do the AE work...
Thanks much for your input!
Copy link to clipboard
Copied
OK, so assuming your JSON file is called "my.json" and contains an array of objects exactly like your yellow-highlighted example (which has "Wind Spd" twice, not good in the world of JSON!)
- Import the JSON file to your project. Doesn't need to be added to the composition.
- New text layer for "Wind Dir", which is in your fields at array index 1.
- Twirl down Text > Source Text and alt-click the stopwatch to set an expression. Type this..
var timeval = Math.round( time * 3 ); // increment three times a second
var f = 1; // the field you want to read
footage("my.json").dataValue( [timeval,f] )
- To get other fields, just change the variable f (0 = data-time, 1 = wind dir, 4 = temp, etc)
- If you want the value within a sentence, just join up some strings:
var timeval = Math.round( time * 3 );
var f = 4;
"Temperature = " + footage("my.json").dataValue( [timeval,f] ) + " F"
An AE expression evaluates to whatever the last line says. Also note that numeric values in JSON data are parsed as such, so you could do math on them (e.g. to convert deg F to C).
Copy link to clipboard
Copied
What I'd like to do is-for example-is to have the Expression poll the .json file every few seconds and update the dial graphic (rotation) or a text box during rendering.
Not possible. All files used for rendering get locked to the AE process and messing around with them while they are being accessed will simply stop the render or crash AE. And given that JSON stuff is still problematic in that even mundane simple files will sometimes not update even while working in AE, this plan is doomed to failure even if other prerequisites and limitations wouldn't apply.
Mylenium

