0

I have a script receiving a JSON from PHP. I have to parse the data. Unfortunately I'm not able to parse the JSON into an object and get the different arrays. I tried to validate the JSON but I cant find an error there either.

Code:

$.ajax({
  type: "POST",
  dataType: "html",
  url: "/sqldblink.php",
  data: data,
  success: function(data) {
    var recdata=data;
    console.log("Received data from listregistered:");
    console.log("Server reports:" + recdata);
    ListRegisteredResults(recdata);
  },
  error: function () {
    console.log("Failed to list!");
   }
});

function ListRegisteredResults(recdata) {
    console.log(typeof recdata);
    var data = JSON.parse(recdata);
    console.log(data);
    console.log(typeof data);
    console.log(data.Address.length);
}

The output:

Server reports:"{\"Address\":[\"Home\",\"\",\"Home,\nHoover House 85\",\"\",\"Home\",\"\",\"\",\"\",\"\",\"\"],\"BloodGroup\":[\"o+\",\"\",\"B+\",\"B+\",\"AB+\",\"o+\",\"o-\",\"\",\"\",\"\"],\"Occupation\":[\"Vendor\",\"\",\"Carpenter\",\"Playing\",\"Nurse\",\"IT professional\",\"Engineer\",\"Doctor\",\"\",\"\"],\"Alternate\":[\"0\",\"0\",\"925\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\"],\"Email\":[\"[email protected]\",\"\",\"[email protected]\",\"\",\"\",\"\",\"[email protected]\",\"\",\"\",\"\"],\"Mobile\":[\"90000006\",\"90000005\",\"90000005\",\"34344444\",\"902w0w05\",\"90002005\",\"900020w5\",\"90000005\",\"90002105\",\"90000005\"],\"Marital\":[\"Married\",\"Married\",\"Married\",\"Unmarried\",\"Unmarried\",\"Married\",\"Married\",\"Married\",\"Married\",\"Married\"],\"Gender\":[\"1\",\"2\",\"\",\"1\",\"1\",\"2\",\"2\",\"1\",\"1\",\"1\"],\"Age\":[\"28\",\"65\",\"35\",\"2\",\"25\",\"34\",\"31\",\"28\",\"60\",\"58\"],\"Name\":[\"Tracy Jim\",\"George Jose\",\"Jim G Mathew\",\"Cary jim\",\"Becky Mathew\",\"Cary Guy\",\"Arun Mose\",\"Tracy Kelly\",\"Dr Kim\",\"Steven Ludwig\"],\"HospitalID\":[\"3\",\"5\",\"6\",\"7\",\"8\",\"9\",\"10\",\"11\",\"12\",\"16\"]}"
string
{"Address":["Home","","Home,\nHoover House 85","","Home","","","","",""],"BloodGroup":["o+","","B+","B+","AB+","o+","o-","","",""],"Occupation":["Vendor","","Carpenter","Playing","Nurse","IT professional","Engineer","Doctor","",""],"Alternate":["0","0","925","0","0","0","0","0","0","0"],"Email":["[email protected]","","[email protected]","","","","[email protected]","","",""],"Mobile":["90000006","90000005","90000005","34344444","902w0w05","90002005","900020w5","90000005","90002105","90000005"],"Marital":["Married","Married","Married","Unmarried","Unmarried","Married","Married","Married","Married","Married"],"Gender":["1","2","","1","1","2","2","1","1","1"],"Age":["28","65","35","2","25","34","31","28","60","58"],"Name":["Tracy Jim","George Jose","Jim G Mathew","Cary jim","Becky Mathew","Cary Guy","Arun Mose","Tracy Kelly","Dr Kim","Steven Ludwig"],"HospitalID":["3","5","6","7","8","9","10","11","12","16"]}
string
userjs/main.js:347 Uncaught TypeError: Cannot read property 'length' of undefined
    at ListRegisteredResults (userjs/main.js:347)
    at Object.success (userjs/main.js:295)
    at i (jquery-3.2.1.min.js:2)
    at Object.fireWith [as resolveWith] (jquery-3.2.1.min.js:2)
    at A (jquery-3.2.1.min.js:4)
    at XMLHttpRequest.<anonymous> (jquery-3.2.1.min.js:4)

After I parse the string into JSON, why is it still being reported as being a string?

4
  • What does console.log(data.Address); say? Commented Mar 20, 2018 at 12:49
  • 1
    Well if string is the result of that last console.log(typeof data);, then you mustn’t wonder much ... Commented Mar 20, 2018 at 12:50
  • @Cerbrus undefined Commented Mar 20, 2018 at 12:51
  • My guess is the data sent by the server literally has outer quotes. In line with CBroe's comment, it's probably a JSON representation of a string, not a representation of an object. Commented Mar 20, 2018 at 12:52

3 Answers 3

2

Remove dataType: "html" as well as any JSON.parse() from your code and it'll work. jQuery automatically parses the data if you don't set wrong dataTypes, or json

$.ajax({
  type: "POST",
  url: "/sqldblink.php",
  data: data,
  success: function(data) {
    var recdata=data;
    console.log("Received data from listregistered:");
    console.log("Server reports:" + recdata);
    ListRegisteredResults(recdata);
  },
  error: function () {
    console.log("Failed to list!");
   }
});

function ListRegisteredResults(recdata) {
    console.log(typeof recdata);
    var data = recdata;
    console.log(data);
    console.log(typeof data);
    console.log(data.Address.length);
}
Sign up to request clarification or add additional context in comments.

1 Comment

Cool.. Thank you
2

You JSON was invalid. \n at position 28 was breaking it, you need to escape it.

"Address":["Home","","Home,\nHoover House 85"

Should be

"Address":["Home","","Home,\\nHoover House 85"

If you want to keep that \n.

let jsonString = `{"Address":["Home","","Home,\\nHoover House 85","","Home","","","","",""],"BloodGroup":["o+","","B+","B+","AB+","o+","o-","","",""],"Occupation":["Vendor","","Carpenter","Playing","Nurse","IT professional","Engineer","Doctor","",""],"Alternate":["0","0","925","0","0","0","0","0","0","0"],"Email":["[email protected]","","[email protected]","","","","[email protected]","","",""],"Mobile":["90000006","90000005","90000005","34344444","902w0w05","90002005","900020w5","90000005","90002105","90000005"],"Marital":["Married","Married","Married","Unmarried","Unmarried","Married","Married","Married","Married","Married"],"Gender":["1","2","","1","1","2","2","1","1","1"],"Age":["28","65","35","2","25","34","31","28","60","58"],"Name":["Tracy Jim","George Jose","Jim G Mathew","Cary jim","Becky Mathew","Cary Guy","Arun Mose","Tracy Kelly","Dr Kim","Steven Ludwig"],"HospitalID":["3","5","6","7","8","9","10","11","12","16"]}`;

let json = JSON.parse(jsonString);
console.log(json.Address);

Comments

0

This is happening only because of dataType: "html", that you are sending with your post request. SO the response you are getting with '\' so JSON.parse() can't handle them. Remove dataType from post request and then try, then you'll get correct response and your required result.

Code:

$.ajax({
  type: "POST",
  url: "/sqldblink.php",
  data: data,
  success: function(data) {
    var recdata=data;
    console.log("Received data from listregistered:");
    console.log("Server reports:" + recdata);
    ListRegisteredResults(recdata);
  },
  error: function () {
    console.log("Failed to list!");
   }
});

function ListRegisteredResults(recdata) {
    console.log(typeof recdata);
    var data = JSON.parse(recdata);
    console.log(data);
    console.log(typeof data);
    console.log(data.Address.length);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.