You'd generally write one JSON object to a file; that object can contain your other objects:

json_data = {
    'p_id': p_id,
    'word_list': word_list,
    # ...
}
with open('data.json', 'w') as fp:
    json.dump(json_data, fp, sort_keys=True, indent=4)

Now all you have to do is read that one object and address the values by the same keys.

If you must write multiple JSON documents, avoid using newlines so you can read the file line by line, as parsing the file one JSON object at a time is a lot more involved.

Answer from Martijn Pieters on Stack Overflow
🌐
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?

🌐
Python
docs.python.org › 3 › library › json.html
JSON encoder and decoder — Python 3.14.7 documentation
In JSON, an object refers to any data wrapped in curly braces, similar to a Python dictionary. ... Be cautious when parsing JSON data from untrusted sources. A malicious JSON string may cause the decoder to consume considerable CPU and memory resources. Limiting the size of data to be parsed is recommended. This module exposes an API familiar to users of the standard library marshal and pickle modules. ... >>> import json >>> json.dumps(['foo', {'bar': ('baz', None, 1.0, 2)}]) '["foo", {"bar": ["baz", null, 1.0, 2]}]' >>> print(json.dumps("\"foo\bar")) "\"foo\bar" >>> print(json.dumps('\u1234')) "\u1234" >>> print(json.dumps('\\')) "\\" >>> print(json.dumps({"c": 0, "b": 0, "a": 0}, sort_keys=True)) {"a": 0, "b": 0, "c": 0} >>> from io import StringIO >>> io = StringIO() >>> json.dump(['streaming API'], io) >>> io.getvalue() '["streaming API"]'
🌐
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 - This approach involves reading file content line by line and parsing each line individually as JSON. json.load() is a built in function present in json module that takes a file object or a string which contains JSON data as input and returns ...
🌐
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 10, 2018 -

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!

🌐
Python Forum
python-forum.io › thread-27109.html
Parse JSON multiple objects
May 26, 2020 - 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...
🌐
Rayobyte
rayobyte.com › blog › how-to-use-json-dumps-in-python
How to Store Scraped Web Data in Python Using JSON Dumps
March 11, 2026 - Data integrity: While ‘json.dump’ ... arithmetic works. Multiple object serialization: ‘json.dump’ is designed to serialize a single Python object at a time....
🌐
Quora
quora.com › How-do-I-write-multiple-Python-dictionaries-to-a-JSON-file
How to write multiple Python dictionaries to a JSON file - Quora
Answer: I’m writing answer for my own question. First load the json file with an empty Dict. with open(‘file.json', ‘w') as f: json.loads(“{}”,f) Then write the Dict and store the data. Open the json file in read mode. with open(‘file.json', ‘r') as r: Data = hain.dumps(r) ...
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › python › json-dumps-in-python
json.dumps() in Python - GeeksforGeeks
It is mainly used when you need to send data over APIs, store structured data or serialize Python objects into JSON text. Example: This example shows how to convert a Python dictionary into a JSON string. ... The output type is str, not a dictionary. json.dumps(obj, skipkeys=False, ensure_ascii=True, allow_nan=True, indent=None, separators=None, sort_keys=False)
Published: January 13, 2026
🌐
Reddit
reddit.com › r/learnpython › how to write multiple dictionaries to file with valid json
r/learnpython on Reddit: How to write multiple dictionaries to file with valid json
December 18, 2017 -

Example code:

def filewriter(line):
    dictionary = {}
    dictionary['name'] = line[0][0] #assume this is a a string
    dictionary['item_to_buy'] = line[0][1]
    dictionary['currency'] = line[0][2]
    dictionary['league'] = line[0][3]
    with open('logs.json', 'r+') as f:
        if len(f.read()) == 0:
            f.write(json.dumps(dictionary))
        else:
            f.write(',\n' + json.dumps(dictionary))

def retrieve():
    with open('logs.json') as f:
        g = json.load(f)
        print(g[1]['name'])

So I'm creating dictionaries separated by a comma and a newline, however json format dictates that I need brackets enclosing multiple dictionaries. For example, the dictionaries

{"name": "name", "item_to_buy": "item", "currency": "orb", "league": "league"},
{"name": "name", "item_to_buy": "item", "currency": "orb", "league": "league"}

