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"
}
}
]