ADOBE AFTER EFFECTS: Forum Expressions Tutorials Creative Cloud

Expression for json-file

COW Forums : Adobe After Effects Expressions

<< PREVIOUS   •   FAQ   •   VIEW ALL   •   PRINT   •   NEXT >>
Johannes Jonasson
Expression for json-file
on Jul 3, 2020 at 9:30:03 am

Hi,

I've never used a .json file in an expression before. This is the data, and I've downloaded the file to my computer and imported it to After Effects:
https://raw.githubusercontent.com/owid/covid-19-data/master/public/data/owi...

I want to make a list with all countries, and I want the list to change depending on the variable total_deaths_per_million for every date.

I realise my expression is far off. My first problem is that I can't work out how I get the right variable. With the expression I've tried I get the error message "Couldn't turn result into numeric value".

First of all, is it even possible to create an expression that works like I want it to? Anyone have any good advices?

Kind regards

footage("owid-covid-data.json").sourceData;

p1="AFG"[1].total_deaths_per_million;
p2="AFG"[2].total_deaths_per_million;

exDepth = 360

min = 0;
max = 1000;

startVal = ((p1 - min)/max)*exDepth;
endVal = (p2/max)*exDepth;

z = ease(time, 1, 4, startVal, endVal);

x=640;
y=z;

[x,y];


Return to posts index

Filip Vandueren
Re: Expression for json-file
on Jul 3, 2020 at 12:25:46 pm

You want to make a list of countries…so what does this expression have to return ?
a textSource with countries ordered by total_deaths_per_million ?



Return to posts index

Johannes Jonasson
Re: Expression for json-file
on Jul 3, 2020 at 12:38:02 pm

Yes, that is what I am trying to do. But I also want the countries to change order depending on the date.


Return to posts index


Filip Vandueren
Re: Expression for json-file
on Jul 3, 2020 at 1:26:12 pm

This works, surprisingly well and fast given the huge dataset…

date = "2020-05-11";

data = footage("owid-covid-data.json").sourceData;
countries = Object.keys(data);

results=[];
for (c=0; c element.date === date).total_deaths_per_million;
} catch(e) {
tdpm=0;
}
results.push( [data[countries[c]]["location"], tdpm] );
}

results.sort( (a,b) => b[1]-a[1] );
results.reduce( ( acc, cur ) => acc + cur[0]+"—"+cur[1]+"\r", "");




Return to posts index

Filip Vandueren
Re: Expression for json-file
on Jul 3, 2020 at 1:34:01 pm

Sorry, problems with the <-symbol in the previous post…

date = "2020-05-11";

data = footage("owid-covid-data.json").sourceData;
countries = Object.keys(data);

results=[];
for (c=0; c<countries.length; c++) {
try{
tdpm= data[countries[c]]["data"].find( element => element.date === date).total_deaths_per_million;
} catch(e) {
tdpm=0;
}
results.push( [data[countries[c]]["location"], tdpm] );
}

results.sort( (a,b) => b[1]-a[1] );
results.reduce( ( acc, cur ) => acc + cur[0]+"—"+cur[1]+"\r", "");




Return to posts index

Johannes Jonasson
Re: Expression for json-file
on Jul 3, 2020 at 2:01:58 pm

Tried it now, but I dont't seem to get it correct (Sorry, I'm a total newbie when it comes to this).

I copied your expression into Expression: Source Text in an empty text layer. But I do not get any text out of it. So what am I doing wrong here?


Return to posts index


Filip Vandueren
Re: Expression for json-file
on Jul 3, 2020 at 2:04:58 pm

Did you try the second post ?

Does the name of the Footage-file match yours ?

Is the Javascript Engine in Project Settings set to "Javascript", not "Extendscript Legacy"

Are you getting an empty layer and no error, or an error ?



Return to posts index

Johannes Jonasson
Re: Expression for json-file
on Jul 3, 2020 at 2:12:11 pm

Yes, tried the second post, and the footage-file name is the same as yours. Also checked the Javascript engine at it was set to "Javascript".

I get an empty layer and no error.

Really appreciate your help by the way!


Return to posts index

Johannes Jonasson
Re: Expression for json-file
on Jul 3, 2020 at 2:15:57 pm

Tried it again and now I got a list of text.
But on every row it says "undefined–0"....


Return to posts index


Johannes Jonasson
Re: Expression for json-file
on Jul 3, 2020 at 2:28:38 pm

Update! Now it seems to be working perfectly, I just had to reload the footage file.

Thank you very much for your help! Was worth alot!


Return to posts index