need to be

[{"name": "name", "item_to_buy": "item", "currency": "orb", "league": "league"},
{"name": "name", "item_to_buy": "item", "currency": "orb", "league": "league"}]

I've tried to do g = json.load(list(f.read())) to hopefully encapsulate them with a list every time I want to read them but to no avail.

I want to store a dictionary in some file and be able to retrieve the dictionary for another program, and the best way seems to be JSON, but I'm having a little trouble formatting it.

Edit: I'm adding dictionaries in real time, so it's not just 2 or 3 dictionaries, but a whole lot I need to write.

🌐
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 - If your file contains a list of JSON objects, and you want to decode one object one-at-a-time, we can do it. To Load and parse a JSON file with multiple JSON objects we need to follow below steps: ... Read the file line by line because each line contains valid JSON. i.e., read one JSON object ...
🌐
Reddit
reddit.com › r/learnpython › python newbie... creating multiple json files from a single json file based on *some* value...
r/learnpython on Reddit: Python newbie... Creating multiple json files from a single json file based on *some* value...
January 5, 2019 -

Ok so I'm back again :)

Here's the input json file

[
  {
    "PlayerID": 589,
    "TeamID": 607,
    "TeamName": "Walsall",
    "Forename": "Trevor",
    "Surname": "Scowcroft",
    "Age": 29,
    "Rating": 35
  },
  {
    "PlayerID": 859,
    "TeamID": 607,
    "TeamName": "Walsall",
    "Forename": "Francisco",
    "Surname": "Alves",
    "Age": 18,
    "Rating": 53
  },
  {
    "PlayerID": 610,
    "TeamID": 609,
    "TeamName": "Bury",
    "Forename": "Simon",
    "Surname": "Marsden",
    "Age": 18,
    "Rating": 40
  },
  {
    "PlayerID": 611,
    "TeamID": 609,
    "TeamName": "Bury",
    "Forename": "Robert",
    "Surname": "Venus",
    "Age": 25,
    "Rating": 44
  },
  {
    "PlayerID": 629,
    "TeamID": 609,
    "TeamName": "Bury",
    "Forename": "Carl",
    "Surname": "Williams",
    "Age": 18,
    "Rating": 53
  },
  {
    "PlayerID": 654,
    "TeamID": 611,
    "TeamName": "Charlton Athletic",
    "Forename": "Robbie",
    "Surname": "Moffat",
    "Age": 24,
    "Rating": 40
  },
  {
    "PlayerID": 655,
    "TeamID": 611,
    "TeamName": "Charlton Athletic",
    "Forename": "Anthony",
    "Surname": "Rowett",
    "Age": 21,
    "Rating": 43
  }
]

And here's the code so far...

import json

jsonFilePath="new.json"

with open(jsonFilePath, encoding='utf-8') as jsonFile:
  jsonData=json.load(jsonFile)

for row in jsonData:
  filename = "teams/"+str(row.get("TeamID"))+".json"
  print(filename)
  # struggling with this part...
  with open(filename, 'w', encoding='utf-8') as outputFile:
    outputFile.write(json.dumps(row, ensure_ascii=False, indent=2))

I've already created the teams directory ;)

The above script creates 3 json files (607.json,609.json,611.json) in teams directory (exactly as I want to)

But inner content is not what I want... :(

It only contains the last item

for example 607.json file only contains

{
  "PlayerID": 859,
  "TeamID": 607,
  "TeamName": "Walsall",
  "Forename": "Francisco",
  "Surname": "Alves",
  "Age": 18,
  "Rating": 53
}

I want it to contain both (I mean all because real data is large again...)

So I change the mode from w to a and sure enough it now contains both (all) items like this

again taking 607.json for example

{
  "PlayerID": 589,
  "TeamID": 607,
  "TeamName": "Walsall",
  "Forename": "Trevor",
  "Surname": "Scowcroft",
  "Age": 29,
  "Rating": 35
}{
  "PlayerID": 859,
  "TeamID": 607,
  "TeamName": "Walsall",
  "Forename": "Francisco",
  "Surname": "Alves",
  "Age": 18,
  "Rating": 53
}

Now there are two issues with this one

First the data is not in correct format....

Secondly if I run the script again then it keeps appending to the file (which is to be expected with a mode) :P

The data should be in this format (again 607.json only)

[
  {
    "PlayerID": 589,
    "TeamID": 607,
    "TeamName": "Walsall",
    "Forename": "Trevor",
    "Surname": "Scowcroft",
    "Age": 29,
    "Rating": 35
  },
  {
    "PlayerID": 859,
    "TeamID": 607,
    "TeamName": "Walsall",
    "Forename": "Francisco",
    "Surname": "Alves",
    "Age": 18,
    "Rating": 53
  }
]

Please point me in the right direction.

Thanks :)

