I have a huge json data obtained from HTTP response and I need only a particular portion to be decoded as a model.
{
"root1": {
"items": [
{
...
},
{
...
},
{
...
},
{
...
},
{
...
}
]
},
"root2": {
...
},
"page": {
"size": 10,
"totalElements": 5,
"totalPages": 1,
"number": 0
}
}
This is my json template and I do not want to create model for root elements. I am interested only in the items
array. Any direct way to decode it?
2
Answers
You can select what to decode by using a
CodingKey
enum but you still need to decode from the top/root level.and then the decoding could be done as
When creating a
Decodable
type, you only have to declare the properties that you want to decode from JSON. All the keys that you don’t care about can be omitted. You don’t even needCodingKeys
for this.Think about the properties used for decoding as a minimum set of requirements, not as a complete set of requirements. Decoding will never fail because there are keys in the JSON that you didn’t specify on your
Decodable
model.You do need to decode from the top level through all intermediate levels though, so you have to declare top-level objects.
Decodable
models:Decoding: