Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot parse JSON object containing escape characters

I'm new to javascript and just learning AJAX calls and parsing JSON objects so I know I'm just missing something obvious. I can retrieve the JSON string from my API but I cannot parse it correctly. I'm not sure if I'm sending a JSON object that cannot be parsed or just trying to read the fields in the wrong way. Thanks for taking the time to read this and your help is greatly appreciated I'm just at a loss for where to go next.

I can get the JSON string by this.responseText but when I try to access the field Title I only get undefiend. I'm trying to access it this way: this.responseText.title I've also tried: this.responseText[title] and this.responseText["title"]

"{\"Id\":220,\"Title\":\"Drawtober 19\",\"YearCreated\":0,\"DatePublished\":\"2018-12-14T03:27:05.51\"}" 

is what I get from the AJAX call and my attempt to get the title:

var xhttp = new XMLHttpRequest();
        xhttp.onreadystatechange = function() {
            if (this.readyState == 4 && this.status == 200) {
                let x = this.responseText;
                let firstTest = JSON.parse(x[0]);
                let secondTest = JSON.parse(x.Title);
            }
        };
        xhttp.open("GET", "http://www.faithfulimagination.com/api/artwork/220", true);
        xhttp.send();
        }

I'm expecting to see "Drawtober 19" and all I get is 'undefined'

EDIT

The issue was originally in my API as Barmar pointed out. I was calling JsonConvert.SerializeObject and returning a string rather than returning just the object. Calling JSON.parse(x) twice worked perfectly as did fixing my API and only having to call it once.
Thank you all for answering so quickly! It seems everyone picked up on my problem right away.

like image 959
Charles Cole Avatar asked May 11 '19 09:05

Charles Cole


People also ask

Does JSON support escape characters?

In JSON the only characters you must escape are \, ", and control codes. Thus in order to escape your structure, you'll need a JSON specific function.

Can JSON have special characters?

If the source data contains special characters, the FOR JSON clause escapes them in the JSON output with \ , as shown in the following table. This escaping occurs both in the names of properties and in their values.

How do I escape a string in JSON?

JSON is pretty liberal: The only characters you must escape are \ , " , and control codes (anything less than U+0020). This structure of escaping is specific to JSON. You'll need a JSON specific function. All of the escapes can be written as \uXXXX where XXXX is the UTF-16 code unit¹ for that character.


1 Answers

Your response is encoded twice, so you need to decode it twice:

let data = JSON.parse(JSON.parse(x));
let title = data.Title;

There's no good reason for double encoding. If faithfulimagination.com is your site, you should fix it.

like image 167
Barmar Avatar answered Oct 19 '22 02:10

Barmar