Easy JSON parsing with Codable

Tudip Technologies
1 min readOct 29, 2021

Swift codable protocol has a combination of Encodable and Decodable protocol that can be used to work with JSON data in both directions. In short, Swift Codable protocol has offered the following features:

  • Using Codable, by writing very few lines of code, we can model JSONObject or PropertyList file into equivalent struct or classes. For the properties in the objects we don’t have to write the constructor. Codable hands it all. We simply need to extend our model to conform to the protocol for coding, decoding or encoding.
  • Swift compiler handled the mismatch between the strong data types of Swift and lose data types of JSON internally. We can now manage types of Swift Data such as Date, URL, Float, etc. Complex JSON can be easily modelled using readability Nesting Structs.
  • Complex JSON can be easily modelled using readability Nesting Structs.
  • Use JSONDecoder to parse actual JSON to become one-liner.

Now Let’s Get Into The Fun Part of coding

There is an API for displaying Zomato cities, and the endpoint is for Pune:

If you want to try you can try that out on link.

Read more: https://tudip.com/blog-post/easy-json-parsing-with-codable/

--

--