0

I receive a complex json response from the API something similar to this.

{
    "result": "success",
    "count": 100,
    "details": [{
            "unnecessaryDetails": "something",
            "area": {
                "name": "Test1"
            }
        },
        {
            "unnecessaryDetails": "something",
            "area": {
                "name": "Test2"
            }
        },
        {
            "unnecessaryDetails": "something",
            "area": {
                "name": "Test3"
            }
        }
    ]
}

My struct is

struct Person {
    var name: String
}

struct Response {
    var result: String
    var count: Int
    var details: [Person]
}

I don't want to create properties for everything I receive from the response. I can't ask the backend developer to give the necessary details only. How to avoid unnecessary details and create struct with require details only?

2
  • have your tried app.quicktype.io ? try it once. It generates Codable class/struct instantly for your json response. Commented Nov 27, 2019 at 5:29
  • @KeshuRai I used it. It creates properties for all variables. I don't want everything Commented Nov 27, 2019 at 5:32

2 Answers 2

1

You can skip intermediate arrays and dictionaries with nested containers.

struct Person : Decodable {
    let name: String
}

struct Response : Decodable {
    let result: String
    let count: Int
    let details: [Person]

    enum CodingKeys: String, CodingKey { case result, count, details }
    enum DetailCodingKeys: String, CodingKey { case area }

    init(from decoder : Decoder) throws {
        let container = try decoder.container(keyedBy: CodingKeys.self)
        result = try container.decode(String.self, forKey: .result)
        count = try container.decode(Int.self, forKey: .count)
        var detailContainer = try container.nestedUnkeyedContainer(forKey: .details)
        var people = [Person]()
        while !detailContainer.isAtEnd {
            let areaContainer = try detailContainer.nestedContainer(keyedBy: DetailCodingKeys.self)
            let person = try areaContainer.decode(Person.self, forKey: .area)
            people.append(person)
        }
        details = people
    }
}

However the effort is much bigger than adding the extra struct

struct Response : Decodable {
    let result: String
    let count: Int
    let details: [Detail]
}

struct Detail : Decodable {
    let area : Person
}

struct Person : Decodable {
    let name: String
}
Sign up to request clarification or add additional context in comments.

1 Comment

"However the effort is much bigger than adding the extra struct" I have given a simple json for example. Actual json is much more complicated. I'll try both. Thanks
0

You can use Codable to parse only property you want to parse and rest of all will be ignored, If you want to parse json in 2 separate models, you can follow this question's answer.

Is it possible to decode single level JSON into 2 separate models?

1 Comment

I have nested json response. Given link isnt useful

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.