Parsing data from JSON - EDP Distribuição

Hi.

I’m trying to parse some data from this json:

[
  {
    "Name": "Lisboa",
    "District": "Lisboa",
    "Geo_Point_County": "-9.15677178979339,38.73767450124629",
    "Geo_Point_District": "-9.1945933,39.016146",
    "field_code": 11,
    "Interruptions": "[{\"Date\":\"13\\/10\\/2019\",\"Start_time\":\"05:00\",\"Parish\":\"Zona urbana\",\"Address\":\"R. Atalaia, R. Barroca, Lrg. Calhariz, Trav. Espera, R. Rosa, R. Luz Soriano, Trav. Merc\\u00eas, R. Salgadeiras, R. Trombeta, Travessa Fi\\u00e9is Deus e imedia\\u00e7\\u00f5es \",\"Additional_information\":\"Conserva\\u00e7\\u00e3o de Rede\",\"Cancelled\":\"0\",\"End_time\":\"11:00\"}]"
  },
  {
    "Name": "Mafra",
    "District": "Lisboa",
    "Geo_Point_County": "-9.308886327661053,38.9611362162495",
    "Geo_Point_District": "-9.1945933,39.016146",
    "field_code": 11,
    "Interruptions": "[{\"Date\":\"13\\/10\\/2019\",\"Start_time\":\"07:00\",\"Parish\":\"Ericeira\",\"Address\":\": Estrada Nacional 247, Trav. Comandante Filipe Freire, Bairro Andorinhas, R. Boavista, Urb. Carrasqueira, R. Dr. Eduardo Burnay,  R. Bairro Jo\\u00e3o David Soares, R. Silvas, Praia Sul, R. Descobrimentos, R. Francisco L. Franco, Urb. Casal Gradil, R. Casal Gradil, Casal Rivoti, R. Manuel P. Santa Rosa, R. Comandante Manuel Freire, R. Comandante Filipe Freire, Avenida S. Sebasti\\u00e3o, R. Lombas, R. Carrasqueira Vila, Prct. Francisco L. Franco e imedia\\u00e7\\u00f5es\",\"Additional_information\":\"Conserva\\u00e7\\u00e3o de Rede\",\"Cancelled\":\"0\",\"End_time\":\"11:00\"},{\"Date\":\"13\\/10\\/2019\",\"Start_time\":\"07:00\",\"Parish\":\"Carvoeira\",\"Address\":\"Quinta Eduardo Alho e imedia\\u00e7\\u00f5es \",\"Additional_information\":\"Conserva\\u00e7\\u00e3o de Rede\",\"Cancelled\":\"0\",\"End_time\":\"11:00\"}]"
  }
]

The “name” and “district” are simple. But the “parish” inside the “interruptions” is not.

This does not work, giving no data at all:

sensor:
  - platform: rest
    name: 'EDPD Interrupções - 1'
    value_template: '{{ value_json[0].Interruptions.Parish }}'
    resource: https://www.edpdistribuicao.pt/pt-pt/county_interruptions?_format=json
    force_update: true

This one, just crashes HA because the content is bigger than 255 chars:

sensor:
  - platform: rest
    name: 'EDPD Interrupções - 1'
    value_template: '{{ value_json[0].Interruptions }}'
    resource: https://www.edpdistribuicao.pt/pt-pt/county_interruptions?_format=json
    force_update: true

Any help would be appreciated.

Thanks.

Yeah, the issue here is that the value of Interruptions is effectively JSON encoded twice.

You could try a regex search:

sensor:
  - platform: rest
    name: 'EDPD Interrupções - 1'
    value_template: '{{ value_json[0].Interruptions|regex_findall_index("\"Parish\":\"([^\"]*)") }}'
    resource: https://www.edpdistribuicao.pt/pt-pt/county_interruptions?_format=json
    force_update: true

The problem is that, although Jinja has a JSON dump function, it doesn’t have a JSON load (i.e., parsing) function.

Awesome, @pnbruckner! It works!

The second level from that json (“name: Mafra”) has multiple “parishes” (at least two). How it’s possible to make them all attributes for that sensor, recursively, please?

Thanks again.

The simple answer is that the regex_findall_index filter takes a second parameter, which is an index. The default is 0 (meaning the first), but you can set it to 1 (meaning the second.) So:

    value_template: '{{ value_json[1].Interruptions|regex_findall_index("\"Parish\":\"([^\"]*)", 1) }}'

would find the second parish in the second entry in the JSON list.

But honestly, you’re asking for something that is beyond what a REST Sensor and Jinja can do easily.

1 Like

Thanks.

But why the json_attributes crashes for more that 255 chars if, instead of declaring N rest sensors, I compile them as attributes in a single one?

Can you post how you did that?

The issue is an entity’s state (which is always converted to a single string) cannot be longer than 255 characters. Attributes, however, can be pretty much anything. So it depends on how exactly you tried to get all the information into one entity.