timsterc
timsterc

Reputation: 1033

Removing JSON key from file in python

I have a file with multiple JSON objects, like so:

[
  {
"fields": {
    "project": {
        "key": "GID"
    },
    "description": "",
    "documentKey": "AB-TC-678",
    "globalId": "GID-462256",
    "idb_metric": "framework.jama.infra.tagged.testcases",
    "lastActivityDate": "2017-12-19",
    "name": "Admin: Action commands",
    "sequence": "4.13.2.13.5.1.17",
    "testCaseStatus": "PASSED",
    "testCaseSteps": "FIELD_NOT_PRESENT"
   }
},
{
"fields": {
    "project": {
        "key": "GID"
    },
    "description": "",
    "documentKey": "AB-TC-679",
    "globalId": "GID-462256",
    "idb_metric": "framework.jama.infra.tagged.testcases",
    "lastActivityDate": "2017-12-19",
    "name": "Admin: Action not",
    "sequence": "4.13.2.13.5.1.18",
    "testCaseStatus": "PASSED",
    "testCaseSteps": "FIELD_NOT_PRESENT"
    }
}
]

What I'm trying to do is delete lines where key=x, for example, where key="idb_metric". Using answers/comments below (thanks!), I'm almost there:

if 'idb_metric' in element["fields"]:
    print("Found idb_metric!")
    del element['fields']['idb_metric']
    print(element)

But if I look for a second key by adding an elif, only the first key (in the first if block) is found/deleted:

elif 'sequence' in element["fields"]:
        print("Found sequence!")
        del element['fields']['sequence']
        print(element)

Expected results if I were to use the above code:

[
  {
"fields": {
    "project": {
        "key": "GID"
    },
    "description": "",
    "documentKey": "AB-TC-678",
    "globalId": "GID-462256",
    "lastActivityDate": "2017-12-19",
    "name": "Admin: Action commands",
    "testCaseStatus": "PASSED",
    "testCaseSteps": "FIELD_NOT_PRESENT"
   }
},
{
"fields": {
    "project": {
        "key": "GID"
    },
    "description": "",
    "documentKey": "AB-TC-679",
    "globalId": "GID-462256",
    "lastActivityDate": "2017-12-19",
    "name": "Admin: Action not",
    "testCaseStatus": "PASSED",
    "testCaseSteps": "FIELD_NOT_PRESENT"
    }
}
]

Upvotes: 0

Views: 13513

Answers (1)

Joel Carlson
Joel Carlson

Reputation: 640

As one of the commenters mentioned, you aren't indexing into the correct field in your json dictionary.

To fix:

with open('input.json',encoding='utf8') as in_file:
    data = json.load(in_file)
    for element in data:
        del element["fields"]["idb_metric"]

    print(element["fields"])

It may also save you some headaches if you make sure that the key exists before trying to delete it. This is one way to check if it exists:

with open('input.json',encoding='utf8') as in_file:
    data = json.load(in_file)
    for element in data:
        if element.get("fields") is not None:
            if element["fields"].get("idb_metric") is not None:
                del element["fields"]["idb_metric"]
    print(element["fields"])

As an answer to your edited question, don't use elif:

if 'idb_metric' in element["fields"]:
    print("Found idb_metric!")
    del element['fields']['idb_metric']
    print(element)

if 'sequence' in element["fields"]:
    print("Found sequence!")
    del element['fields']['sequence']
    print(element)

Upvotes: 2

Related Questions