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"
    }
}
]
6
  • 3
    your key is nested in the dictionary object, you are expected to use nested dictionary calls: element['fields']['idb_metric'] Commented Oct 24, 2018 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 stackoverflow.com/a/5385075/5491375 Commented Oct 25, 2018 at 0:03
  • please limit to one question per post Commented Oct 25, 2018 at 0:04
  • please include your desired output/result in the question? Commented Oct 25, 2018 at 0: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. Commented Oct 25, 2018 at 14:48

1 Answer 1

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)
Sign up to request clarification or add additional context in comments.

2 Comments

You can simplify that check to if 'idb_metric' in element["fields"]
Thanks, that worked but then next question is how to find/delete multiple keys. Updated question accordingly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.