How to delete multiple json objects from a big json file

2.2k views Asked by At

Hello every one i tried the following code, work best for deleting an object from the json file

#!/usr/bin/python

# Load the JSON module and use it to load your JSON file.
# I'm assuming that the JSON file contains a list of objects.
import json
obj  = json.load(open("pandas.json"))

# Iterate through the objects in the JSON and pop (remove)
# the obj once we find it.
for i in range(len(obj)):
    #path = ["000000000036.jpg","000000000049.jpg", "000000000077.jpg"]
    if obj[i]["path"] == "000000000036.jpg":
        obj.pop(i)
        break

# Output the updated file with pretty JSON
open("updated_file.json", "w").write(
    json.dumps(obj)
)

here i have a question what i suppose to do if i want to delete many random json objects from my json file i tried but fail please let me clear on it "help appreciate".

3

There are 3 answers

3
Pradip On

Assuming the json data is an array of json elements, the below code will remove 3 random elements from it. Customize it as per your need.

import json
import random

data = json.load(open("filepath.json"))
print(json.dumps(data, indent=4))

keys_to_be_deleted = [random.randint(0, len(data)-1) for _ in range(3)]
[data.pop(i) for i in keys_to_be_deleted]
print(json.dumps(data, indent=4))
0
Ni Az On
import json
obj = json.load(open("new_pandas.json"))

# Iterate through the objects in the JSON and pop (remove)
# the obj once we find it.
for i in range(len(obj)):
    path = ["000000000036.jpg","000000000049.jpg","000000000077.jpg"]

    for j in path:
        
        if obj[i]["path"] == j:
            obj.pop(i)
            break


# Output the updated file with pretty JSON
open("updated-file.json", "w").write(
    json.dumps(obj)
)
0
Ni Az On

I found the way how to delete specific json objects from big json file, here is an example code.

#!/usr/bin/python

# Load the JSON module and use it to load your JSON file.
# I'm assuming that the JSON file contains a list of objects.
import json
obj = json.load(open("new_pandas.json"))

# Iterate through the objects in the JSON and pop (remove)
# the obj once we find it.

num = 0
n = 0
flag = False

for i in range(len(obj)):
    path = ["000000170035.jpg","000000171962.jpg","000000004972.jpg"]

    for j in path:

        if obj[n]["path"] == j:
            print(obj[n]["path"])
            obj.pop(n)
            flag = True
            break

    if flag == False: # Cannot find pass forward 
        n = n + 1
    else:
        flag = False


    if obj[i]["path"] == "000000581357.jpg": #end object of json file
        break

    # print(obj[i]["path"])
    num = num + 1
    if num >= 100000:
        break

# Output the updated file with pretty JSON
open("updated-file.json", "w").write(
    json.dumps(obj)
)