Debunking the myths about parsing JSON in Swift

There are a bunch of myths out there about parsing JSON in Swift. And the problem with these myths is that they lead us to solve imaginary problems using bloated frameworks or wacky techniques that gain us very little, especially considering the cost they impose on us. Here are a few of the most popular myths — and the truth about them — so you can better handle JSON in Swift.

Myth #1: You need crazy nested if checks to parse JSON in Swift

False. The days of Swift 1.1 and the Pyramid of Doom are long behind us. You don’t have to write code like this any more:

// EXTREMELY OUTDATED TECHNIQUE FOR PARSING JSON IN SWIFT
if let data = data {
    if let json = try NSJSONSerialization.JSONObjectWithData(data, options: []) as? [String: AnyObject] {
        if let name = json["name"] as? String {
            if let description = json["description"] as? String {
                print("name: \(name), description: \(description)")
            }
        }
    }
}

And you don’t have to use a third-party framework to avoid such horrible code. You can use Swift’s multiple optional binding instead:

if let data = data,
    json = try NSJSONSerialization.JSONObjectWithData(data, options: []) as? [String: AnyObject],
    name = json["name"] as? String,
    description = json["description"] as? String {
        print("name: \(name), description: \(description)")
}

Et voilà! Same result, better code. Alternatively, you could use one or more guard statements — which similarly don’t need to be nested. Repeat after me: good developers don’t use crazy nested if statements.

Myth #2: You need wacky operators to parse JSON in Swift

What’s a wacky operator, you ask? I suppose that’s subjective, but these look pretty wacky to me:

<*>
<~~
<^>
<-
<||
<--

Each of these is a real operator from a third-party JSON framework, and if you’re going to use one of those frameworks, you certainly do need to use their wacky operators. But you don’t have to use one of those frameworks. There are two good alternatives:

  1. Do it yourself. Write your own JSON parsing code without using a third-party framework (which is really quite simple).
  2. Use a third-party framework that doesn’t require you to use wacky operators. Some of the most popular ones don’t define new operators and don’t force you to use them.

You don’t need wacky operators to parse JSON in Swift.

Myth #3: It’s fine to use the ! operator occasionally

This one is rarely stated explicitly, but is often implied by JSON tutorials around the web. Here are some examples of the ! operator in use, pulled straight from those tutorials, with a bit of my own commentary:

// the crash-prone implicitly unwrapped optional
var json: [String: AnyObject]!
// the crash-prone forced cast operator
let name = json["name"] as! String
// the crash-prone forced unwrapping of an optional
if let results = dict!["results"] as? [String: AnyObject]

All of these snippets can easily be rewritten so they’re safer and less crash-prone.

To summarize: your app will crash if you use the ! operator. I like to call it The Crash Operator due to its impeccable ability to cause apps to “quit unexpectedly”. Find a better approach, like declaring non-optional types, using the conditional cast operator, or using optional binding.


Good developers don’t use crazy nested if statements. You don’t need wacky operators. Your app will crash if you use the ! operator. And you (yes, you!) can write safe, elegant JSON parsing code without a third-party framework.