0

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"
    }
}
]
timsterc
  • 963
  • 3
  • 10
  • 18
  • 3
    your `key` is nested in the dictionary object, you are expected to use nested dictionary calls: `element['fields']['idb_metric']` – Rocky Li Oct 24 '18 at 23:11
  • as an FYI, be careful about deleting elements from a `dict` that you are iterating over. in this case it's ok, because the you aren't actually removing the elements over which you are iterating https://stackoverflow.com/a/5385075/5491375 – aydow Oct 25 '18 at 00:03
  • [please limit to one question per post](https://meta.stackexchange.com/questions/222735/can-i-ask-only-one-question-per-post) – chickity china chinese chicken Oct 25 '18 at 00:04
  • please include your desired output/result in the question? – chickity china chinese chicken Oct 25 '18 at 00:12
  • @davedwards Added expected results. Also, I wouldn't say this is a second question as the original question was not satisfactorily answered. This is why I updated my comments for clarity. – timsterc Oct 25 '18 at 14:48
  • To your second question, use another `if` rather than `elif`. The `elif` won't run if the the first `if` is satisfied. – Joel Carlson Oct 25 '18 at 17:05

1 Answers1

2

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)
Joel Carlson
  • 630
  • 3
  • 9