[Fixed]-Invalid Characters in JSON after unicode conversion

1👍

This is not a JSON issue. This is an issue with how you generate your JavaScript. You are trying to treat string data as JavaScript objects.

Moreover, those are Python data structures, not JSON lists, because you decoded them already. There is also no need to encode everything to UTF-8 here. Last but not least, you could just leave the venue structure as dictionaries / JSON objects as you don’t need those lists separately.

You’ll need to convert your lists back to JSON to insert them as JavaScript list literals into your generated JavaScript, so that you can then treat them as JavaScript objects (once the browser has parsed those literals).

data = json.load(urllib2.urlopen(url))

raw_dts = json.dumps([i["datetime"] for i in data])

ticket_urls = json.dumps([i["ticket_url"] for i in data])
ticket_statuses = json.dumps([i["ticket_status"] for i in data])
venues = json.dumps([i["venue"] for i in data])

and your template:

var $divline = $("<div id = ticketObj></div>");
var venues = {{ venues|safe }};

console.log("length of artist list" + venues.length );
for(var i = 0; i < venues.length; i++){

    $divline.append('Artist {{form_artistSelect}} Location: ' +  venues[i].city + ', ' + venues[i].region + ' Venue: ' + venues[i].name
 ); 

}
var $resultObj = $('<div id = "inviteObj" style="margin:100px"></div>');
$resultObj.append($divline);
$resultObj.appendTo($("#results"));

Here the {{ venues|safe }} line tells Django to not HTML-escape the values, because they have already been encoded to JSON instead.

Leave a comment