Use SwiftyJSON to deal with JSON data in Swift

How many time did you get lost in optional chaining while dealing with JSON data in Swift? The struggle is real and code gets really messy while parsing complex JSON data.

Let us take an example of the following type of JSON data that you might receive from API Call:

Standard Way

Now to access the “title” of the “glossary”, the standard way we would write is:

Optional Chaining

Or if we do optional chaining it will look messy even if we are just accessing first level element of JSON

SwiftyJSON Way

Now, let us see how to parse it using SwiftyJSON:

Yeah! no tension of optional unwrapping, simply access it via keys you want and SwiftyJSON takes care of the rest.

And guess what? Even if you access a wrong key by mistake, .string property takes care of safety.

Hence, SwiftyJSON makes our life easier to deal with JSON data compared to the standard way of dealing with it.

To use SwiftyJSON, you can integrate it either via Cocoapods or Carthage.

And finally, there is even an extension to make serializing Alamofire‘s response with SwiftyJSON easily: Alamofire-SwiftyJSON.

Found it useful? Share it with other fellow developers to help them write more swiftier code.