0
{"status":"success","responseMesg":"{\"totalDomains\":1,\"accepted\":0,\"rejected\":0,\"errors\":\"[{\\\"errorMessage\\\":\\\"Could not connect to XRP, max retry count exceeded\\\",\\\"domainEventId\\\":\\\"119\\\"}]\"}"}

Above the response of a service call. I used angular.fromJson to convert this to a JSON. I was able to access accepted, rejected, status, totalDomains, responseMesg properly. But I'm not able to access errorMessages. When I print errors, I see following

[{"errorMessage":"Could not connect to XRP, max retry count exceeded","domainEventId":"119"}]

Which looks like an array. But I'm not able to access individual objects in this array of objects like if arr is the array then I want to access arr[0].errorMessage, but not able to. It prints undefined. When I do arr.length, it printed 93. Is this just getting treated as string ? How to solve ?

6
  • did you try this err[0] ? Commented Jan 29, 2015 at 5:23
  • Yes I tried. Did not work. It printed undefined. Commented Jan 29, 2015 at 5:24
  • arr.err[0].errorMessage will work Commented Jan 29, 2015 at 5:25
  • That did not work as well. Commented Jan 29, 2015 at 5:27
  • Looks like there's an extra level of escaping from somewhere. Commented Jan 29, 2015 at 5:28

2 Answers 2

2

It looks like your serializing your JS objects to JSON on couple of levels. Here's why:

responseMesg is just a string, and your JSON after parsing looks like this:

{
    "status": "success",
    "responseMesg": "{\"totalDomains\":1,\"accepted\":0,\"rejected\":0,\"errors\":\"[{\\\"errorMessage\\\":\\\"Could not connect to XRP, max retry count exceeded\\\",\\\"domainEventId\\\":\\\"119\\\"}]\"}"
}

Now you need to parse responseMesg to JSON separately, here's the output:

{
    "totalDomains": 1,
    "accepted": 0,
    "rejected": 0,
    "errors": "[{\"errorMessage\":\"Could not connect to XRP, max retry count exceeded\",\"domainEventId\":\"119\"}]"
}

Next step is to parse errors which is again, only a string. Parsing will give you this:

[
    {
        "errorMessage": "Could not connect to XRP, max retry count exceeded",
        "domainEventId": "119"
    }
]

I have checked and JSONLint and angular.fromJson are giving me exactly the same output, so you could try this:

var a = angular.fromJson({"status":"success","responseMesg":"{\"totalDomains\":1,\"accepted\":0,\"rejected\":0,\"errors\":\"[{\\\"errorMessage\\\":\\\"Could not connect to XRP, max retry count exceeded\\\",\\\"domainEventId\\\":\\\"119\\\"}]\"}"});
a.responseMesg = angular.fromJson(a.responseMesg);
a.responseMesg.errors = angular.fromJson(a.responseMesg.errors);
console.log(a);

To avoid it in the future construct your JS object fully (without serializing) and then serialize the whole thing. If you don't have a control over nested serializing, try deserialize while constructing your response, if you detect a string.

Try using JSONLint to validate your JSON. Hope it helps.

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

3 Comments

I did try the solution you gave before posting this question, but definitely did some thing wrong while printing the value. This solution worked though. How do I deserialize ?
@TechCrunch angular.toJson serialize data (Object -> string), angular.fromJson deserialize data (string -> Object). So when you create your response and expect object or array, check whether data is just a string (angular.isString) and run angular.fromJson on it. Your responseMseg and errors are being returned as strings (already serialized) and not as objects as you would expect. And then they get just escaped...
@TechCrunch So if you have control over responseMseg and errors, and the way how they're returned, simply change it to be an object and not string.
0

you json is not correct the correct json is

    {
  "status": "success",
  "responseMesg": {
    "totalDomains": 1,
    "accepted": 0,
    "rejected": 0,
    "errors": [
      {
        "errorMessage": "Could not connect to XRP, max retry count exceeded",
        "domainEventId": "119"
      }
    ]
  }
}

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.