Reading JSON Help

I have always had problems with this.. I thought I had this worked out this time but nope..

I am looking for help with this. I am wanting to retrieve the name of the Away team from this JSON data set. See image..

My blocks are as follows. Is my logic sound?

Any help at all will be greatly appreciated.

Thanks!

1 Like

There are two blocks that may be of value for you. I've implemented a version with list by walking key path but you could also use get value at key path if you know that you only want the away team for the first game for the first date. You can do that by substituting the value 1 for the walk all at level blocks in the following code:

The difference is that the former will return a list of all possible values where walk all at level acts as a wildcard whereas the latter will return the first value matching the given path.

4 Likes

Thank you for this. Now let me see if I understand what you are doing here as I am not very familiar with the Dictionary blocks.

Your version will give me a list of all the Away team names available for that particular date? I am not sure what the "walk all at level" block does.. but it appears to be an Index of some sort?

1 Like

Yes, let me expand on that, but essentially the walk all at level block acts as a wildcard.

Effectively, you can read the list in reverse... that is "give me a list the of name of the team of the away of the teams for all games for all dates. If your endpoint returns a set of games for a specific team then you'll get a list of away teams head-to-head against that team. If the endpoint returns all home vs away games then you'll get a result with a different shape (the output logically mirrors the input). My code example was based on the screenshot you provided so depending on the whole response it may be different.

2 Likes

Thanks.. this is starting to make sense.

What I am trying to do is create a ticker of sorts, one that will display the Home and Away team for each game.. the games are for one day only, and there will be a different number of games each day.

The JASON file gives me all of that data on a daily basis so I will need to be able to refresh it each day.

I can see that it would be easy to substitute Home for Away in the list of tags, thats easy enough.

Pulling the rest of the data using your method may take a little work but this is also a learning experience for me. Scores, maybe the time on the game clock, and the number of games each day will be about it.

Let me figure this much out before I dive into the rest of that. Baby steps.

1 Like

Try this extension

1 Like

I visited the link you provided and saw all the examples, but I did not see a link to the .aix file anywhere..

1 Like

http://kio4.com/appinventor/299G_extension_Decodifica_Json.htm

2 Likes

Thanks @ewpatton I worked out my issue. Your method is a lot cleaner than mine was and I learned a little bit about using Dictionaries.

I do have another JSON issue though.. I worked out everything when there is data in my JSON import. But there will be occasions where sometimes there is no data. For example, my JSON data is of NFL football games. The NFL schedules games about 4 days a week. Sunday of course, Monday night, Thursday night, and sometimes Saturdays like in the playoffs. On a Tuesday for example when there are no games my Events tag is empty..

JSON01

When my blocks execute on a gameday all is well because there is data to read. On a non-game day I get the following error:

JSON02

..and I understand why. My question is how to handle this error gracefully .. when I try to check to see if list "Events" is empty I still get this error, I am guessing because the value is null? I cannot read a list/index of 1 if it does not exist. Is there a check for this and I just don't know about it?

Here are my blocks checking for data as of right now..

Your test is boloney.
You are testing constants, so the test result will never change.

To test if there is a home gave in the dict gameData, test if
is in list(dict values(gameData), 'home')

That gives you a true/false result.

I don't know how to proceed with that, because I can't see further into what you posted.

Hi ABG,

I think I understand what you are telling me but I am not sure your solution will work either? If I understood you then here is what I have now.. is this what you were saying?

But .. EVENTS is an empty array .. there is no value to check... the tag is there but the value is empty, or null..

JSON01

I hope I am describing this correctly..

Please download and post each of those event block(s)/procedures here ...
(sample video)

Don't clip the downloaded blocks image, or I won't be able to fix it in AI2.

is in list of values
Appli this test to the variable gameDict.

I see part of our confusion. The first part of this thread was for a completely different project. That issue is resolved. I am guessing I should have started a new post completely for this one. My apologies there..

Here is my entire project. Its small ..

FootballScores.aia (5.8 KB)

If you load this up you will see the two text blocks defining the URL for WEB1 .. the one on top does not work and the one on bottom does. One is an actual game day and the other is not. The JSON data file is different for a game day than it is for a non-game day.

JSON01

I understand what you are suggesting but that's not what I am asking/needing. The check generates an error because the list is empty/null on a non-game day. I am not able to check for values if they do not exist.

JSON02

I am just trying to figure out how to handle a non-game day when this error is generated. Is there a graceful way to handle this error?

Your block images don't show the all-important game days in the URLs.

Give me a date where there is no game.

The URL blocks are complete, I dont know how to stretch them to see the entire string?

This URL works as there was a game last night
https://site.api.espn.com/apis/site/v2/sports/football/nfl/scoreboard?dates=20211209

This URL does not work as there are no game today
https://site.api.espn.com/apis/site/v2/sports/football/nfl/scoreboard?dates=20211210

You can paste the URLs into a browser (or click the links above) to see the raw JSON data to help give you an idea of what I have to work with.

I included the .aia file of the project in my previous post.. that has everything in it.

Using a for each in list against the events value eliminates the chance of hitting a non-existent event.

FootballScores_ABG.aia (10.3 KB)


To accommodate that, I had to clip off two items from each path list in each event:

  • events
  • 1

This approach iterates through each event going into the same label overwriting the preceding event, so you will see only the last event.

Wow. Outstanding. I looked at your version and see how you handled my issue.. very creative. I do not know that I would have come up with that on my own. This solves my problem and I really appreciate the help.

I did learn from your solution and will be sure to use this technique in the future.

Thanks again!!

Did you ever finish this app? I am curious what the final product / solution is. Like did you create a Ticker Arrangement group for the 16 weekly games and make one invisible if no game that week ?
Or some other method ?