0

Looking for help to decode irregular (for a lack of a better word) json. As an example:

[
    {
        "texts":
        [
            {
                "value": "value 1"
            }
        ],
    
        "commentType": "someComment"
    },
    
    {
        "texts":
        [
            {
                "value": "value 2"
            }
        ],
        "commentType": "someComment2"
    },
    
    {
        "texts":
        [
            {
                "evidences": 
                [
                    {
                        "evidenceCode": "code 1",
                    },
                    {
                        "evidenceCode": "code 2",
                    },
                    {
                        "evidenceCode": "code 3",
                    },
                    {
                        "evidenceCode": "code 4",
                    }
                ],
                
                "value": "value 3"
            }
        ],
        "commentType": "someComment3"
    }
]

I can decode comment and the first two "texts":

enum CodingKeys: String, CodingKey {
    case texts
    case commentType
}

do {
    let container = try decoder.container(keyedBy: CodingKeys.self)
    let name = try container.decode(String.self, forKey: .commentType)
    
    if let texts = try container.decodeIfPresent([[String: String]].self, forKey: .texts) {
        for text in texts {
            if let value = text["value"] {
                // add to model object
            }
        }
    }

} catch {
    print(error)
}

But I get an error for the third "texts" block:

"Expected to decode String but found an array instead."

Which I understand, since now instead of an array of [String:String], it is a mixed array of [String:[String:String] and [String:String].

How do I decode value3 from that third block?

1 Answer 1

2

Actually this is not complicated at all because the content of texts is the same if we treat evidences as an optional array. The below models will decode the json correctly without any custom code.

struct Result: Decodable {
    var texts: [TextData]
    var commentType: String
}

struct TextData: Decodable {
    let evidences: [Evidence]?
    let value: String
}

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

Comments

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.