react-native

console.log does not return json attribute value, only array - React Native


I have an array result coming from a fetch, and when I send a console.log(json) it shows me the following array data:

[{"accuracy": 0, "address": "Parque das Nações, Parnamirim, , Rio Grande do Norte, BR", "altitude": 0, "attributes": {"batteryLevel": 100, "blocked": false, "charge": true, "distance": 0, "hours": 69440243, "ignition": false, "motion": false, "rssi": 1, "status": 4, "totalDistance": 4544404.921780741, "type": 19}, "course": 349, "deviceId": 3, "deviceTime": "2025-03-21T19:31:03.110+00:00", "fixTime": "2025-03-21T18:49:15.000+00:00", "geofenceIds": null, "id": 13647, "latitude": xxxx, "longitude": xxxxxx, "network": null, "outdated": false, "protocol": "gt06", "serverTime": "2025-03-21T19:31:03.110+00:00", "speed": 0, "valid": true}, {"accuracy": 0, "address": "Rio Grande do Norte, BR", "altitude": 0, "attributes": {"batteryLevel": 100, "blocked": false, "charge": true, "distance": 0, "hours": 290877, "ignition": false, "motion": false, "rssi": 4, "status": 69, "totalDistance": 4181458.924858348, "type": 19}, "course": 237, "deviceId": 10, "deviceTime": "2025-03-21T19:31:48.024+00:00", "fixTime": "2025-03-21T19:08:02.000+00:00", "geofenceIds": null, "id": 13648, "latitude": xxxx, "longitude": xxxx, "network": null, "outdated": false, "protocol": "gt06", "serverTime": "2025-03-21T19:31:48.024+00:00", "speed": 0, "valid": true}]

but when I send console.log(json.address) it returns undefined. What's wrong?

fetch code

   async function buscarPosicoes() {
    fetch("https://xxx.xxx")
      .then((resposta) => resposta.json())
      .then((json) => {
        setPositions(json);
        console.log(json);
      })
      .catch((error) => console.error(error));

Solution

  • try with:

    console.log(json[0].address);