Top answer
1 of 2
3
It only contains the last item You aren't collating your data in python. You have a list of dictionaries, and overwrite (or append) the file for each dictionary in the list, based on TeamID. But what you *want* is a dict of lists-of-dictionaries (probably), with each list-of-dictionaries each having the same TeamID entries: import json from collections import defaultdict jsonFilePath="new.json" with open(jsonFilePath, encoding='utf-8') as jsonFile: jsonData=json.load(jsonFile) # Collate dictionaries with the same TeamID into new lists. collated_teamids = defaultdict(list) for team in jsonData: # for each team, add to the list of team data collated_teamids[team['TeamID']].append(team) for team_id, teams in collated_teamids.items(): filename = "teams/" + str(team_id) + ".json" print(filename) with open(filename, 'w', encoding='utf-8') as outputFile: outputFile.write(json.dumps(teams, ensure_ascii=False, indent=2)) Note that I'm using a defaultdict(list) here to make it easy to just append each new 'team' dictionary (what you called row). But a regular dict is almost as easy, if you use the Python dict.get or dict.setdefault methods (or just test for the key first and create a new list). You definitely don't want to be appending to json files; they are structured data, and can't reliably be concatenated. Assemble the data first, and then write it all at once (as above). I don't make an effort to sort each team file by player ID, but that could be done by sorting the 'teams' variable that is retrieved by iterating over the collated_teamids.
2 of 2
1
Create the structure you want in Python, and then just json.dump() it. If you want to write a list of dicts, then just make a list of dicts, and write that in one call, not looping over and appending to the file.
🌐
Python
docs.python.org › 3.3 › library › json.html
19.2. json — JSON encoder and decoder — Python 3.3.7 documentation
JSON (JavaScript Object Notation), specified by RFC 4627, is a lightweight data interchange format based on a subset of JavaScript syntax (ECMA-262 3rd edition). json exposes an API familiar to users of the standard library marshal and pickle modules. ... >>> import json >>> json.dumps(['foo', {'bar': ('baz', None, 1.0, 2)}]) '["foo", {"bar": ["baz", null, 1.0, 2]}]' >>> print(json.dumps("\"foo\bar")) "\"foo\bar" >>> print(json.dumps('\u1234')) "\u1234" >>> print(json.dumps('\\')) "\\" >>> print(json.dumps({"c": 0, "b": 0, "a": 0}, sort_keys=True)) {"a": 0, "b": 0, "c": 0} >>> from io import StringIO >>> io = StringIO() >>> json.dump(['streaming API'], io) >>> io.getvalue() '["streaming API"]'
🌐
Real Python
realpython.com › python-json
Working With JSON Data in Python – Real Python
August 20, 2025 - You connect JSON with Python by using the json module to serialize Python objects into JSON and deserialize JSON data into Python objects. How do you convert a Python dictionary to a JSON-formatted string?Show/Hide · You can use the json.dumps() function from Python’s json module to convert a Python dictionary to a JSON-formatted string.
🌐
W3Schools
w3schools.com › python › python_json.asp
Python JSON
The json.dumps() method has parameters to make it easier to read the result: Use the indent parameter to define the numbers of indents: ... You can also define the separators, default value is (", ", ": "), which means using a comma and a space to separate each object, and a colon and a space to separate keys from values:
🌐
Medium
medium.com › @programinbasic › merge-multiple-json-files-into-one-in-python-65c009aad81d
Merge Multiple JSON files into One in Python | by ProgrammingBasic | Medium
January 17, 2024 - It opens a new file called ‘merged.json’ for writing as file object f · It dumps the merged data from data1 into the merged.json file using json.dump(). This writes the updated data1 dict as JSON to the output file. Another way to merge JSON in Python is by using the pandas library.