There are several problems with the logic of your code.

ss = s.read()

reads the entire file s into a single string. The next line

for line in ss:

iterates over each character in that string, one by one. So on each loop line is a single character. In

    line = ss[7:]

you are getting the entire file contents apart from the first 7 characters (in positions 0 through 6, inclusive) and replacing the previous content of line with that. And then

T.append(json.loads(line))

attempts to convert that to JSON and store the resulting object into the T list.


Here's some code that does what you want. We don't need to read the entire file into a string with .read, or into a list of lines with .readlines, we can simply put the file handle into a for loop and that will iterate over the file line by line.

We use a with statement to open the file, so that it will get closed automatically when we exit the with block, or if there's an IO error.

import json

table = []
with open('simple.json', 'r') as f:
    for line in f:
        table.append(json.loads(line[7:]))

for row in table:
    print(row)

output

{'color': '33ef', 'age': '55', 'gender': 'm'}
{'color': '3444', 'age': '56', 'gender': 'f'}
{'color': '3999', 'age': '70', 'gender': 'm'}

We can make this more compact by building the table list in a list comprehension:

import json

with open('simple.json', 'r') as f:
    table = [json.loads(line[7:]) for line in f]

for row in table:
    print(row)
Answer from PM 2Ring on Stack Overflow
Top answer
1 of 4
9

There are several problems with the logic of your code.

ss = s.read()

reads the entire file s into a single string. The next line

for line in ss:

iterates over each character in that string, one by one. So on each loop line is a single character. In

    line = ss[7:]

you are getting the entire file contents apart from the first 7 characters (in positions 0 through 6, inclusive) and replacing the previous content of line with that. And then

T.append(json.loads(line))

attempts to convert that to JSON and store the resulting object into the T list.


Here's some code that does what you want. We don't need to read the entire file into a string with .read, or into a list of lines with .readlines, we can simply put the file handle into a for loop and that will iterate over the file line by line.

We use a with statement to open the file, so that it will get closed automatically when we exit the with block, or if there's an IO error.

import json

table = []
with open('simple.json', 'r') as f:
    for line in f:
        table.append(json.loads(line[7:]))

for row in table:
    print(row)

output

{'color': '33ef', 'age': '55', 'gender': 'm'}
{'color': '3444', 'age': '56', 'gender': 'f'}
{'color': '3999', 'age': '70', 'gender': 'm'}

We can make this more compact by building the table list in a list comprehension:

import json

with open('simple.json', 'r') as f:
    table = [json.loads(line[7:]) for line in f]

for row in table:
    print(row)
2 of 4
8

If you use Pandas you can simply write df = pd.read_json(f, lines=True)

as per doc the lines=True:

Read the file as a json object per line.

