Reputation: 71
I was noticing that in my client code, I was missing some(but not all) properties on my service response. When I looked in the dev tools Network tab in Chrome, I found that the service response seemed to include these missing properties but the object returned from the Angular http client did not. So for debugging purposes, I tried to configure my http client request options to interpret the response body as text and return a string and logged it. Then I did a JSON.parse(responseText) after. The logged responseText had the full set of properties that I expected but the JSON.parse failed with an unexpected token exception. So I tried to inject JSON.stringify between the response and parse after finding that some escape backslashes seemed to be causing the parse issue. I logged the stringified response as well and it still showed the full set of expected properties. And the JSON.parse passed after adding the stringify as well. However, the JSON.parse caused the original missing properties to be dropped. I don't get a parse error in this case. Just missing some properties.
The first missing property from the source string is a simple property called id that has a UUID string value in it. And the thre properties after it are missing as well.
{
"a": "A",
"b": "B",
"c": "C",
"id": "726abb0e-6cc6-4496-94a2-906439c06a30",
"1": 12345,
"2": "stringVal",
"3": "stringValue"
}
parsing this string results in
{
"a": "A",
"b": "B",
"c": "C"
}
the full JSON is much more complex and what I've included here is a single instance that is inside an array of several of these types. All of the instances in the array are missing the same properties. So it doesn't stop parsing the full json and it still parses other instances within the array.
Upvotes: 0
Views: 2573
Reputation: 71
It turns out that the real answer to my question was that the parsing was NOT excluding some properties. Rather I was being misled by the console statements in Chrome. What I experienced was that the console log only evaluates the state of the object logged when you expand it in the log. I was assuming that it represented the state at the time the log entry was written. What was happening was that the reference was being modified later in the code and the console was reflecting the changed object reference as I was reading it AFTER the other code made changes to the object. Thanks to all that read through this and offered any suggestions but I'm afraid it was my own misunderstanding of how this worked that was the true cause of the issue.
Upvotes: 4
Reputation: 2004
The reason is JSON.parse is not working properly with special charaters.The solution for this is as follows:
function escapeUnicode(str) {
return str.replace(/[^\0-~]/g, function(ch) {
return "\\u" + ("0000" + ch.charCodeAt().toString(16)).slice(-4);
});
}
Upvotes: 0
Reputation: 308
I'm not sure why you're getting just a partial return, but your JSON string doesn't look valid. you need to have a colon :
after your "a"
, "b"
, and "c"
properties. The following should parse properly.
{
"a": "A",
"b": "B",
"c": "C",
"id": "726abb0e-6cc6-4496-94a2-906439c06a30",
"1": 12345,
"2": "stringVal",
"3": "stringValue"
}
Upvotes: 0