Parsing JSON Data with Swift's Codable Protocol in Swift 4.2

Json Parsing in Swift 4.2 using Codable

Introduction

In recent years, JSON has become a widely used format for exchanging data between systems. Apple’s Swift programming language supports JSON parsing through its built-in Codable protocol. In this article, we will explore how to parse JSON data in Swift 4.2 using the Codable protocol.

Understanding Codable

The Codable protocol is a part of Swift’s standard library and allows developers to convert between Swift data types and JSON data types. The Codable protocol has two main parts: Encodable and Decodable. The Encodable protocol defines the structure of the data that can be encoded into JSON, while the Decodable protocol defines the structure of the data that can be decoded from JSON.

In our case, we are interested in using the Decodable protocol to parse JSON data.

Defining a Codable Structure

To start parsing JSON data, we need to define a Codable structure. The structure should conform to the Decodable protocol and contain properties that match the structure of the JSON data.

struct ReviewRating: Decodable {
    let data: ReviewData
    let status: Bool
    let auth: Bool
    let message: String?

    private enum CodingKeys: String, CodingKey {
        case data = "data"
        case status = "status"
        case auth = "auth"
        case message = "message"
    }

    init(from decoder: Decoder) throws {
        let container = try decoder.container(keyedBy: CodingKeys.self)
        data = try container.decode(ReviewData.self, forKey: .data)
        status = try container.decode(Bool.self, forKey: .status)
        auth = try container.decode(Bool.self, forKey: .auth)
        message = try? container.decode(String?.self, forKey: .message)
    }
}

struct ReviewData: Decodable {
    let dealer_rating: ReviewRating
    let review: [ReviewItem]

    private enum CodingKeys: String, CodingKey {
        case dealer_rating = "dealer_rating"
        case review = "review"
    }

    init(from decoder: Decoder) throws {
        let container = try decoder.container(keyedBy: CodingKeys.self)
        dealer_rating = try container.decode(ReviewRating.self, forKey: .dealer_rating)
        review = try container.decode([ReviewItem].self, forKey: .review)
    }
}

struct ReviewItem: Decodable {
    let post_author: String
    let username: String?
    let post_content: String
    let post_title: String
    let rate1: String
    let rate1_label: String
    let rate2: String
    let rate2_label: String
    let rate3: String
    let rate3_label: String
    let stm_recommended: String?
    let average: Int

    private enum CodingKeys: String, CodingKey {
        case post_author = "post_author"
        case username = "username"
        case post_content = "post_content"
        case post_title = "post_title"
        case rate1 = "rate1"
        case rate2 = "rate2"
        case rate3 = "rate3"
        case stm_recommended = "stm_recommended"
        case average = "average"
    }

    init(from decoder: Decoder) throws {
        let container = try decoder.container(keyedBy: CodingKeys.self)
        post_author = try container.decode(String.self, forKey: .post_author)
        username = try? container.decode(String?.self, forKey: .username)
        post_content = try container.decode(String.self, forKey: .post_content)
        post_title = try container.decode(String.self, forKey: .post_title)
        rate1 = try container.decode(String.self, forKey: .rate1)
        rate2 = try container.decode(String.self, forKey: .rate2)
        rate3 = try container.decode(String.self, forKey: .rate3)
        stm_recommended = try? container.decode(String?.self, forKey: .stm_received)
        average = try container.decode(Int.self, forKey: .average)
    }
}

Parsing JSON Data

To parse the JSON data, we need to create an instance of the ReviewRating structure. We can do this by using the JSONDecoder class and passing in our ReviewRating structure as the type.

let jsonData = try! JSONEncoder().encode(ReviewRating(data: ReviewData(dealer_rating: ReviewRating(), review: []), status: true, auth: true, message: "Review found."))
let jsonDecoder = JSONDecoder()
let reviewRating = try! jsonDecoder.decode(ReviewRating.self, from: jsonData)

Conclusion

In this article, we have explored how to parse JSON data in Swift 4.2 using the Codable protocol. We defined a Decodable structure that conforms to the Codable protocol and contains properties that match the structure of the JSON data. We also parsed JSON data by creating an instance of the ReviewRating structure.

Note: This is just one way to parse JSON data in Swift 4.2. There are many other ways to do it, depending on your specific requirements and constraints.


Last modified on 2023-06-08