My JSON
response is the following:
{
"data": [
{
"unknown-key-c3e7f0": {
"date_time": 1546944854000,
"medication": "f4f25ea4-0607-4aac-b85a-edf40cc7d5b6",
"record": {
"status": "never"
}
},
"unknown-key-619d40": {
"date_time": 1546944854000,
"medication": "deef2278-f176-418f-ac34-c65fa54e712c",
"record": {
"status": "always"
}
},
"event": "06b445b9-dae0-48a1-85e4-b9f48c9a2349",
"created": 1546949155020,
"user": "8fb3fcd1-ffe6-4fd9-89b8-d22b1653cb6a",
"id": "1546944855000",
"type": "compliance"
},
{
"unknown-key-619d40": {
"date_time": 1546944975000,
"medication": "deef2278-f176-418f-ac34-c65fa54e712c",
"record": {
"status": "sometimes"
}
},
"event": "7309d8e9-b71c-4068-b278-0ae6d91a57a6",
"created": 1546946798407,
"user": "8fb3fcd1-ffe6-4fd9-89b8-d22b1653cb6a",
"id": "1546944975000",
"type": "compliance"
}
}
From the above response, I want to get the unknown keys and their values. The values of the unknown keys are of a custom type called Record
which conforms to the Codable
protocol.
I have created this struct for the parsing the data
struct RecordSuper: Codable
{
var data: [[String: Record]]
}
So, I want to filter all other keys like event, created, user
, etc which I am getting in the response and save only the unknown keys and the values.
Please suggest how to parse this using codable.
I have gone through this answer as well as the variation suggested in the third comment of the answer. https://stackoverflow.com/a/46369152/8330469
This answer shows how to filter the incorrect data in an Array so that the correct data is not lost. I am trying to do something similar.
For example, I want to discard the event
key because it is of type String
and not of type Record
.
The above answer will discard the whole dictionary because all the dictionaries are have incorrect data like event
. And in the end, I get an empty array.
Thanks in advance.
Codable
for decoding/encoding JSON data. I use my own framework MapCodableKit or if you prefer something more popular you can use ObjectMapper. – JacobMapCodableKit
definately would work for you in this case and I'm pretty sureObjectMapper
would too. But I'm myself curious how someone solves this usingCodable
. – Jacob