Frank
Frank

Reputation: 2173

correct malformed JSON data

I'm using $.ajax to get JSON data from a REST API.

The problem is that the responseText I get is malformed so I get SyntaxError: JSON.parse: unexpected non-whitespace character error.

I found out that the problem is that the responseText is something like this:

"433
{"Result":{"Locale":"us","ServiceId":1111,"Name":"name",
"HDLevel":"HD Level 
5a0
Unknown","Category":"News","Subcategory":"ne
5b0
ws"}
}"

...

so it can't be parsed correctly to JSON.

I think I need a way to delete all those strings (433, 5a0, 5b0) and also delete new line characters. But I think I need a general way to delete those strings because there are more like them in my responseText and I can't know all names.

Any ideas on how I can do that and obtain a correct JSON? Thanks

edit:

the service uses JSON as format of the returned data and I'm using:

$.ajax({
    type: 'GET',
    url: URL,
    dataType: 'json', 
    success: function(obj) {

    },
    error: function( jqXHR, textStatus, errorThrown ) {

    },
});

I can't access the service server side so I can't edit any php or other languages issues.

Upvotes: 0

Views: 5416

Answers (1)

Sebastian S
Sebastian S

Reputation: 4712

Seems to me more like an unexpected http transfer encoding (chunked). Your actual JSON data is probably fine. Take a look at this question: jquery support Transfer-Encoding:chunked? how

Upvotes: 2

Related Questions