-1

problem facing to show the name in maindata, whereas maindata is showing perfectly but maindata->name : undefined. maindata is longtext column in MySQL

//this is the jQuery code section below
//original data stored in MySQL database in column MAINDATA
{
  "name":"SOUMITRA AND & SARKAR",
  "address":"24/1 Sahid Ananta Dutta Sarani, P.O. Rajbari,Kolkata 700 081",
  "stateid":["19","20","21"],
  "ref":{"noref":01,"myref":"02"}
 }

//retuning after AJAX call in jQuery script the $.parseJSON value is below

[
  {
    "maindata":"{
        "name":"SOUMITRA AND & SARKAR",
        "address":"24/1 Sahid Ananta Dutta Sarani, P.O.Rajbari, Kolkata 700 081",
        "stateid":["19","20","21"],
        "ref":{"noref":01,"myref":"02"}
     }"
   }
]


for (var i=0;i<pass_data.length;i++)
{
    //maindata is showing perfectly
    alert(pass_data[i].maindata);
}
for (var i=0;i<pass_data.length;i++)
{
    alert(pass_data[i].maindata["address"]; // undefined
}

I have shown it in my question, so far I found that parse data is doing something wrong. after maindata there is { second bracket which is within quotes.

1
  • 2
    It's in quotes, because it is a string value. On first glance it looks like this was JSON-encoded data again - but the "noref":01 value makes it invalid JSON. So if you want to be able to decode it as JSON, you will need to fix that error first.
    – C3roe
    Commented Jun 14, 2023 at 7:48

1 Answer 1

0

In your returned value maindata is a string, so you can't access the properties within. You have to parse the string again to make the properties accessible.

For example:

const ajaxResult = [
  {
    "maindata":"{
        "name":"SOUMITRA AND & SARKAR",
        "address":"24/1 Sahid Ananta Dutta Sarani, P.O.Rajbari, Kolkata 700 081",
        "stateid":["19","20","21"],
        "ref":{"noref":01,"myref":"02"}
     }"
   }
]

ajaxResult[0].maindata = JSON.parse(ajaxResult[0].maindata)

console.log(ajaxResult[0].maindata.address) // "24/1 Sahid Ananta Dutta Sarani, P.O.Rajbari, Kolkata 700 081"
0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.