How to remove first few characters from every 1st line of each json file
Question: I am relatively new to python. I am trying to merge all JSON files into a one
single JSON file from a folder. I could do my merge. However I would like to
remove the some characters of the 1st line in every file to make the entire
JSON valid.

    
    
    # Script to combine all jsons but need to remove the closing , at the end
    
    import glob
    import re
    
    # read the whole folder
    read_files = glob.glob("bus_stop_1012/*.json")
    
    with open("bus_stop_1012/bus_arrival_1012.json", "wb") as outfile:
    # this is the beginning of the combined file
    outfile.write(' ')
    
    for f in read_files:
    
        # will append each data file
        with open(f, "rb") as infile:
    
            outfile.write(infile.read())
            # will have to add , at the end of each element
            outfile.write(',')
    
    # move back 1 character to remove the last , and end the file
    outfile.seek(-1,1)
    outfile.write(']}')
    

which generates this single JSON file from a example of 2 json files:

    
    
    {"data": [{"time": "2016-03-02 17:45:20 SGT+0800", "result":{
    "BusStopID": "1012", 
    "Services": [
        {
            "NextBus": {
                "EstimatedArrival": "2016-03-02T17:48:21+08:00", 
                "Feature": "WAB", 
                "Latitude": "1.2871405", 
                "Load": "Seats Available", 
                "Longitude": "103.8456715", 
                "VisitNumber": "1"
            }, 
            "Operator": "SBST", 
            "OriginatingID": "10589", 
            "ServiceNo": "12", 
            "Status": "In Operation", 
            "SubsequentBus": {
                "EstimatedArrival": "2016-03-02T17:56:02+08:00", 
                "Feature": "WAB", 
                "Latitude": "0", 
                "Load": "Seats Available", 
                "Longitude": "0", 
                "VisitNumber": "1"
            }, 
            "SubsequentBus3": {
                "EstimatedArrival": "2016-03-02T18:06:02+08:00", 
                "Feature": "WAB", 
                "Latitude": "0", 
                "Load": "Seats Available", 
                "Longitude": "0", 
                "VisitNumber": "1"
            }, 
            "TerminatingID": "77009"
        }
    ], 
    "odata.metadata":
    "http://datamall2.mytransport.sg/ltaodataservice/$metadata#BusArrival/@Element"
    }},{"data": [{"time": "2016-03-02 17:49:36 SGT+0800", "result":{
    "BusStopID": "1012", 
    "Services": [
        {
            "NextBus": {
                "EstimatedArrival": "2016-03-02T17:48:47+08:00", 
                "Feature": "WAB", 
                "Latitude": "1.2944553333333333", 
                "Load": "Seats Available", 
                "Longitude": "103.85045283333334", 
                "VisitNumber": "1"
            }, 
            "Operator": "SBST", 
            "OriginatingID": "10589", 
            "ServiceNo": "12", 
            "Status": "In Operation", 
            "SubsequentBus": {
                "EstimatedArrival": "2016-03-02T17:58:26+08:00", 
                "Feature": "WAB", 
                "Latitude": "1.2821243333333334", 
                "Load": "Seats Available", 
                "Longitude": "103.841401", 
                "VisitNumber": "1"
            }, 
            "SubsequentBus3": {
                "EstimatedArrival": "2016-03-02T18:06:02+08:00", 
                "Feature": "WAB", 
                "Latitude": "0", 
                "Load": "Seats Available", 
                "Longitude": "0", 
                "VisitNumber": "1"
            }, 
            "TerminatingID": "77009"
        }
        ], 
    "odata.metadata":     "http://datamall2.mytransport.sg/ltaodataservice/$metadata#BusArrival/@Element"
    }}]}
    

I would need the **{"data": [** of each subsequent JSON file to be removed as
it is found in every JSON file.


Answer: You could decode from JSON, extract the elements you want, then write those
out as JSON again.

If the goal is to produce one large `{"data": [....]}` list, you can get away
with writing each element in the list separately if you take care not to write
a last comma:

    
    
    import glob
    import json
    
    # read the whole folder
    read_files = glob.glob("bus_stop_1012/*.json")
    
    with open("bus_stop_1012/bus_arrival_1012.json", "wb") as outfile:
        # this is the beginning of the combined file
        outfile.write('{"data": [\n')
        sep = ''
        for f in read_files:
            # will append each data file
            with open(f) as infile:
                 try:
                    for obj in json.load(infile)['data']:
                        outfile.write(sep)
                        json.dump(obj, outfile)
                        sep = ','
                 except ValueError:
                     print 'Failed to load {}'.format(f)
        outfile.write(']}')
    

