How to parse json from web

Hello everyone,

I’m trying to make myself something to check if school are open or closed. I found out this api that give a list of each school in JSON format that are closed
https://www.tvanouvelles.ca/api/schools/closed

Exemple of the content when something is closed here: https://github.com/mdallaire/domo-quebec/blob/main/tva/ecoles/donnees/closed.json

Now, I would like to be able to read that file, find one or more specific school (got the school ID here https://github.com/mdallaire/domo-quebec/blob/main/tva/ecoles/list.md) and then have an item on/off depending on if school is open.

I though about using an HTTP binding but I’m unsure how to do that.

Anyone have any idea on how to achieve that?

Thank you!

Tutorials and Solutions is a category to post a solution, not ask for one. I’ve moved this to a more appropriate category.

The HTTP binding is indeed the most appropriate approach. Have you looked at the add-on’s docs? Have you attempted to configure it and have something specific that confuses you or you don’t know what it means?

Over all, you’d follow a similar process as demonstrated in Getting Started for MQTT: Adding Things - Advanced | openHAB.

You’ll probably want to use a REGEX filter so all documents that do not contain the school you care about are ignored. You’ll use the JSONPATH transform to extract the data you want from the JSON.

Because the JSON is an array, using JSONPATH might be challenging so you could just link the whole JSON to a String Item and then use a Rule to parse and extract the data you want and update the Item that way.

sorry for the wrong place to post, I’ll try to remember it.

I did look at the add-on’s doc. Currently, I have the thing setup with the url pointed to it. I created a string channel with a string item that get all of the content. I though maybe use regex directly on the channel and create a channel for each school I want to monitor but it seems even more harder to do.

Now I have my item with the string in it. I’m trying to find how to get the regex right.

I’m on regex101 toying with the information. I’m trying to get

“id”: 36011,
“state”: “closed”,
“datetime_start”: “2021-02-16T05:25:41-05:00”,
“datetime_end”: “2021-02-16T20:45:00-05:00”,
“closure_informations”: [
{
“name”: “Pas de transport pour la journée”
}
]
}

from this

      }
            ]
          },
          {
            "id": 36011,
            "state": "closed",
            "datetime_start": "2021-02-16T05:25:41-05:00",
            "datetime_end": "2021-02-16T20:45:00-05:00",
            "closure_informations": [
              {
                "name": "Pas de transport pour la journée"
              }
            ]
          },
          {
            "id": 40052,
            "state": "closed",
            "datetime_start": "2022-01-16T19:13:09-05:00",
            "datetime_end": "2022-01-17T20:45:00-05:00",
            "message": "En raison des avertissements météorologiques en vigueur.Il n’y a pas de cours à distance puisque le calendrier scolaire prévoit que cette journée de classe sera reprise au cours des prochaines semaines.",
            "closure_informations": [
              {
                "name": "Cours suspendus"
              },
              {
                "name": "Pas de transport pour la journée"
              }
            ]
          },

(I took a snippet from the demo file to test regex). So far, I got it matching the beginning and everything else but I can’t get it to stop.

“id”: 36011(.|\n|\r|\t)*

My goal is to have all the information and then display it like the message, date, etc.

Regex is not the right tool for this.

Do you want to just have a Contact item that is OPEN or CLOSED depending on whether the given scool is currently open / closed?

Or do you want to have the list of closure periods, in which case you should probably have it in an array or something.

I tried HTTP Binding + JSONPATH, it works for basic / static / specific school ID.

  • Add a thing that points to the source URL
  • Add a String type channel, and in the State Transformation enter JSONPATH:$.result.institutions[?(@.id==1274)].closures
  • Link to a String Item

In the above case, the school ID is 1274.

You can do further simplify this depending on what outcome you want.

Hello,

My first goal was to simply have a on/off switch /contact for specific school that is open or closed (since I don’t need to know all of the school but only my kids one).

But seeing there’s other data like start/end date, reason and other stuff, it’s nice to have those info too

edit: just saw the 2nd answer, I’ll thinker around with what you just gave, pretty sure I’ll find it now. Thanks!

looking at the demo file, I just found out that sometime, the school is under result.institutions.id and other time it’s under result.institutions.[id].parents.id… anyway to parse one or the other? For now, I don’t know if that’s static aka always same path or if it’s variable depending on the context.

edit: it seems I badly understood and under “.parent” aren’t other school but other closure, looks like past closure.

Use code fences instead of quotes.

```
code goes here
```

And an important thing is REGEX works a little differently from normal in OH. The expression must match the entire String, newlines and all. The first matching group in the REGEX is what gets returned. Consequently a REGEX in OH is almost always going to be bracketed with .* and have one open and closed paren (see below).

The one place where REGEX is useful here is I don’t know that id=1274 will always exist in the JSON so chaining a REGEX filter before the JSONPATH will filter out those JSON and avoid errors in the logs. Something like

REGEX:(.*id=1274.*)∩JSONPATH:$.result.institutions[?(@.id==1274)].closures

The only way I can think of is to use a script transformation. This is kind of like a rule but it runs as a transformation. It gets passed the raw data and returns the transformed data. How it gets transformed is up to you. Here you could put in an if condition to pull the data from one place or the other.

It looks like this is moot but I wanted to be sure the question was answered.

Oh sorry I forgot to come back. I used a thing that get all the files. Then, I created a channel for each school I want and created multiple items under them for each value I want.

As for the parent nested one, I’m waiting for confirmation on the original dev, I think it’s only for like “this school is from this district”, I’m unsure.

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.