I suggest several possible solutions.

One solution is to write custom code to slurp in the input file. I would suggest putting a special line before each JSON object in the file, such as: ###

Then you could write code like this:

import json

def json_get_objects(f):
    temp = ''
    line = next(f)  # pull first line
    assert line == SPECIAL_LINE

    for line in f:
        if line != SPECIAL_LINE:
            temp += line
        else:
            # found special marker, temp now contains a complete JSON object
            j = json.loads(temp)
            yield j
            temp = ''
    # after loop done, yield up last JSON object
    if temp:
        j = json.loads(temp)
        yield j

with open("specs_data.txt", "r") as f:
    for j in json_get_objects(f):
        pass # do something with JSON object j

Two notes on this. First, I am simply appending to a string over and over; this used to be a very slow way to do this in Python, so if you are using a very old version of Python, don't do it this way unless your JSON objects are very small. Second, I wrote code to split the input and yield up JSON objects one at a time, but you could also use a guaranteed-unique string, slurp in all the data with a single call to f.read() and then split on your guaranteed-unique string using the str.split() method function.

Another solution would be to write the whole file as a valid JSON list of valid JSON objects. Write the file like this:

{"mylist":[
# first JSON object, followed by a comma
# second JSON object, followed by a comma
# third JSON object
]}

This would require your file appending code to open the file with writing permission, and seek to the last ] in the file before writing a comma plus newline, then the new JSON object on the end, and then finally writing ]} to close out the file. If you do it this way, you can use json.loads() to slurp the whole thing in and have a list of JSON objects.

Finally, I suggest that maybe you should just use a database. Use SQLite or something and just throw the JSON strings in to a table. If you choose this, I suggest using an ORM to make your life simple, rather than writing SQL commands by hand.

Personally, I favor the first suggestion: write in a special line like ###, then have custom code to split the input on those marks and then get the JSON objects.

EDIT: Okay, the first suggestion was sort of assuming that the JSON was formatted for human readability, with a bunch of short lines:

{
    "foo": 0,
    "bar": 1,
    "baz": 2
}

But it's all run together as one big long line:

{"foo":0,"bar":1,"baz":2}

Here are three ways to fix this.

0) write a newline before the ### and after it, like so:

###
{"foo":0,"bar":1,"baz":2}
###
{"foo":0,"bar":1,"baz":2}

Then each input line will alternately be ### or a complete JSON object.

1) As long as SPECIAL_LINE is completely unique (never appears inside a string in the JSON) you can do this:

with open("specs_data.txt", "r") as f:
    temp = f.read()  # read entire file contents
    lst = temp.split(SPECIAL_LINE)
    json_objects = [json.loads(x) for x in lst]
    for j in json_objects:
        pass # do something with JSON object j

The .split() method function can split up the temp string into JSON objects for you.

2) If you are certain that each JSON object will never have a newline character inside it, you could simply write JSON objects to the file, one after another, putting a newline after each; then assume that each line is a JSON object:

import json

def json_get_objects(f):
    for line in f:
        if line.strip():
            yield json.loads(line)

with open("specs_data.txt", "r") as f:
    for j in json_get_objects(f):
        pass # do something with JSON object j

I like the simplicity of option (2), but I like the reliability of option (0). If a newline ever got written in as part of a JSON object, option (0) would still work, but option (2) would error.

Again, you can also simply use an actual database (SQLite) with an ORM and let the database worry about the details.

Good luck.

Answer from steveha on Stack Overflow
Top answer
1 of 2
2

I suggest several possible solutions.

One solution is to write custom code to slurp in the input file. I would suggest putting a special line before each JSON object in the file, such as: ###

Then you could write code like this:

import json

def json_get_objects(f):
    temp = ''
    line = next(f)  # pull first line
    assert line == SPECIAL_LINE

    for line in f:
        if line != SPECIAL_LINE:
            temp += line
        else:
            # found special marker, temp now contains a complete JSON object
            j = json.loads(temp)
            yield j
            temp = ''
    # after loop done, yield up last JSON object
    if temp:
        j = json.loads(temp)
        yield j

with open("specs_data.txt", "r") as f:
    for j in json_get_objects(f):
        pass # do something with JSON object j

