How to parse JSON with Swift 2

Are you using Swift 3 or 4? Read the Swift 3 version or the Swift 4 version of this article to learn how to parse JSON in newer versions of Swift.

I’ve written about how to parse JSON in Swift before, but someone asked recently about how to do it with Swift 2 and I realized that everything changed from Swift 1.2 to Swift 2.0. So how do you parse JSON with Swift 2?

We’ll look at how to parse a JSON array that we could use to display a list of items in a table view. More spefically, we’ll use the following JSON (copied and tweaked from David Owens II) to display a list of blog titles in our app.

{
    "blogs": [
        {
            "needspassword": true,
            "id": 73,
            "url": "http://remote.bloxus.com/",
            "name": "Bloxus test"
        },
        {
            "needspassword": false,
            "id": 74,
            "url": "http://flickrtest1.userland.com/",
            "name": "Manila Test"
        }
    ],
    "stat": "ok"
}

Assuming we’ve made a request and received the JSON above, we just need to ask NSJSONSerialization to give us a JSON object, and then pull out the “blogs” and “name” keys. First, take a look at the full code, then we’ll break it down and explain it in smaller pieces:

var names = [String]()

do {
    let json = try NSJSONSerialization.JSONObjectWithData(data, options: .AllowFragments)

    if let blogs = json["blogs"] as? [[String: AnyObject]] {
        for blog in blogs {
            if let name = blog["name"] as? String {
                names.append(name)
            }
        }
    }
} catch {
    print("error serializing JSON: \(error)")
}

print(names) // ["Bloxus test", "Manila Test"]

Let’s break down what’s actually happening here.

First of all, everything is surrounded in a do/catch block since JSONObjectWithData:options: may throw an error. If you’re familiar with Objective-C, you’ll notice that we don’t pass in an NSError pointer here – instead, JSONObjectWithData is marked with throws, meaning it may throw an error. The error that we catch (which is called error by default) replaces the NSError pointer from Objective-C.

Let’s look at the next line (with a bit of commentary added):

if let blogs = json["blogs"] as? [[String: AnyObject]] {
    // if we get "blogs" from the JSON
    // AND we can cast it to an array of dictionaries
    // then this code will execute
}

Here, we’re attempting to take "blogs" from the JSON, cast it to an array of dictionaries ([[String: AnyObject]]), and assign it to a constant called blogs. If all of that is successful, our if block will execute. (Aside: this if let syntax is called optional binding, and you need to understand it if you’re writing Swift. You can read about it in The Swift Programming Language.)

Moving on:

for blog in blogs {
    if let name = blog["name"] as? String {
        names.append(name)
    }
}

Here, we’re iterating through our array of blogs, and we know that blog is a dictionary of type [String: AnyObject] based on the type of our blogs array. Inside the for loop, we’re doing optional binding again to grab the name of the blog as a String, then appending it to our names array.

Conclusion

I hope this helps you understand how to parse JSON in Swift 2. It’s relatively simple once you understand optional binding and the do/try/catch syntax in Swift 2. So many people seem to want grab a third-party library to parse JSON, but for most use cases, I see very little benefit to using a third-party library to do something that’s so simple. You don’t need a third-party library for everything.

You can get the playground on GitHub.