-1

The idea:
A JSON file should be loaded and the object 2-uID with its sub-items should be deleted. The edited content should be saved in the same JSON file.

The problem:
I have already tried several approaches, like this one from an SO user, but nothing has worked for me yet.
The code shown does not delete anything, but pushes the content all into one line.

The current code
Python code:

import json

with open("path/to/json") as data_file:
    data = json.load(data_file)

for element in data:
    if '2-uID' in element:
        del element['2-uID']

with open("path/to/json", 'w') as data_file:
    data = json.dump(data, data_file)

JSON file:

    {
    "uID": {
        "1-uID": {
            "username": "1-username",
            "pinned": true
        },
        "2-uID": {
            "username": "2-username",
            "pinned": false
        },
        "3-uID": {
            "username": "3-username",
            "pinned": false
        }
    }
}

This is how the JSON file should look like after the process:

    {
    "uID": {
        "1-uID": {
            "username": "1-username",
            "pinned": true
        },
        "3-uID": {
            "username": "3-username",
            "pinned": false
        }
    }
}
JueK3y
  • 267
  • 9
  • 22
  • 1
    Once a JSON formatted data is read by python, it is stored as a list of dictionaries/lists. For your case, you can use `pop()` to delete a particular key. See https://stackoverflow.com/questions/11277432/how-can-i-remove-a-key-from-a-python-dictionary. Your example only deletes a copy of the key, not from the actual list of dictionaries/lists. – yoonghm Dec 22 '21 at 10:31

1 Answers1

1

EDIT:

This is your problem:

for element in data:
    if '2-uID' in element:
        del element['2-uID']

data is the top-level element, so it only has one key: "uID". Try printing out "element" :)


Maybe I'm misundertanding you, but shouldn't you just use del ?

This works for me

# string holding the JSON object
s = r"""{
    "uID": {
        "1-uID": {
            "username": "1-username",
            "pinned": true
        },
        "2-uID": {
            "username": "2-username",
            "pinned": false
        },
        "3-uID": {
            "username": "3-username",
            "pinned": false
        }
    }
}"""

import json

j = json.loads(s)

# remove one of the elements
del j["uID"]["2-uID"]

# see that the element is now gone
print(j)

# output:
# {'uID': {'2-uID': {'pinned': False, 'username': '2-username'},
#          '3-uID': {'pinned': False, 'username': '3-username'}}}
Morten Jensen
  • 5,818
  • 3
  • 43
  • 55
  • Does its job somehow - However (related to the JSON file) the content is written under the previous content. So I still have the old content with 2-uID and additionally in the same file the new content without 2-uID. – JueK3y Dec 22 '21 at 10:46
  • Then you need to check how you store to disk. Are you perhaps opening the file in "append"-mode? I have a feeling that the code you have posted, is a simplification of your actual code, no? – Morten Jensen Dec 22 '21 at 10:49
  • That is correct, the actual code is much larger. However, the question refers to a specific function that should also run independently. I open the file with r+, so the problem shouldn't come from that – JueK3y Dec 22 '21 at 10:57
  • 1
    Ugh, never mind, I was able to fix the exporting problem. I forgot to use "json.dump(j, delObj)" with "with open(path, "w") as jsonFile:". – JueK3y Dec 22 '21 at 11:03
  • 1
    @JueK3y glad you found a solution :) If two things are "wrong" simultaneously, it can be extremely difficult to diagnose the problem! – Morten Jensen Dec 22 '21 at 11:10