Jump to content

(Python 2.7) JSON not decoding correctly.

Featured Replies

I don't understand why this is happening. On my own PC, my program works flawlessly. It's using the TwitterAPI to find data specifically useful for my industry. Like I said, it works fine on my own PC, but I'm trying to set it up on the server right now and I keep getting this message-

Traceback (most recent call last):
  File "<pyshell#2>", line 1, in <module>
    Tweeter()
  File "C:\Python27\lib\site-packages\Tweeter.py", line 16, in Tweeter
    for item in s.get_iterator():
  File "C:\Python27\lib\site-packages\TwitterAPI\TwitterAPI.py", line 198, in __iter__
    yield json.loads(item.decode('utf-8'))
  File "C:\Python27\lib\json\__init__.py", line 338, in loads
    return _default_decoder.decode(s)
  File "C:\Python27\lib\json\decoder.py", line 365, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "C:\Python27\lib\json\decoder.py", line 383, in raw_decode
    raise ValueError("No JSON object could be decoded")
ValueError: No JSON object could be decoded

I've tried looking into it already, and I've probably spent 2 hours now trying to fix the issue, but I'm running out of resources because the ones I am seeing aren't coherent to me.

Is the content of the JSON good? Maybe in the server environment (due to firewalls or whatever - something different to your own PC) you're getting an error message back instead of well formed JSON data?

 

Can you dump/print/save the raw JSON before trying to decode?

These paths are Windows-like.

Server is Windows? I don't think so..

They're rather Linux-based..

  • Author

The server is windows, and thanks for pointing out the firewall issue, that may actually be the solution. I'll check into it


>>> import json
>>> print json.dumps('\\')
"\\"
>>> json.loads('\\')

Traceback (most recent call last):
  File "<pyshell#2>", line 1, in <module>
    json.loads('\\')
  File "C:\Python27\lib\json\__init__.py", line 338, in loads
    return _default_decoder.decode(s)
  File "C:\Python27\lib\json\decoder.py", line 365, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "C:\Python27\lib\json\decoder.py", line 383, in raw_decode
    raise ValueError("No JSON object could be decoded")
ValueError: No JSON object could be decoded
>>> json.loads('"\\"foo\\bar"')
u'"foo\x08ar'

The firewall is not the solution, at least not on my end.

Edited by Popcorn Sutton

  • Author

>>> r = api.request('search/tweets', {'q':'pizza'})
>>> for item in r.get_iterator():
	print (item['text'] if 'text' in item else item)

	
IF YOU FOLLOW ME I'LL BRING YOU PIZZA OK 

#AlfieWantsFood 18
Awkward missing the guy delivering pizza to the flat because we were having a planking competition
@butch_1983 yeah Pizza Hut
RT @Michael5SOS: "excuse me can I have a photo?" lol @Harry_Styles http://t.co/ljfU7uH7lU
RT @stephshuley_22: Boys aren't worth crying over honestly hey just eat some pizza and you'll be good
@PointlessBlog You followed dominos pizza lol #AlfieWantsFood pizza pizza pizza
RT @bucaloubaby: Pizza is in the shape of the food pyramid so can I just eat it all the time?
@dkf__ come over and let's go to pizza King
Me comi una pizza, mexicana, hawaiana y de peperoni, estaba mas buena
RT @Alyssamcdonald5: Craving pizza 24/7 
@ilooklloyd pizza
Someone give me a pizza
@PointlessBlog #AlfieWantsFood @Dominos_UK could you slip a pizza to alfie? @PointlessBlogTv preferably tuna&sweetcorn stuffed crust :Dxxx
“@ChloeDeCuyper01: Ik wil dat ge nu bij mij zijt en dat we samen pizza eten terwijl we een film zien ” @LiseLecocq
RT @feloquence: En pizza skulle sitta fint

The search works, but streaming from our location returns the error.

I am absolutely stumped on this one. It works just fine on my own PC but not on the server.

Is there an encoding (or encoding of encoding) issue?

\\bar

... in the call seems to emit as:

\x08ar

... that is, the second backslash is being treated as an escape sequence.

 

(The combinations of double-quotes inside the single-quotes also look weird to me.)

  • Author

When it works on my PC there is a bit of an encoding issue but I don't think that that is the problem. I've tried using different quotes as well but at this point I'm thinking that it's an os issue which is unfortunate

  • 3 weeks later...

I bet its a UTF-8 issue, try simplejson sir, its a much better library. (you can install it through pip or apt)

Archived

This topic is now archived and is closed to further replies.

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.