Python JSON processing - extra data error

karthik.sharma at gmail.com karthik.sharma at gmail.com
Mon Mar 30 17:12:39 EDT 2015


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`





More information about the Python-list mailing list