Parsing JSON Using the Swift Built-in Mechanism
A how-to guide when it comes to parsing JSON using the friendly Swift built-in mechanism.
Nowadays most applications communicate with a server in the cloud. The two most common formats which are used over and over for data interchange are XML and JSON. I’ll explain how to parse JSON using the friendly Swift built-in mechanism, which is a basic task when writing a networking layer. The first step is to understand encoding and decoding definitions.
ENCODING / SERIALIZATION
Encoding is the process that transforms human-readable data (JSON) into binary data — basically, a stream of bytes. There are some types in Foundation which are already encodable, for example, Int, Double, String, and Date.
If we provide our custom type struct or enum, we have to extend it with Encodable protocol — this means that this data structure can be easily transformable to binary data.
JSONEncoder class contains a throwing function which requires 1 argument value which has to be Encodable and must return data.
Encoding string
Encoding struct to data
If we decode this data we receive the JSON below
But, what if we want to change our JSON a little bit?
We need to implement a custom encoding function
JSON will look like this
DECODING / DESERIALIZATION
Decoding is the reverse process that allows the transformation of binary data to a format readable by humans. If we want to use some struct for decoding the binary data, all we need to do is conform to Decodable protocol.
JSONDecoder class contains a throwing function which requires 2 arguments: the type of data to decode and the data to decode and it returns the value of the requested type.
Decoding binary data
Decoding different types of JSON
Many times the backend API that we consume doesn’t look as we expect it to. I’ve been in a lot of situations when I wanted to avoid one level of nested properties, change properties name a little bit or change the JSON’s structure. It’s not always doable because, for example, when a backend API is already completed, there are no backend developers on board and there is no one to improve the backend API. Still, if we want to improve the way we operate on data from backend we need to change a few things on our side.
Decoding objects array (custom keys)
Array with only one valid element
Codable typealias
In the Swift standard we have defined typealias for Encodable & Decodable protocols
In case our custom type needs to support encoding & decoding, instead of writing this
We can make it even shorter using Codable
Parsing JSON made easy
Parsing JSON with a built-in mechanism has become much easier since Apple introduced JSONDecoder and JSONEncoder classes. It’s really easy to use and the big advantage is that we can handle network response without integrating third-party libraries. We don’t need to write boilerplate and the code is much more readable.
Using Encodable & Decodable protocols with default key parameters, without any customization, couldn’t be easier! If you want to transform struct to JSON, conform to Encodable. If you want to parse JSON, your model should conform to Decodable and that’s all that you need to do. Last but not least — I really ❤️ that from Swift 4 you don’t need any additional libraries to parse JSON in your application!