Kivylius
Kivylius

Reputation: 6557

Remove u' infront of my JSON data

I'm new to python and it seems that all my JSON data is combined with u' prefix as such:

{u'number': u'12345666', u"items"...}

I don't need this data (unicode or whatever) as I want to print the string into a Javascript variable:

var obj = data; // data is the object above.

My python looks something like this;

index.html:
var obj = ${data};

I'm using the moko framework for templating.

// getitems() return {'number':'12312...}
context = {'data': getitems(self)}
self.render_response('index.html',**context)

The processed javascript output data look like this:

var obj = {u'number': u'12345666', u"items"...} 

This is my problem.

Upvotes: 3

Views: 4253

Answers (1)

freakish
freakish

Reputation: 56477

The problem is that you are converting a dictionary to a string (probably Mako does str(...) for you). But you should jsonify it, i.e.

import json
context = { 'data': json.dumps(getitems(self)) }

Upvotes: 8

Related Questions