Jihoon
Jihoon

Reputation: 23

ValueError when loading a json file (python)

I tried to load my twit data in python but I got some error. Below is my code for loading my data. And I am attaching the error message. I am guessing that my TwitData1.json data has some problems, so need to be handled... I am not sure how I can handle this. Thank you in advance.

import json, pprint

with open("TwitData1.json") as json_file:

  json_data = json.load(json_file)

  pprint(json_data)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-26-ba1a50c671e1> in <module>()
      2 
      3 with open("TwitData1.json") as json_file:
----> 4     json_data = json.load(json_file)
      5     json_data.close()
      6     pprint(json_data)

C:\Users\Jay\AppData\Local\Enthought\Canopy\App\appdata\canopy-1.4.1.1975.win-x86_64\lib\json\__init__.pyc in load(fp, encoding, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)
    288         parse_float=parse_float, parse_int=parse_int,
    289         parse_constant=parse_constant, object_pairs_hook=object_pairs_hook,
--> 290         **kw)
    291 
    292 

C:\Users\Jay\AppData\Local\Enthought\Canopy\App\appdata\canopy-1.4.1.1975.win-x86_64\lib\json\__init__.pyc in loads(s, encoding, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)
    336             parse_int is None and parse_float is None and
    337             parse_constant is None and object_pairs_hook is None and not kw):
--> 338         return _default_decoder.decode(s)
    339     if cls is None:
    340         cls = JSONDecoder

C:\Users\Jay\AppData\Local\Enthought\Canopy\App\appdata\canopy-1.4.1.1975.win-x86_64\lib\json\decoder.pyc in decode(self, s, _w)
    366         end = _w(s, end).end()
    367         if end != len(s):
--> 368             raise ValueError(errmsg("Extra data", s, end, len(s)))
    369         return obj
    370 

ValueError: Extra data: line 2 column 1 - line 21 column 1 (char 2057 - 70650)

Upvotes: 0

Views: 2671

Answers (1)

biobirdman
biobirdman

Reputation: 4120

first I will use a json validator to validate that your json file is correct (just google json validator). My bet is that it isn't, the error message stated clearly that there is an error in line 2 column 1 - line 21 column 1. Extra data error usually occur when you have multiple json objects in a single json file.

json.loads('{"object" : "A"}{"object" : "B"}')
valueError: Extra data: line 1 column 16 - line 1 column 32 (char 16 - 32)

To load multiple json object place the 2 json objects into a single list

json.loads('[{"object" : "A"},{"object" : "B"}]')
[{u'object': u'A'}, {u'object': u'B'}]

I hope you will find this useful

Cheers!

Upvotes: 1

Related Questions