'How to compare keys in json response and keep only matching ones
Python newbie here and while I find my foothold in Python, I have a use case where I query an API and get back a json response (list of nested dictionaries). Not all the dictionaries in this response have the same keys. Example response is below. I want to be able to compare the keys, ignore the keys that do not exist across the response and load into my DB. So in the following example - ignore the "parallelExecutionAllowed" key from the first dictionary (because it doesn't exist in the second) and the "errorMessage" key from second. Please note that I don't know what keys to ignore beforehand; there has to be a comparison of keys and then dynamic removal of non-matching keys. Thanks in advance.
{“items": [{
"executionId": "f9d521-4ab-68-a8-f4d75d99a",
"startTimestamp": "2022-04-27T12:58:13.703Z",
"finishTimestamp": "2022-04-27T13:13:00.359Z",
"userId": “abc”,
"status": "execution_successful",
"runtime": {
"type": "REMOTE",
"engineId": "615ac51e2fbe7"
},
"executionStatus": "EXECUTION_SUCCESS",
"parallelExecutionAllowed": false
},
{
"executionId": "28485e-63c-4b-a8-450a9a1a0",
"startTimestamp": "2022-04-27T11:56:17.986Z",
"finishTimestamp": "2022-04-27T12:08:11.876Z",
"userId": “xyzzy”,
"status": "execution_successful",
"runtime": {
"type": "REMOTE_,
"engineId": "61546e066a"
},
"executionStatus": "EXECUTION_SUCCESS”,
“errorMessage”: “Expected result not met”
}
]}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
