2

I am geting the following response from a Ajax call

[
    {
        "orderjson": "[{\"contact_phone_no\":\"9866545438\"},{\"contact_phone_no\":\"9866545438\"}]"
    }
]

I am trying to take the length of orderjson this way .

var data = 

    [
    {
        "orderjson": "[{\"contact_phone_no\":\"9866545438\"},{\"contact_phone_no\":\"9866545438\"}]"
    }
]

  console.log(JSON.stringify(data[0].orderjson.length));

But when i do so i am geting its length as 69 wheer as it is 2 .

Please tell me how to resolve this

1
  • It looks like you're double-encoding the returned JSON, which means the child property is being encoded as one large string. You should change the response format. Commented Mar 13, 2015 at 11:51

1 Answer 1

4

Using JSON.stringify you're transforming the JSON into a string then the length call gives you the length of the resulting string.

Just do:

console.log(JSON.parse(data[0].orderjson).length) 

and it should work.

You need JSON.parse because the orderjson content is a string.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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