Two notes on this. First, I am simply appending to a string over and over; this used to be a very slow way to do this in Python, so if you are using a very old version of Python, don't do it this way unless your JSON objects are very small. Second, I wrote code to split the input and yield up JSON objects one at a time, but you could also use a guaranteed-unique string, slurp in all the data with a single call to f.read() and then split on your guaranteed-unique string using the str.split() method function.

Another solution would be to write the whole file as a valid JSON list of valid JSON objects. Write the file like this:

{"mylist":[
# first JSON object, followed by a comma
# second JSON object, followed by a comma
# third JSON object
]}

This would require your file appending code to open the file with writing permission, and seek to the last ] in the file before writing a comma plus newline, then the new JSON object on the end, and then finally writing ]} to close out the file. If you do it this way, you can use json.loads() to slurp the whole thing in and have a list of JSON objects.

Finally, I suggest that maybe you should just use a database. Use SQLite or something and just throw the JSON strings in to a table. If you choose this, I suggest using an ORM to make your life simple, rather than writing SQL commands by hand.

Personally, I favor the first suggestion: write in a special line like ###, then have custom code to split the input on those marks and then get the JSON objects.

EDIT: Okay, the first suggestion was sort of assuming that the JSON was formatted for human readability, with a bunch of short lines:

{
    "foo": 0,
    "bar": 1,
    "baz": 2
}

But it's all run together as one big long line:

{"foo":0,"bar":1,"baz":2}

Here are three ways to fix this.

0) write a newline before the ### and after it, like so:

###
{"foo":0,"bar":1,"baz":2}
###
{"foo":0,"bar":1,"baz":2}

Then each input line will alternately be ### or a complete JSON object.

1) As long as SPECIAL_LINE is completely unique (never appears inside a string in the JSON) you can do this:

with open("specs_data.txt", "r") as f:
    temp = f.read()  # read entire file contents
    lst = temp.split(SPECIAL_LINE)
    json_objects = [json.loads(x) for x in lst]
    for j in json_objects:
        pass # do something with JSON object j

The .split() method function can split up the temp string into JSON objects for you.

2) If you are certain that each JSON object will never have a newline character inside it, you could simply write JSON objects to the file, one after another, putting a newline after each; then assume that each line is a JSON object:

import json

def json_get_objects(f):
    for line in f:
        if line.strip():
            yield json.loads(line)

with open("specs_data.txt", "r") as f:
    for j in json_get_objects(f):
        pass # do something with JSON object j

I like the simplicity of option (2), but I like the reliability of option (0). If a newline ever got written in as part of a JSON object, option (0) would still work, but option (2) would error.

Again, you can also simply use an actual database (SQLite) with an ORM and let the database worry about the details.

Good luck.

2 of 2
2

Append json data to a dict on every loop.

In the end dump this dict as a json and write it to a file.

For getting you an idea for appending data to dict:

>>> d1 = {'suku':12}
>>> t1 = {'suku1':212}
>>> d1.update(t1)
>>> d1
{'suku1': 212, 'suku': 12}
Top answer
1 of 1
1

I've got a file with hundreds of JSON lines in it.

No you don't, and that's the problem.


Hundreds of JSON texts is not a valid JSON file. A valid JSON file is just one text. Which is why json.load is returning an error.


Hundreds of JSON texts that each fit on exactly one line with newlines in between them is a valid file in other formats like JSONlines or NDJ. It's still not a valid JSON file, so you can't use json.load, but you could use a JSONlines or NDJ library, or just parse it like this:

with open('fixed.json') as f:
    for line in f:
        data = json.loads(line)
        # do stuff

For writing a JSONlines file, again, you can use a JSONlines library, or you can just make sure that each JSON text has no embedded newlines—which actually happens by default, if you don't specify non-default ensure_ascii or indent parameters—and just write out json.dumps(data) + "\n" for each value.


But hundreds of JSON texts that each take up multiple lines isn't a valid anything file.

This is actually explained in the json module docs:

Note Unlike pickle and marshal, JSON is not a framed protocol, so trying to serialize multiple objects with repeated calls to dump() using the same fp will result in an invalid JSON file.

What "not a framed protocol" means is basically that the format would be ambiguous. For example, if you did a json.dump(2, f), and then a json.dump(3, f), what you'd get in your file is 23. Which is the same thing you get from json.dump(23, f).


If you can fix your file to be something valid, like JSONlines, that's the easy solution.


If you can't…

Well, pre-standardization, there was a concept of "JSON document", which meant basically a JSON text that's either an Array or Object. And a stream of JSON documents is not ambiguous.

