-1

I have the following JSON coming from a php ajax call and want to display some data to the users:

JSON

{"headers":{},"body":"{\"comuni\":[{\"datapresub\":\"08\/08\/2018\",\"datasub\":\"01\/02\/2018\",\"nomeComune\":\"ROMA\",\"provincia\":\"RM\"}]}","response":{"code":200,"message":"OK"},"cookies":[{"name":"bcf106ed7722e1b4a749630715ee3e66","value":"5338310d3fecaa76e2c9c583dfb02ddd","expires":null,"path":"\/","domain":"anpr-servizi-portale-anpr-portale-ocptest.ocpval.sogei.it","host_only":true}],"filename":null,"http_response":{"data":null,"headers":null,"status":null}}

This is my script:

success: function(data) {
                            console.log(data);
                            var jsonData = JSON.parse(data);  
                            if(jsonData[0].datasub != 0) {
                                $('.nome-comune').html(jsonData[0].nomeComune);
                                $('.provincia').html(jsonData[0].provincia);
                                $('.data-sub').html(jsonData[0].datasub);
                                resultsSub();
                            } else if(jsonData[0].datapresub != 0 && jsonData[0].datasub == 0) {
                                $('.nome-comune').html(jsonData[0].nomeComune);
                                $('.provincia').html(jsonData[0].provincia);
                                $('.data-presub').html(jsonData[0].datapresub);
                                resultsPresub();
                            } else if(jsonData[0].datapresub == 0 && jsonData[0].datasub == 0) {
                                $('.nome-comune').html(jsonData[0].nomeComune);
                                $('.provincia').html(jsonData[0].provincia);
                                noAnpr();
                            }     
                        }

I got the following error:

TypeError: jsonData[0] is undefined

Any idea to solve it?

  • 1
    You need to access the actual array using `jsonData.body.comuni[0]` and so on. `jsonData` is an object, not array – xxMrPHDxx Jul 09 '20 at 12:29

2 Answers2

1

Your object is not an array, you can't access it as jsonData[0]. Also your body JSON is inside a string, you have to parse it as well if you want to read from that.

Code:

const jsonData = {
  "headers": {},
  "body": "{\"comuni\":[{\"datapresub\":\"08\\/08\\/2018\",\"datasub\":\"01\\/02\\/2018\",\"nomeComune\":\"ROMA\",\"provincia\":\"RM\"}]}",
  "response": {
    "code": 200,
    "message": "OK"
  },
  "cookies": [
    {
      "name": "bcf106ed7722e1b4a749630715ee3e66",
      "value": "5338310d3fecaa76e2c9c583dfb02ddd",
      "expires": null,
      "path": "\\/",
      "domain": "anpr-servizi-portale-anpr-portale-ocptest.ocpval.sogei.it",
      "host_only": true
    }
  ],
  "filename": null,
  "http_response": {
    "data": null,
    "headers": null,
    "status": null
  }
}

const body = JSON.parse(jsonData.body)

// Let's check if "body.comuni[0].datasub" exists before working with it.
if (body && body.comuni && body.comuni[0] && body.comuni[0].datasub) {
    console.log(body.comuni[0].datasub)

    if(body.comuni[0].datasub != 0) {
        // Add the rest of your logic here.
    }
}
Telmo Trooper
  • 4,993
  • 1
  • 30
  • 35
0

You can see by printing the parsed json, it is an Object and not an Array.

var json = '{"headers":{},"body":{\"comuni\":[{\"datapresub\":\"08\/08\/2018\",\"datasub\":\"01\/02\/2018\",\"nomeComune\":\"ROMA\",\"provincia\":\"RM\"}]},"response":{"code":200,"message":"OK"},"cookies":[{"name":"bcf106ed7722e1b4a749630715ee3e66","value":"5338310d3fecaa76e2c9c583dfb02ddd","expires":null,"path":"\/","domain":"anpr-servizi-portale-anpr-portale-ocptest.ocpval.sogei.it","host_only":true}],"filename":null,"http_response":{"data":null,"headers":null,"status":null}}';

console.log(JSON.parse(json))
Greedo
  • 3,438
  • 1
  • 13
  • 28