librelist archives

« back to archive

Strange problem with ajax call and jsonify

Strange problem with ajax call and jsonify

From:
Alessio Civitillo
Date:
2012-05-06 @ 14:34
Hello,

I am making an ajax call with jquery on a url that is supposed to return
json. To return a json I am first building a dictionary (d) (with no tuples
or sets) and then the view returns: jsonify(d).

However, if there are numbers in the json, it will give something like:


   -

   raise TypeError(repr(o) + " is not JSON serializable")


TypeError: 100 is not JSON serializable

While all works if in the json there are numbers. What could be the issue
here?

* A copy of the json giving me trouble is at http://pastebin.com/zxDnV5mP.

Stack trace below:

Traceback (most recent call last):
  File
"/usr/lib/python2.7/site-packages/Flask-0.7.2-py2.7.egg/flask/app.py", line
1306, in __call__
    return self.wsgi_app(environ, start_response)
  File
"/usr/lib/python2.7/site-packages/Flask-0.7.2-py2.7.egg/flask/app.py", line
1294, in wsgi_app
    response = self.make_response(self.handle_exception(e))
  File
"/usr/lib/python2.7/site-packages/Flask-0.7.2-py2.7.egg/flask/app.py", line
1292, in wsgi_app
    response = self.full_dispatch_request()
  File
"/usr/lib/python2.7/site-packages/Flask-0.7.2-py2.7.egg/flask/app.py", line
1062, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File
"/usr/lib/python2.7/site-packages/Flask-0.7.2-py2.7.egg/flask/app.py", line
1060, in full_dispatch_request
    rv = self.dispatch_request()
  File
"/usr/lib/python2.7/site-packages/Flask-0.7.2-py2.7.egg/flask/app.py", line
1047, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/home/alessio/projects/07_datacrowd/datacrowd/views/views.py", line
74, in productsAoP_df
    return jsonify(dataOut)
  File
"/usr/lib/python2.7/site-packages/Flask-0.7.2-py2.7.egg/flask/helpers.py",
line 114, in jsonify
    indent=None if request.is_xhr else 2), mimetype='application/json')
  File "/usr/lib/python2.7/json/__init__.py", line 231, in dumps
    return _default_encoder.encode(obj)
  File "/usr/lib/python2.7/json/encoder.py", line 201, in encode
    chunks = self.iterencode(o, _one_shot=True)
  File "/usr/lib/python2.7/json/encoder.py", line 264, in iterencode
    return _iterencode(o, 0)
  File "/usr/lib/python2.7/json/encoder.py", line 178, in default
    raise TypeError(repr(o) + " is not JSON serializable")
TypeError: 100 is not JSON serializable

Re: [flask] Strange problem with ajax call and jsonify

From:
Simon Sapin
Date:
2012-05-06 @ 15:15
Le 06/05/2012 16:34, Alessio Civitillo a écrit :
> I am making an ajax call with jquery on a url that is supposed to return
> json. To return a json I am first building a dictionary (d) (with no
> tuples or sets) and then the view returns: jsonify(d).
>
> However, if there are numbers in the json, it will give something like:
>
>   *
>
>     raise TypeError(repr(o) + " is not JSON serializable")
>
>     TypeError: 100 is not JSON serializable
>
> While all works if in the json there are numbers. What could be the
> issue here?
>
> * A copy of the json giving me trouble is at http://pastebin.com/zxDnV5mP.
>


Hi,

Strange indeed, numbers are one of the basic JSON types.

I can’t reproduce the problem, this works fine for me:

 >>> url = 'http://pastebin.com/raw.php?i=zxDnV5mP'
 >>> jsonify(eval(urllib.urlopen(url).read())).data[:18]
'{\n  "customers": {'

(Of course, to anyone who to try this at home: check for yourself that 
the URL’s content is harmless before running eval().)

Can you copy the exact code you use to build the dictionary and call 
jsonify?

Regards,
-- 
Simon Sapin

Re: [flask] Strange problem with ajax call and jsonify

From:
Alessio Civitillo
Date:
2012-05-06 @ 15:39
On Sun, May 6, 2012 at 5:15 PM, Simon Sapin <simon.sapin@exyr.org> wrote:

> Le 06/05/2012 16:34, Alessio Civitillo a écrit :
> > I am making an ajax call with jquery on a url that is supposed to return
> > json. To return a json I am first building a dictionary (d) (with no
> > tuples or sets) and then the view returns: jsonify(d).
> >
> > However, if there are numbers in the json, it will give something like:
> >
> >   *
> >
> >     raise TypeError(repr(o) + " is not JSON serializable")
> >
> >     TypeError: 100 is not JSON serializable
> >
> > While all works if in the json there are numbers. What could be the
> > issue here?
> >
> > * A copy of the json giving me trouble is at
> http://pastebin.com/zxDnV5mP.
> >
>
>
> Hi,
>
> Strange indeed, numbers are one of the basic JSON types.
>
> I can’t reproduce the problem, this works fine for me:
>
>  >>> url = 'http://pastebin.com/raw.php?i=zxDnV5mP'
>  >>> jsonify(eval(urllib.urlopen(url).read())).data[:18]
> '{\n  "customers": {'
>
> (Of course, to anyone who to try this at home: check for yourself that
> the URL’s content is harmless before running eval().)
>
> Can you copy the exact code you use to build the dictionary and call
> jsonify?
>
> Regards,
> --
> Simon Sapin
>

Hello,

the application is on github, the code (very unclean) you asked is at:

https://github.com/kfk/datacrowd/blob/master/datacrowd/views/views.py

I will try to deploy this api later, maybe the issue is with the javascript
call?

thanks