Since this isn't a standard format, you're probably not going to find a parser for it, so you'll have to write one yourself.

One way you can do that is by using the raw_decode method in the json module. This will try to decode a JSON text, possibly with extra stuff after it, and also return the index to that extra stuff. Which, in your case, is the next JSON document.

Since hundreds of objects of that size isn't too big, it's probably simpler to just read the whole file into memory and then parse it, so we don't have to worry about buffering:

with open('fixed.json') as f:
    contents = f.read()
decoder = json.JSONDecoder()
while contents:
    data, idx = decoder.raw_decode(contents)
    do_stuff(data)
    contents = contents[idx:].lstrip()

Remember that this will only work if your file is a stream of JSON documents—that is, the top-level values are always Array or Object. Also, if you're editing these files by hand, unlike JSONlines, which can skip one bad text and continue to parse the rest, there's now way to recover from an error here, because you have no idea where the next document starts.

Discussions

Looping through JSON in python - Stack Overflow
I am a total beginner and I am trying to create a program for a course that will go through a dataset about some results. I face a difficulty getting to all the teams in the following JSON: { ... More on stackoverflow.com
🌐 stackoverflow.com
python - Read a JSON File in a FOR loop - Stack Overflow
Yes - or, even more simply, for line in json_formatted_str.splitlines(). 2025-10-27T21:36:01.273Z+00:00 ... This is probably the easiest solution. ... Mick T is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct. ... Welcome to SO - but I recommend taking a look at What should I do when someone answers my question? Don't write ... More on stackoverflow.com
🌐 stackoverflow.com
October 24, 2024
Python: Loop through JSON File - Stack Overflow
Communities for your favorite technologies. Explore all Collectives · Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work More on stackoverflow.com
🌐 stackoverflow.com
Loop through JSON data in Python - Stack Overflow
I am still new to Python and at this moment I am experimenting with GET requests and only show values from one specific String. import requests import json response = requests.get("APIURL") data = More on stackoverflow.com
🌐 stackoverflow.com
🌐
GeeksforGeeks
geeksforgeeks.org › python › loop-through-a-json-array-in-python
Loop through a JSON array in Python - GeeksforGeeks
July 23, 2025 - You can loop through a JSON array in Python by using the json module and then iterating through the array using a for loop.
🌐
Readthedocs
jsonlines.readthedocs.io
jsonlines — jsonlines documentation - Read the Docs
The sort_keys argument can be used to sort keys in json objects, and will produce deterministic output. For more control, provide a a custom encoder callable using the dumps argument. The callable must produce (unicode) string output. If specified, the compact and sort arguments will be ignored. When the flush argument is set to True, the writer will call fp.flush() after each written line.
Find elsewhere
🌐
Delft Stack
delftstack.com › home › howto › python › iterate through json python
How to Iterate Through JSON Object in Python | Delft Stack
February 2, 2024 - Using json.loads(), we convert this string into a Python dictionary (json_data). The essential part of this example is the for loop. Instead of using the more traditional approach of iterating through keys and accessing values separately, we employ the items() method. This method returns an iterable of key-value pairs, allowing us to directly unpack and print both the key and value within the loop. ... In this output, each line ...
🌐
Medium
galea.medium.com › how-to-love-jsonl-using-json-line-format-in-your-workflow-b6884f65175b
How to Love jsonl — using JSON Lines in your Workflow | by Alex Galea | Medium
April 9, 2024 - From there it can be loaded in ... format (e.g. Postgres, MySQL, CSV). Here are python functions that can be used to write and read jsonl: Let’s pick up and use these functions to process some data. Say we’re testing a website to make sure pages are returning good status codes. We’ll loop over some ...
🌐
Python Forum
python-forum.io › thread-39669.html
Loop through json file and reset values [SOLVED]
March 23, 2023 - Hello everybody, I have a json file which stores information for different people, which looks like this: { "person": }Everytime my script get's executed I want to select a random person. Once the per
🌐
Stack Overflow
stackoverflow.com › questions › 75550746 › loop-in-python-and-write-to-a-nested-json-array
Loop in Python and write to a Nested JSON array - Stack Overflow
all_tracks = get_app().project.get("layers") track_count = 0 json_data = track_dict for track in reversed(sorted(all_tracks, key=itemgetter('number'))): existing_track = Track.get(number=track.get("number")) if not existing_track: log.error('No track object found with number: %s' % track.get("number")) continue # Track name track_name = track.get("label") clips_on_track = Clip.filter(layer=track.get("number")) if not clips_on_track: continue with open("%s-%s.json" % (file_path.replace(".json", ""), track_name), 'w', encoding="utf8") as f: # Loop through clips on this track for index, clip in e
🌐
Reddit
reddit.com › r/learnpython › using python to loop through json dictionary that re-uses the same key?
r/learnpython on Reddit: Using Python to loop through JSON dictionary that re-uses the same key?
October 13, 2022 -

I'm trying to restructure JSON output from a API to the specific format that Ansible wants for it's invetory file. To do this I thought I would loop through the JSON and grab the variables I need and then feed that into the proper structure. However, I noticed my API JSON is using the same $ key for multiple variables. How can I properly reference and differentiate between the variables? Here is an example:

 "model-responses": {
        "model": [
            {
                "@mh": "0x11e013",
                "attribute": [
                    {
                        "@id": "0x1006e",
                        "$": "switch1"
                    },
                    {
                        "@id": "0x23000e",
                        "$": "Raritan Computer, Inc.DV"
                    },
                    {
                        "@id": "0x12d7f",
                        "$": "10.10.10.1"
                    },
                    {
                        "@id": "0x10052",
                        "$": "PX2 030610"
                    }
                ]
            },
            {
                "@mh": "0x115014",
                "attribute": [
                    {
                        "@id": "0x1006e",
                        "$": "switch2"
                    },
                    {
                        "@id": "0x23000e",
                        "$": "DCS-7010T-48"
                    },
                    {
                        "@id": "0x12d7f",
                        "$": "10.10.10.2"
                    },
                    {
                        "@id": "0x10052",
                        "$": "Arista Networks EOS version 4.22.3M-INT running on an Arista Networks DCS-7010T-48"
                    }
                ]
            },
🌐
Tech-otaku
tech-otaku.com › mac › using-python-to-loop-through-json-encoded-data
Using Python to Loop Through JSON-Encoded Data – Tech Otaku
The examples assume the file macos.json is located in your home directory: /User/username (macOS) and /home/username (Ubuntu). If pasting code examples containing a for loop or with statement, press enter twice to execute the code and return to the >>> Python interpreter prompt.
Top answer
1 of 2
30

The problem is that you are appending to data multiple times in the loop: first {"id":feature.pk}, then {attribute.attribute.name : attribute.value} in the inner loop.

Instead, you need to define a dictionary inside the loop, fill it with id item and attributes and only then append:

data=[]
for feature in features_selected:
    item = {"id": feature.pk}
    for attribute in attributes_selected:
        if attribute.feature == feature:
            item[attribute.attribute.name] = attribute.value
    data.append(item)

jsonData=json.dumps(data)
2 of 2
1

For those who came here looking for a way to create an arbitrary json in a loop, the question is akin to creating a Python list / dictionary in a loop because the canonical way to build a json in Python is to build the corresponding Python data structure (list -> json array, dictionary -> json object) and serialize it using json.dump (to store it in a json file) or json.dumps (to create a json string in memory).

The case in the OP may be one case where a comprehension makes the loop more intuitive and less error-prone. The nested structure in the OP can be written as a dict comprehension nested inside a list comprehension. The idea is to build a dictionary in each outer iteration using an inner iteration. Two dictionaries can be merged as well, which is put to use since an 'id' key is needed in each dict. A similar example is as follows:

data = [{'id': i+10, **{j: j // 2 for j in range(i, i+60) if j % 30 == 0}} for i in range(2)]

# convert to a json string
json_string = json.dumps(data)
# '[{"id": 10, "0": 0, "30": 15}, {"id": 11, "30": 15, "60": 30}]'

# or
# write to a json file
with open('myjson.json', 'w') as f:
    json.dump(data, f)
🌐
Quora
quora.com › How-do-I-loop-through-a-JSON-file-with-multiple-keys-sub-keys-in-Python
How to loop through a JSON file with multiple keys/sub-keys in Python - Quora
January 24, 2023 - Answer (1 of 4): You have to do it recursively. Here is an example. [code]a_dict = {'a': 'a value', 'b': {'b.1': 'b.1 value'}} def iterate(dictionary): for key, value in dictionary.items(): if isinstance(value, dict): iterate(value) continue print('key {...