Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #88346 > unrolled thread

Python JSON processing - extra data error

Started bykarthik.sharma@gmail.com
First post2015-03-30 14:12 -0700
Last post2015-03-31 08:05 +0200
Articles 2 — 2 participants

Back to article view | Back to comp.lang.python


Contents

  Python JSON processing - extra data error karthik.sharma@gmail.com - 2015-03-30 14:12 -0700
    Re: Python JSON processing - extra data error dieter <dieter@handshake.de> - 2015-03-31 08:05 +0200

#88346 — Python JSON processing - extra data error

Fromkarthik.sharma@gmail.com
Date2015-03-30 14:12 -0700
SubjectPython JSON processing - extra data error
Message-ID<ba7c8df2-938b-4283-9aac-1ad32863521d@googlegroups.com>
I have the following python program to read a set of JSON files do some processing on it and dump them back to the same folder. However When I run the below program and then try to see the output of the JSON file using

`cat file.json | python -m json.tool` 

I get the following error

`extra data: line 1 column 307 - line 1 column 852 (char 306 - 851)`

What is wrong with my program?
 
    #Process 'new' events to extract more info from 'Messages'
    rootDir = '/home/s_parts'
    for dirName, subdirList, fileList in os.walk(rootDir):
        print('Found directory: %s' % dirName)
        for fname in fileList:
            fname='s_parts/'+fname
            with open(fname, 'r+') as f:
                json_data = json.load(f)
                et = json_data['Et']
                ms = json_data['Ms']
                if (event == 'a.b.c.d') or (event == 'e.f.g.h'):
                    url = re.sub('.+roxy=([^& ]*).*', r'\1', ms)
                    nt = re.findall(r"NT:\s*([^,)]*)",ms)[0]
                    bt = re.findall(r"BT:\s*([^,)]*)",ms)[0]
                    xt = re.findall(r"XT:\s*([^,)]*)",ms)[0]
                    appde = ms.split('Appde:')[1].strip().split('<br>')[0]
                    version = ms.split('version:')[1].strip().split('<br>')[0]
                    json_data["url"] = url
                    json_data["BT"] = bt
                    json_data["XT"] = xt
                    json_data["NT"] = nt
                    json_data["Appde"] = appde
                    json_data["version"] = version
                else:
                    json_data["url"] = "null"
                    json_data["BT"] = "null"
                    json_data["XT"] = "null"
                    json_data["NT"] = "null"
                    json_data["Appde"] = "null"
                    json_data["version"] = "null"
               json.dump(json_data,f)

If I do a `file` command on the output file I get 
`events_parts/data_95: ASCII text, with very long lines, with no line terminators`

[toc] | [next] | [standalone]


#88362

Fromdieter <dieter@handshake.de>
Date2015-03-31 08:05 +0200
Message-ID<mailman.358.1427781935.10327.python-list@python.org>
In reply to#88346
karthik.sharma@gmail.com writes:

> I have the following python program to read a set of JSON files do some processing on it and dump them back to the same folder. However When I run the below program and then try to see the output of the JSON file using
>
> `cat file.json | python -m json.tool` 
>
> I get the following error
>
> `extra data: line 1 column 307 - line 1 column 852 (char 306 - 851)`

You might have got a traceback which would tell you where precisely
the problem was detected. Looking the the code there (Python is "Open Source";
thus, you can look at code) will tell you what apparently went wrong.


"extra data" suggests to me that your JSON input may be invalid.

"1 2", for example could cause such a problem, because it contains
two integers (not a single one) without a containing container.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web