Python json.loads shows ValueError: Extra data -
i getting data json file "new.json", , want filter data , store new json file. here code:
import json open('new.json') infile: data = json.load(infile) item in data: iden = item.get["id"] = item.get["a"] b = item.get["b"] c = item.get["c"] if c == 'xyz' or "xyz" in data["text"]: filename = 'abc.json' try: outfile = open(filename,'ab') except: outfile = open(filename,'wb') obj_json={} obj_json["id"] = iden obj_json["val_a"] = obj_json["val_b"] = b and getting error, traceback is:
file "rtfav.py", line 3, in <module> data = json.load(infile) file "/usr/lib64/python2.7/json/__init__.py", line 278, in load **kw) file "/usr/lib64/python2.7/json/__init__.py", line 326, in loads return _default_decoder.decode(s) file "/usr/lib64/python2.7/json/decoder.py", line 369, in decode raise valueerror(errmsg("extra data", s, end, len(s))) valueerror: data: line 88 column 2 - line 50607 column 2 (char 3077 - 1868399) can me?
here sample of data in new.json, there 1500 more such dictionaries in file
{ "contributors": null, "truncated": false, "text": "@homeshop18 #dreamjob professional rafter", "in_reply_to_status_id": null, "id": 421584490452893696, "favorite_count": 0, "source": "<a href=\"https://mobile.twitter.com\" rel=\"nofollow\">mobile web (m2)</a>", "retweeted": false, "coordinates": null, "entities": { "symbols": [], "user_mentions": [ { "id": 183093247, "indices": [ 0, 11 ], "id_str": "183093247", "screen_name": "homeshop18", "name": "homeshop18" } ], "hashtags": [ { "indices": [ 12, 21 ], "text": "dreamjob" } ], "urls": [] }, "in_reply_to_screen_name": "homeshop18", "id_str": "421584490452893696", "retweet_count": 0, "in_reply_to_user_id": 183093247, "favorited": false, "user": { "follow_request_sent": null, "profile_use_background_image": true, "default_profile_image": false, "id": 2254546045, "verified": false, "profile_image_url_https": "https://pbs.twimg.com/profile_images/413952088880594944/rcdr59oy_normal.jpeg", "profile_sidebar_fill_color": "171106", "profile_text_color": "8a7302", "followers_count": 87, "profile_sidebar_border_color": "bcb302", "id_str": "2254546045", "profile_background_color": "0f0a02", "listed_count": 1, "profile_background_image_url_https": "https://abs.twimg.com/images/themes/theme1/bg.png", "utc_offset": null, "statuses_count": 9793, "description": "rafter. rafting do. me aur mera tablet. technocrat of future", "friends_count": 231, "location": "", "profile_link_color": "473623", "profile_image_url": "http://pbs.twimg.com/profile_images/413952088880594944/rcdr59oy_normal.jpeg", "following": null, "geo_enabled": false, "profile_banner_url": "https://pbs.twimg.com/profile_banners/2254546045/1388065343", "profile_background_image_url": "http://abs.twimg.com/images/themes/theme1/bg.png", "name": "jayy", "lang": "en", "profile_background_tile": false, "favourites_count": 41, "screen_name": "jzayypsingh", "notifications": null, "url": null, "created_at": "fri dec 20 05:46:00 +0000 2013", "contributors_enabled": false, "time_zone": null, "protected": false, "default_profile": false, "is_translator": false }, "geo": null, "in_reply_to_user_id_str": "183093247", "lang": "en", "created_at": "fri jan 10 10:09:09 +0000 2014", "filter_level": "medium", "in_reply_to_status_id_str": null, "place": null }
as can see in following example, json.loads (and json.load) not decode multiple json object.
>>> json.loads('{}') {} >>> json.loads('{}{}') # == json.loads(json.dumps({}) + json.dumps({})) traceback (most recent call last): file "<stdin>", line 1, in <module> file "c:\python27\lib\json\__init__.py", line 338, in loads return _default_decoder.decode(s) file "c:\python27\lib\json\decoder.py", line 368, in decode raise valueerror(errmsg("extra data", s, end, len(s))) valueerror: data: line 1 column 3 - line 1 column 5 (char 2 - 4) if want dump multiple dictionaries, wrap them in list, dump list (instead of dumping dictionaries multiple times)
>>> dict1 = {} >>> dict2 = {} >>> json.dumps([dict1, dict2]) '[{}, {}]' >>> json.loads(json.dumps([dict1, dict2])) [{}, {}]
Comments
Post a Comment