Filip Vandueren
Re: Expression for json-file
on Jul 3, 2020 at 4:04:50 pm

Let me know how it turns out! Or if you need more help.



Return to posts index

Johannes Jonasson
Re: Expression for json-file
on Jul 6, 2020 at 9:09:16 am

Ah, a problem occured. I have to change the data set to WHO:s data set.
https://covid19.who.int/?gclid=Cj0KCQjwl4v4BRDaARIsAFjATPkGlMd_GDm_2iQ96kJ5...

(Deaths > "Download map data").

But then i get an error message.
"TypeError: Cannot convert undefined or null to object
countries = Object.keys(data);".

And I guess I have to change pretty much more in the expression later on? But I have no clue tbh


Return to posts index

Filip Vandueren
Re: Expression for json-file
on Jul 6, 2020 at 9:28:04 am

Of course, you have to know the structure of the JSON to know how to retrieve data from it.
The example I coded was made especially for the dataset you referred to.



Return to posts index

Filip Vandueren
Re: Expression for json-file
on Jul 6, 2020 at 11:10:44 am

Ouch, it's not even a JSON this time I see.

I've found that working with CSV of this large set is paaaaainfully slow.

It's much much faster to just look at the sourceText of the csv and do some parsing ourselves with regular expressions working on the strings:


f=footage("WHO-COVID-19-global-data.csv");
st=f.sourceText.replace(/(,"[^"]*),([^"]*")/g, "$1-$2");
// a first replace() to get rid of pesky comma's inside of region names

date ="2020-05-01";
re = new RegExp(date+".*","g");

// match strings (lines) starting with our date
matches = st.match(re);

// string split into array
matches=matches.map( (r) => r.split(",") );

// only keep element 3 and 7 (country name, daily deaths)
matches=matches.map( (r) => [r[2], r[6]]);

// reverse sort by 2nd element (deaths)
matches.sort( (a,b) => b[1]-a[1] );

// reduce array into a string
matches.reduce( ( acc, cur ) => acc + cur[0]+"—"+cur[1]+"\r", "");




Return to posts index

Johannes Jonasson
Re: Expression for json-file
on Jul 6, 2020 at 12:51:33 pm

Wow! This works perfectly!

Now I have two new questions;
Is there some way to change this data each second with an expression? So like first second shows 1st of january, and second 15 shows 15th of january? I've been trying this with multiple "if" statements, but it is alot of work to do it by hand.

And if so, is it then possible to make the countries change place smoothly? Whit this expression they just pop up at a different place, but I want the change of position to be visible. So that when one country gets a higher value you can see it move up along the list.


Return to posts index

Filip Vandueren
Re: Expression for json-file
on Jul 6, 2020 at 12:57:58 pm

Get the date from the sourceText of a different layer.

You can keyframe the dates manually, or it’s easy enough to write an expression to increase the date every second.

Your second point: yes this is possible, but not (easy) in 1 text-layer.

Are you doing this for non-profit. Because I don’t want to be building this whole thing for someone else to reap the benefits if you get what I mean.



Return to posts index

Johannes Jonasson
Re: Expression for json-file
on Jul 6, 2020 at 1:24:44 pm

Totally understand. But I'm trying to learn more about expressions during the summer, and thought this would be an interesting animation to put on like YouTube (as a hobby).


Return to posts index

Filip Vandueren
Re: Expression for json-file
on Jul 6, 2020 at 1:37:29 pm

Ok, I thought you had a client because you mentioned you now have to use another dataset.

Anyway; is it going to stay a list of text and numbers , or are there going to be graphs, color-coding,... maps,...
best to plan a bit ahead and design a mock-up of what you want before trying to make it data-driven.



Return to posts index

Tomas Bumbulevičius
Re: Expression for json-file
on Jul 11, 2020 at 11:06:45 am

Filip has a good point here - prior planning is needed, in order to make it happen in a more clear way, with less friction and back and forth. Yet solely on my previous experiences in this field of data-driven animations, making them comparable through expressions is an overkill - too slow, too limited. While it has benefits of instant data updates when updating the file - its not beneficial enough IMO.

Nor it provides enough flexibility and easiness of customisation. My vote would be to go scripting instead, but of course - learning how to achieve similar through expressions is a good path to familiarise with expressions world!

Find out more:
Motion Graphics Design & After Effects Tutorials
On YT
On VH


Return to posts index

<< PREVIOUS   •   VIEW ALL   •   PRINT   •   NEXT >>
© 2020 CreativeCOW.net All Rights Reserved
[TOP]