-3

I need the "u" prefixed removed because I am passing these json serialized lists over to the front end and handling them with javascript. Javascript can not make sense of these "u"'s.

Here is the code:

context['list_of_dicts'] = serialize('json', my_list_of_dicts)
# this function is wrapped with a @json response decorator

@json_response looks like:

def json_response(func):
    """
    A decorator thats takes a view response and turns it
    into json. If a callback is added through GET or POST
    the response is JSONP.
    """ 
    def decorator(request, *args, **kwargs):
        objects = func(request, *args, **kwargs)
        if isinstance(objects, HttpResponse):
            return objects
        try:
            data = simplejson.dumps(objects)
            if 'callback' in request.REQUEST:
                # a jsonp response!
                data = '%s(%s);' % (request.REQUEST['callback'], data)
                return HttpResponse(data, "text/javascript")
        except:
            data = simplejson.dumps(str(objects))
        return HttpResponse(data, "application/json")
    return decorator

On the frontend, I get the error:

Uncaught SyntaxError: Unexpected token u in JSON at position 0

That's because the "u" prefix has not been removed. How do I remove the "u" prefix so my frontend can decode the JSON?

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Mike Johnson Jr
  • 776
  • 1
  • 13
  • 32

1 Answers1

6

You are serialising your data three times. First with:

context['list_of_dicts'] = serialize('json', my_list_of_dicts)

and then two times more with:

data = simplejson.dumps(str(objects))

This encodes a str() representation of the objects returned from your view function, which is then converted to a JSON string document. The str() conversion adds the u prefix; you are serialising a Unicode string object literal:

>>> context = {}
>>> context['list_of_dicts'] = u'<Some JSON literal here, correctly encoded>'
>>> import json
>>> json.dumps(str(context))
'"{\'list_of_dicts\': u\'<Some JSON literal here, correctly encoded>\'}"'

Your decorator has several more issues:

  • you are using a blanket except handler, so you are masking whatever error the first attempt at serialising with simplejson.dumps() threw. Never use a blanket except and silence the exception. You now don't know what went wrong there. Also see Why is "except: pass" a bad programming practice?

  • you appear to be serialising objects into a dictionary; if you want to send back a JSON object, construct something consisting only of Python objects first, then serialise the whole.

  • if you need custom serialisation logic (like using the Django serialisation framework), don't use a @json_response decorator to re-encode, or at the very least return a HttpResponse instance to avoid being serialised again.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343