Home > Back-end >  When trying to remove key/value from json file I get 'str' object has no attribute 'p
When trying to remove key/value from json file I get 'str' object has no attribute 'p

Time:06-25

I have a json file with this structure:

{
  "collections": {
    "tags": {
      "2GvAB3TrWwJSdDeAdg4R": {
        "id": "",
        "aperances": 1,
        "tagName": "todolist",
        "__collections__": {}
      }
   }
}

what I want is to remove from the "2GvAB3TrWwJSdDeAdg4R" object(or the objects inside "tags") the attributes "id" and "__ collections __", So i can have the object like:

{
"collections": {
   "tags": {
      "2GvAB3TrWwJSdDeAdg4R": {
         "aperances": 1,
         "tagName": "todolist",
        }
     }
}

This is the code I have tried but using pop or del will give me a AttributeError: 'str' object has no attribute ....

import json
with open('jsontest.json', 'r ') as f:
    data = json.load(f)
    for element in data:
        for attibute in element:
            attibute.pop('id', None)
            attibute.pop('_collections', None)

with open('jsontest.json', 'w') as data_file:
    data = json.dump(data, f)

Any help appreciated, Thanks!

CodePudding user response:

you are accessing keys in for loop and not value. try following

data = {
  "collections": {
    "tags": {
      "2GvAB3TrWwJSdDeAdg4R": {
        "id": "",
        "aperances": 1,
        "tagName": "todolist",
        "__collections__": {}
      }
   }
}
}

for element in data:
    for tag in data[element]:
        for attribute in data[element][tag]:
            data[element][tag][attribute].pop('id', None)
            data[element][tag][attribute].pop('_collections', None)

CodePudding user response:

The problem is that when you iterate over a dictionary, you just get the keys by default. We can see this by printing the variables in your code:

for element in d:
    print(element)
    for attribute in element:
        print(attribute)

The first variable is a key, ie a string, and when we iterate it we get another string, ie each of its characters:

collections
c
o
l
l
e
c
t
i
o
n
s

The way I interpret the data, we can just index "collections" and "tags", and then iterate:

for element in data:
    print(element)
    for attribute in element:
        print(attribute)        

collections = data["collections"]
tags = collections["tags"]

for k in tags:
    del(tags[k]["id"])
    del(tags[k]["__collections__"])

print(data)
# {'collections': {'tags': {'2GvAB3TrWwJSdDeAdg4R': {'aperances': 1, 'tagName': 'todolist'}}}}

CodePudding user response:

you are completely doing it wrong man, this is not the way to iterate over a dictionary in python. if you have the schema you can directly delete the values like this too and after that you can dump data to file.

import json
with open('jsontest.json', 'r ') as f:
    data = json.load(f)
    print("Orignal Data: ",data)
    del data['collections']['tags']['2GvAB3TrWwJSdDeAdg4R']['id']
    del data['collections']['tags']['2GvAB3TrWwJSdDeAdg4R']['__collections__']
    print("Updated Data: ",data)

CodePudding user response:

You can do that too

with open(r'jsontest.json', 'r ') as f:
    data = json.load(f)
    del(data["collections"]["tags"]["2GvAB3TrWwJSdDeAdg4R"]['id'])
    del(data["collections"]["tags"]["2GvAB3TrWwJSdDeAdg4R"]['__collections__'])

result:

{'collections': {'tags': {'2GvAB3TrWwJSdDeAdg4R': {'aperances': 1, 'tagName': 'todolist'}}}}
  • Related