Script fetch

Good evening everyone,

I need your help on a script.

fetch('https://hubeau.eaufrance.fr/api/v1/hydrometrie/observations_tr?code_entite=H505012001&size=3&pretty&grandeur_hydro=H&fields=code_station,date_debut_serie,date_fin_serie,date_obs,resultat_obs,continuite_obs_hydro')
  .then(response => response.json())
  .then(data => console.log(data))
  .catch(error => console.log(error));

I get this response

count: 8611,
  first: 'https://hubeau.eaufrance.fr/api/v1/hydrometrie/observations_tr?code_entite=H505012001&pretty&grandeur_hydro=H&fields=code_station,date_debut_serie,date_fin_serie,date_obs,resultat_obs,continuite_obs_hydro&cursor=&size=3',
  prev: null,
  next: 'https://hubeau.eaufrance.fr/api/v1/hydrometrie/observations_tr?code_entite=H505012001&pretty&grandeur_hydro=H&fields=code_station,date_debut_serie,date_fin_serie,date_obs,resultat_obs,continuite_obs_hydro&cursor=AoJw6KDW54sDPwxINTA1MDEyMF9INTA1MDEyMDAxX0hfNF8yMDIzLTExLTE0VDE5OjQwOjAw&size=3',
  api_version: '1.0.1',
  data: [
    {
      code_station: 'H505012001',
      date_debut_serie: '2023-11-14T00:05:00Z',
      date_fin_serie: '2023-11-14T19:50:00Z',
      date_obs: '2023-11-14T19:50:00Z',
      resultat_obs: 5750,
      continuite_obs_hydro: true
    },
    {
      code_station: 'H505012001',
      date_debut_serie: '2023-11-14T00:05:00Z',
      date_fin_serie: '2023-11-14T19:50:00Z',
      date_obs: '2023-11-14T19:45:00Z',
      resultat_obs: 5780,
      continuite_obs_hydro: true
    },
    {
      code_station: 'H505012001',
      date_debut_serie: '2023-11-14T00:05:00Z',
      date_fin_serie: '2023-11-14T19:50:00Z',
      date_obs: '2023-11-14T19:40:00Z',
      resultat_obs: 5820,
      continuite_obs_hydro: true
    }
  ]
}

How to extract just the first result from resultat_obs ?

Thank you so mutch

You can take a look here for examples:
https://community.homey.app/t/problem-reading-i-json-string/94164

try {
  const response = await fetch('https://hubeau.eaufrance.fr/api/v1/hydrometrie/observations_tr?code_entite=H505012001&size=3&pretty&grandeur_hydro=H&fields=code_station,date_debut_serie,date_fin_serie,date_obs,resultat_obs,continuite_obs_hydro');
  const data = await response.json();
  const value = data.data[0]?.resultat_obs;
  ...
} catch(error) {
  console.log(error);
}
  
(async () => {
  try {
    const response = await fetch("https://hubeau.eaufrance.fr/api/v1/hydrometrie/observations_tr?code_entite=H505012001&size=1&pretty&grandeur_hydro=H&fields=resultat_obs");
    const data = await response.json();

    // Traiter les données ici
    // console.log("Données brutes :", data);

    // Extraire la valeur du champ "resultat_obs" si elle existe
    const valeurObservation = data.data[0] ? data.data[0].resultat_obs : null;

    // Diviser la valeur par 1000
    const valeurDivisee = valeurObservation / 1000;

    console.log("Valeur de l'observation :", valeurObservation);
    console.log("Valeur divisée par 1000 :", valeurDivisee);

    await tag('hauteur_eau', valeurDivisee);
  } catch (error) {
    console.error("Erreur lors de la requête :", error);
  }
})();