0
[
  [
    {
      "Id": 1234,
      "PersonId": 1,
      "Message": "hiii",
      "Image": "5_201309091104109.jpg",
      "Likes": 7,
      "Status": 1,
      "OtherId": 3,
      "Friends": 0
    }
  ],
  [
    {
      "Id": 201309091100159,
      "PersonId": 1,
      "Message": "heyyyyyyyy",
      "Image": "",
      "Likes": 2,
      "Status": 1,
      "OtherId": 3,
      "Friends": 3
    }
  ]
]

I am trying to parse this JSON data in javascript,but its giving an error "SyntaxError: JSON.parse: unexpected character" Tell me how to parse or access this json data and how to get numbers of records saved in JSON data.

I am running this on firefox ..Please help me to resolve this problem.Thanks in advance

8
  • 1
    The JSON is valid (even though the structure is a bit odd). Maybe you are trying to parse something that is not JSON. Please post your code. Commented Sep 19, 2013 at 8:58
  • This is not JSON. ITs just object literal. Post your JSON which is string. Commented Sep 19, 2013 at 8:59
  • 4
    @shiplu.mokadd.im: This is JSON. Of course in JavaScript (or Python, Java, <insert language here>) JSON can only exist in a string, but JSON is a language independent data format. You can only talk about object literals in JavaScript source code, but the "code" posted here is not JavaScript. Commented Sep 19, 2013 at 8:59
  • 1
    One chance is there may be unicode character some where in your object like here : {‌} you are not seeing it which is inside curly braces. Paste it to notepad and use backspace you will find three chars there. Commented Sep 19, 2013 at 9:10
  • 1
    @FelixKling This JSON was returned by using res.send(rows) method in node.js and rows are the records selected from database. And i m receiving this JSON from a ajax function or may be i can access data like msg[0][0].Id where msg is response returned to ajax function. Commented Sep 19, 2013 at 9:24

2 Answers 2

2

If your msg variable is already a Javascript object literal you can access properties in it directly like you said : msg[0][0].Id

If it is a string you can use JSON.parse() function to obtain a JS object: Parse JSON in JavaScript?

Fiddle: http://jsfiddle.net/nilgundag/EdWKv/1/

var msg1 = [
  [
    {
      "Id": 1234,
      "PersonId": 1,
      "Message": "hiii",
      "Image": "5_201309091104109.jpg",
      "Likes": 7,
      "Status": 1,
      "OtherId": 3,
      "Friends": 0
    }
  ],
  [
    {
      "Id": 201309091100159,
      "PersonId": 1,
      "Message": "heyyyyyyyy",
      "Image": "",
      "Likes": 2,
      "Status": 1,
      "OtherId": 3,
      "Friends": 3
    }
  ]
];
$("#first").text(msg1[0][0].Id);

var myJSONString = '[[{"Id": 1234,"PersonId": 1,"Message": "hiii","Image": "5_201309091104109.jpg","Likes": 7,  "Status": 1,     "OtherId": 3,      "Friends": 0    }  ],  [    {      "Id": 201309091100159,      "PersonId": 1,      "Message": "heyyyyyyyy",      "Image": "",      "Likes": 2,      "Status": 1,      "OtherId": 3,      "Friends": 3    }  ]]';
var msg2 = JSON.parse(myJSONString);
$("#second").text(msg2[0][0].Id);
Sign up to request clarification or add additional context in comments.

1 Comment

Thankss for ur answer,, now its working. I am just counting length of numbers of records returned in JSON ,using count=msg.length; and accessing all element in loop for(var i=0;i<count;i++){ msg[i][0]['Id']; }
0

I thought that the json str is valid。but when that string isn't in JS enviroment..that's format is invalid.

1 Comment

It's valid JSON, it doesn't matter in which environment it is.

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.