Any Idea how to Decode this JSON

Can Anyone help me out in decoding this JSON
Code Block
{
"name": "Foo",
"age": 34,
"activities": [
{
"test1": "testing",
"type": "xyz",
"three": {
"label1": "Testing1",
"content": [
{
"mark1": "markTest1",
"mark2": "markTest2"
}
]
}
},
{
"test2": "Running",
"type": "zzzz",
"three": [
{
"label2": "Testing1"
},
{
"label2": "Testing1"
}
]
}
]
}

Accepted Reply

You can try it like this:
Code Block Swift
        let demo = """
{"name":"Foo","age":34,"activities":[{"test1":"testing","type":"xyz","three":{"label1":"Testing1","content":[{"mark1":"markTest1","mark2":"markTest2"}]}},{"test2":"Running","type":"zzzz","three":[{"label2":"Testing1"},{"label2":"Testing1"}]}]}
"""
        guard let data = demo.data(using: .utf8) else { return }
        let decoded = try? JSONDecoder().decode(TopLevel.self, from: data)
        print(decoded?.name)

```

Replies

Is this the right structure of your JSON as referred in my comment of another thread of yours?

You should better leave some replies to the comments you get, for better communication.
Yes this is the right JSON any thoughts on how to decode this structure.?
In my opinion, it is far from well-made-for-Decodable. My best recommendation is to contact to the server side engineer and request them to update the JSON structure in order to fit for Decodable.

Do you insist on using it as is?
Yeah. Is there any way of decoding this type of JSON. I checked we can use enums but i'm kinda wage on using enum can you help me out this!
does this stack overflow post answer your question? https://stackoverflow.com/questions/24109608/json-deserialization-in-swift

all you want is to convert the json object to a swift object so you can access the data, right?

Could you show the code where you try to decode, and the struct you use to match JSON model ?
@aamirkhan

Yeah. 

Can you tell me why you choose the difficult and hard way?

The below code should be able to decode that structure.

Code Block Swift
import Foundation
// MARK: - TopLevel
struct TopLevel: Codable {
    let name: String
    let age: Int
    let activities: [Activity]
    
    enum CodingKeys: String, CodingKey {
        case name
        case age
        case activities
    }
}
// MARK: - Activity
struct Activity: Codable {
    let test1: String?
    let type: String
    let three: ThreeUnion
    let test2: String?
    
    enum CodingKeys: String, CodingKey {
        case test1
        case type
        case three
        case test2
    }
}
enum ThreeUnion: Codable {
    case purpleThree(PurpleThree)
    case threeElementArray([ThreeElement])
    
    init(from decoder: Decoder) throws {
        let container = try decoder.singleValueContainer()
        if let x = try? container.decode([ThreeElement].self) {
            self = .threeElementArray(x)
            return
        }
        if let x = try? container.decode(PurpleThree.self) {
            self = .purpleThree(x)
            return
        }
        throw DecodingError.typeMismatch(ThreeUnion.self, DecodingError.Context(codingPath: decoder.codingPath, debugDescription: "Wrong type for ThreeUnion"))
    }
    
    func encode(to encoder: Encoder) throws {
        var container = encoder.singleValueContainer()
        switch self {
        case .purpleThree(let x):
            try container.encode(x)
        case .threeElementArray(let x):
            try container.encode(x)
        }
    }
}
// MARK: - ThreeElement
struct ThreeElement: Codable {
    let label2: String
    
    enum CodingKeys: String, CodingKey {
        case label2
    }
}
// MARK: - PurpleThree
struct PurpleThree: Codable {
    let label1: String
    let content: [SBContent]
    
    enum CodingKeys: String, CodingKey {
        case label1
        case content
    }
}
// MARK: - SBContent
struct SBContent: Codable {
    let mark1: String
    let mark2: String
    
    enum CodingKeys: String, CodingKey {
        case mark1
        case mark2
    }
}


You can try it like this:
Code Block Swift
        let demo = """
{"name":"Foo","age":34,"activities":[{"test1":"testing","type":"xyz","three":{"label1":"Testing1","content":[{"mark1":"markTest1","mark2":"markTest2"}]}},{"test2":"Running","type":"zzzz","three":[{"label2":"Testing1"},{"label2":"Testing1"}]}]}
"""
        guard let data = demo.data(using: .utf8) else { return }
        let decoded = try? JSONDecoder().decode(TopLevel.self, from: data)
        print(decoded?.name)

```