🌐
Python Forum
python-forum.io › thread-27109.html
Parse JSON multiple objects
I'm having trouble parsing multiple objects within a JSON array. I can get my code to work, but I have to manipulate the JSON file which I shouldn't have to do. I'm on Python 3, and here's my code: import json tradingList = [] print with open('par...
🌐
Reddit
reddit.com › r/learnpython › multiple objects in json file
r/learnpython on Reddit: Multiple objects in JSON file
February 5, 2020 -

Hey, i am new to programming and I am trying to decode thousands of JSON files.
Usually there is one object in each JSON file, but for some reason a lot of my files have multiple JSON objects. Some have up to 5 objects.

{
	"testNumber": "test200",
	"device": {
		"deviceID": 4000008

	},
	"user": {
		"userID": "4121412"
	}
}
{
	"testNumber": "test201",
	"device": {
		"deviceID": 4000009

	},
	"user": {
		"userID": "4121232"
	}
}

My code gives me the error: json.decoder.JSONDecodeError: Extra data: line 2 column 1
Because of that I am using except ValueError but I would like to get the data out of these JSON files.

import json
import os

test_dir = r'C:\Users\path\path'
for file in os.listdir(test_dir):
    if 'testNumber' in file:
        try: 
            data = json.load(open(test_dir + '\\' + file, 'r'))  
            print("valid")
        except ValueError: 
               print("Decoding JSON has failed")

Since json.loads and json.load don't work: is there any other way open the JSON file so that I can try to split the content in 2 objects?

🌐
PYnative
pynative.com › home › python › json › python parse multiple json objects from file
Python Parse multiple JSON objects from file | Solve ValueError: Extra data
May 14, 2021 - To parse a JSON file with multiple JSON objects read one JSON object at a time and Convert it into Python dict using a json.loads()
Top answer
1 of 6
99

Update: I wrote a solution that does not require reading the entire file in one go. It is too big for a stackoverflow answer, but can be found here jsonstream.

You can use json.JSONDecoder.raw_decode to decode arbitarily big strings of "stacked" JSON (so long as they can fit in memory). raw_decode stops once it has a valid object and returns the last position where was not part of the parsed object. It is poorly documented [1] (see footer), but you can pass this position back to raw_decode and it start parsing again from that position. Unfortunately, the Python json module doesn ot accept strings that have prefixing whitespace. So we need to search to find the first non-whitespace part of your document.

from json import JSONDecoder, JSONDecodeError
import re

NOT_WHITESPACE = re.compile(r'\S')

def decode_stacked(document, idx=0, decoder=JSONDecoder()):
    while True:
        match = NOT_WHITESPACE.search(document, idx)
        if not match:
            return
        idx = match.start()
        
        try:
            obj, idx = decoder.raw_decode(document, idx)
        except JSONDecodeError:
            # do something sensible if there's some error
            raise
        yield obj

s = """

{"a": 1}  


   [
1
,   
2
]


"""

for obj in decode_stacked(s):
    print(obj)

prints:

{'a': 1}
[1, 2]

Note About Missing Documentation

The current signature of raw_decode() dates from 2009, when simplejson was ported into the standard library. The documentation for raw_decode() in simplejson mentions an optional idx argument that can be used to start parsing at an offset. Given that the signature of raw_decode() has not changed since 2009, I think it is fair to assume the API is fairly stable. Especially as decode() uses the idx argument of raw_decode() to ignore prefixing whitespace when parsing a string. And this is exactly what this answer is using the idx argument for too. The documentation of raw_decode() in simplejson is:

raw_decode(s[, idx=0])

Decode a JSON document from s (a str or unicode beginning with a JSON document) starting from the index idx and return a 2-tuple of the Python representation and the index in s where the document ended.

This can be used to decode a JSON document from a string that may have extraneous data at the end, or to decode a string that has a series of JSON objects.

JSONDecodeError will be raised if the given JSON document is not valid.

2 of 6
38

Use a json array, in the format:

[
{"ID":"12345","Timestamp":"20140101", "Usefulness":"Yes",
  "Code":[{"event1":"A","result":"1"},…]},
{"ID":"1A35B","Timestamp":"20140102", "Usefulness":"No",
  "Code":[{"event1":"B","result":"1"},…]},
{"ID":"AA356","Timestamp":"20140103", "Usefulness":"No",
  "Code":[{"event1":"B","result":"0"},…]},
...
]

Then import it into your python code

import json

with open('file.json') as json_file:

    data = json.load(json_file)

Now the content of data is an array with dictionaries representing each of the elements.

You can access it easily, i.e:

data[0]["ID"]
🌐
GeeksforGeeks
geeksforgeeks.org › python › extract-multiple-json-objects-from-one-file-using-python
Extract Multiple JSON Objects from one File using Python - GeeksforGeeks
July 23, 2025 - Entire file content will be read and by using re.findall() method the defined pattern will be applied to the file content and it will return the list of strings of json objects found in a file, each string is passed to the json.loads() method to parse it to the python object.
🌐
Reddit
reddit.com › r/learnpython › help with decoding json file with multiple objects
r/learnpython on Reddit: Help with decoding JSON file with multiple Objects
July 6, 2017 -

Hi all!

I'm getting this error when I want to load (decode) multiple JSON objects.

json.decoder.JSONDecodeError: Extra data: line 1 column 3 (char 2)

Done a little digging and found it's due to the JSON module being unable to parse multiple top level objects from a JSON file. I read, if you put the Dictionaries inside a list, you can dump them all and load them back. Perfect!

I wrote this code to test it, sadly it doesn't work because (I think) I'm adding another JSON Object wrapped in an Array outside of the first JSON Array.

import json

dict1 = {}
dict2 = {}

with open('test.json', 'a') as test:
    json.dump([dict1,dict2], test)   # This works and decodes!
    
    json.dump([dict2],test)          # This line breaks the decoder when run with line above!

with open('test.json','r') as test:
    x = json.load(test)
    
print(x) # Should print out contents of file. 

Is there any workaround (or something I'm missing) that can help me out and will let me load multiple top level Objects from a JSON file?

Thanks!

Find elsewhere
🌐
Python.org
discuss.python.org › python help
How do I use Python to read multiple JSON files and export specific values to a CSV? - Python Help - Discussions on Python.org
September 7, 2022 - I am not a programmer and have no experience with Pyton, so I would really appreciate any help to solve the few remaining issues I’ll explain bellow: What I am trying to do is collect a few but same values from many .js…
🌐
Reddit
reddit.com › r/learnpython › how do i parse json file with multiple json objects (but each json object isn't on one line)
r/learnpython on Reddit: how do I parse json file with multiple json objects (but each json object isn't on one line)
November 4, 2018 -

how do I parse json file with multiple json objects (but each json object isn't on one line)

I have a json file with multiple json objects but each json object isn't on a distinct line.

 

For example 3 json objects below:

1 {
2 "names": [],
3 "ids": [], 
4 } {
5  "names": [],
6 "ids": [
7 {
8 "groups": [],
9 } {
10 "key": "1738"
11 }
12 ] 
13 }{
12 "names": [],
13 "key": "9",
14 "ss": "123"
15 }

 

Basically, there are multiple json objects but are not separated by commas and I don't know where each is separated because each json object is not all on one line. Each json object does not contain the same stuff.

 

Ideally, I would like to put all the json objects and put them in brackets w/ each json object separated by commas ultimately to convert it into a dictionary or array of json objects but the original file does not separate each json object.

Top answer
1 of 2
24

Load 6 extra lines instead, and pass the string to json.loads():

with open(file) as f:
    for line in f:
        # slice the next 6 lines from the iterable, as a list.
        lines = [line] + list(itertools.islice(f, 6))
        jfile = json.loads(''.join(lines))

        # do something with jfile

json.load() will slurp up more than just the next object in the file, and islice(f, 0, 7) would read only the first 7 lines, rather than read the file in 7-line blocks.

You can wrap reading a file in blocks of size N in a generator:

from itertools import islice, chain

def lines_per_n(f, n):
    for line in f:
        yield ''.join(chain([line], itertools.islice(f, n - 1)))

then use that to chunk up your input file:

with open(file) as f:
    for chunk in lines_per_n(f, 7):
        jfile = json.loads(chunk)

        # do something with jfile

Alternatively, if your blocks turn out to be of variable length, read until you have something that parses:

with open(file) as f:
    for line in f:
        while True:
            try:
                jfile = json.loads(line)
                break
            except ValueError:
                # Not yet a complete JSON value
                line += next(f)

        # do something with jfile
2 of 2
9

As stated elsewhere, a general solution is to read the file in pieces, append each piece to the last, and try to parse that new chunk. If it doesn't parse, continue until you get something that does. Once you have something that parses, return it, and restart the process. Rinse-lather-repeat until you run out of data.

Here is a succinct generator that will do this:

def load_json_multiple(segments):
    chunk = ""
    for segment in segments:
        chunk += segment
        try:
            yield json.loads(chunk)
            chunk = ""
        except ValueError:
            pass

Use it like this:

with open('foo.json') as f:
   for parsed_json in load_json_multiple(f):
      print parsed_json

I hope this helps.

🌐
Zyte
zyte.com › home › blog › json parsing with python [practical guide]
JSON Parsing with Python [Practical Guide]
July 6, 2023 - The json module provides two methods, loads and load, that allow you to parse JSON strings and JSON files, respectively, to convert JSON into Python objects such as lists and dictionaries.
🌐
Stack Overflow
stackoverflow.com › questions › 56376432 › how-to-parse-json-file-with-multiple-objects
python - How to parse JSON file with multiple objects - Stack Overflow
I am trying to parse a JSON file which is containing multiple JSON objects. Here is my code: { "obj1": { "type": "object", "permission": "r", "obj2": { "ty...
🌐
Python.org
discuss.python.org › python help
How to parse different part of json file (Newbie here) - Python Help - Discussions on Python.org
September 27, 2021 - I have created a python code that extracts jitter, latency, link, packet loss, timestamp from json file to csv file. However, inside this json file, there are multiple tests done (e.g. bronek 1, bronek 2, bronek 3, etc.) that under these testings have pair keys of jitter, latency, link, packet ...
🌐
Real Python
realpython.com › python-json
Working With JSON Data in Python – Real Python
August 20, 2025 - Yes, JSON is widely used for data interchange in Python because it’s lightweight, language-independent, and easy to parse with Python’s built-in json module. ... You can write JSON with Python by using the json.dump() function to serialize Python objects into a JSON file.
Top answer
1 of 3
3

The file format is not correct if this is the complete file. Between the curly brackets there must be a comma and it should start and end with a square bracket. Like so: [{...},{...}]. For your data it would look like:

[{"review_id":"x7mDIiDB3jEiPGPHOmDzyw","user_id":"msQe1u7Z_XuqjGoqhB0J5g","business_id": ...},
{"review_id":"dDl8zu1vWPdKGihJrwQbpw","user_id":"msQe1u7Z_XuqjGoqhB0J5g","business_id": ...}]

Here is some code how to clean your file:

lastline = None

with open("yourfile.json","r") as f:
    lineList = f.readlines()
    lastline=lineList[-1]

with open("yourfile.json","r") as f, open("cleanfile.json","w") as g:
    for i,line in enumerate(f,0):
        if i == 0:
            line = "["+str(line)+","
            g.write(line)
        elif line == lastline:            
            g.write(line)
            g.write("]")
        else:
            line = str(line)+","
            g.write(line)

To read a json file properly you could also consider using the pandas library (https://pandas.pydata.org/pandas-docs/stable/generated/pandas.read_json.html).

import pandas as pd

#get a pandas dataframe object from json file
df = pd.read_json("path/to/your/filename.json")

If you are not familiar with pandas, here a quick headstart, how to work with a dataframe object:

df.head() #gives you the first rows of the dataframe
df["review_id"] # gives you the column review_id as a vector
df.iloc[1,:] # gives you the complete row with index 1
df.iloc[1,2] # gives you the item in row with index 1 and column with index 2 
2 of 3
2

While each line on it's own is valid JSON, your file as a whole is not. As such, you can't parse it in one go, you will have to iterate over each line parse it into an object.

You can aggregate these objects in one list, and from there do whatever you like with your data :

import json
with open(filename, 'r') as f:
    object_list = []
    for line in f.readlines():
        object_list.append(json.loads(line))
    # object_list will contain all of your file's data

You could do it as a list comprehension to have it a little more pythonic :

with open(filename, 'r') as f:    
    object_list = [json.loads(line) 
                   for line in f.readlines()]
    # object_list will contain all of your file's data
🌐
Screenspan
screenspan.net › blog › python-multiple-json-to-csv
Using Python to Read Multiple JSON Files and Export Values to a CSV | Brian Louis Ramirez | Digital Sustainability & Web Performance Optimization
# To run the script via command line: "python3 json-to-csv-exporter.py" import json import glob from datetime import datetime import csv # Place your JSON data in a directory named 'data/' src = "data/" date = datetime.now() data = [] # Change the glob if you want to only look through files with specific names files = glob.glob('data/*', recursive=True) # Loop through files for single_file in files: with open(single_file, 'r') as f: # Use 'try-except' to skip files that may be missing data try: json_file = json.load(f) data.append([ json_file['requestedUrl'], json_file['fetchTime'], json_file[