How to read JSON using Swift 2.0

Hey everyone! The last swift video seemed to answer questions for many of you, but is also sparked more questions. When we were posting JSON to our endpoint, I kinda glossed over how we read JSON using Swift, so in this tutorial I figured we’d focus just on reading JSON.

This question was posted in the comments for the last tutorial, and I figured it would be a great place to start. Thanks Techie Stewie Stuff for the question. Basically Techie is asking how to navigate the arrays and dictionaries when you load JSON. And thanks to Techie, we’ll work with the JSON from the original question.

I’m putting the JSON into a file in the example code, but the real starting point for reading JSON is NSData. We’ll start with NSData if it’s coming from a URL as well.

JSON code can have three possibilities. We might get back an array of items, we might get back key-value pairs, or we might get a value. Things between square braces are arrays, and curly braces are key-value pairs. When we read JSON in Swift, we need to account for all possibilities. An array gets loaded into an NSArray, and key-value pairs go into an NSDictionary. Values can go into a number, a string, a boolean, or it can be null. Arrays can contain objects, and objects can have values that are arrays. Or arrays of arrays, or values that are objects. The JSON can get complex quickly.

In our example JSON, our root has a single key value pair and it starts with a curly brace. That makes it a dictionary. The root is an implied key, and the body of JSON document is the value.

We can read this all into one dictionary with the following line of code. This parses the JSON into an NSDictionary. If we didn’t know the structure in advance, we’d need to check if our returned JSON object was an array or dictionary. We know it’s a dictionary, so we’ll just read it as such.

This gives us a dictionary with a single key-value pair. The key is list, and everything after is the value. So let’s get the dictionary the key “list” points to.

We’ll create another variable that gets the value for the key “list”. We’ll cast the result as a dictionary. Again, if we didn’t know in advance what we were reading, we’d need to check to see if the value is a dictionary, array, or string value. We know, so we’ll hard code it as a dictionary.

Here’s where things get a bit interesting. Inside the list element, we have key value pairs where some of the objects are values, and one is an array. To read the total value, we dereference the value using the key. The values are all optionals, so we’ll get the value using the ! operator.

Getting the item element will return an array. We can get the array the same way we got the total value. In this case, we’ll cast the result to an NSArray. Once we have the array, we can loop and print each one. Remember each item will always be an optional. This code assumes there will always be a value in the optional, but if we’re writing code where we don’t know if there is a value, we’ll need to account for it.

So when we’re reading JSON, we can use the NSJSONSerialization object to read the document. From there we pull out dictionaries, arrays and values. The only trick is knowing what object you are getting, and then unpacking optionals when you get to the values.

Thanks for watching! If you have any more questions let me know in the comments. New videos out every week, so make sure you subscribe. Liking the videos let’s me know what I’m doing right, so if the tutorial was helpful for you, let me know!

And with that, I’ll see you in the next tutorial!

Great videos. Better emails.

Have the latest videos emailed to you every month! This way you’ll never miss an important video.

Don't miss another video!

New videos come out every week. Make sure you subscribe!

Comments

comments

DJ Spiess

Your personal instructor

My name is DJ Spiess and I’m a developer with a Masters degree in Computer Science working in Colorado, USA. I primarily work with Java server applications. I started programming as a kid in the 1980s, and I’ve programmed professionally since 1996. My main focus are REST APIs, large-scale data, and mobile development. The last six years I’ve worked on large National Science Foundation projects. You can read more about my development experience on my LinkedIn account.