Parsing JSON in Swift 3

Josh Brown
0 ratings

"Swift is supposed to be easy to learn and use. So why am I stuck trying to get a simple JSON parser to work?"

You start your day all fired up to get your app running with live data from your JSON API, but before you know it the day is over and you’ve barely made any progress. Your Xcode console displays just one line of output: a single string you managed to pull out of the JSON that serves as an indicator of how little you accomplished today. Rather than making you more productive, Swift is preventing you from getting anything done. In front of you lies a mess of code that’s littered with nested conditionals, and your app still crashes when you try to pull out other values. And you haven’t even started the part where you deserialize the JSON into model objects you can actually use in your app.

Does it really take this much work to parse a simple JSON object, or are you doing it wrong?

What if you could run your app with JSON from the API today?

Imagine this: you start your day all fired up to get your app running with live data from your JSON API. Before you know it, you’ve deserialized your JSON into model objects you can use in your app. Xcode shows a suite of passing tests: a clear indication of all the progress you’ve made. You’re proud of the work you’ve done and confident your app can handle any JSON the API returns. As you head to lunch, you can’t help but smile as you think about how much you got done this morning.

Deserializing JSON in Swift can make you feel like you’re wrestling the compiler, but you don’t have to let it beat you.

Great news: you can build a simple JSON parser in Swift without sinking the next three days into it

Parsing JSON in Swift will teach you to harness the power of Swift and give you confidence that your app can gracefully handle any JSON that comes its way.

You'll learn:

  • How to use NSJSONSerialization to transform JSON into Swift types, and why you should avoid using AnyObject whenever you can
  • The dangers of forced type casting
  • Simple strategies you can use to make your JSON deserialization more concise, readable, and elegant
  • Whether to return nil, empty, or throw an error when transforming JSON into an array of model objects
  • How to write unit tests to increase your confidence in your parser's correctness

You'll go from fighting an uphill battle against an uncooperative compiler to having a JSON parser that's proven to be stable with automated tests. Here's what's in the book:

Convert JSON into Swift types with NSJSONSerialization

  • Deserializing JSON into Swift types
  • The dangers of forced type casting
  • Where should all this JSON code go?
  • Handling errors with do-try-catch

Increase your confidence with automated tests

  • Why test?
  • Creating a JSON test file
  • Creating a JSON test case
  • Dealing with test failures
  • Increasing confidence with test coverage
  • Avoiding false confidence with strong assertions
  • Testing the error path
  • Finding the point of diminishing returns

Achieve conciseness, readability, and elegance with these simple principles

  • Proper typing leads to less clutter
  • Use typealias to increase clarity and readability

Transform Swift types into model objects for convenience and stability

  • Why model objects?
  • Testing model objects
  • Creating a model object
  • Transforming dictionaries into model objects
  • Producing specific error messages to clarify where parsing failed
  • Handling arrays and nil

If you follow the patterns and principles in Parsing JSON in Swift, you'll be confident that your app can gracefully handle any JSON your API might return.

Is Parsing JSON in Swift right for me?

If you can fetch JSON data from your API but you’re frustrated with trying to parse it, this book is for you. It’ll show you how to transform your JSON into model objects you can use in your app – and it’ll give you confidence that your parser can handle both expected and unexpected data.

If you’re still trying to figure out how to make HTTP requests and get data from your API, you won’t find that here. There are already plenty of tutorials out there to teach you networking in Swift – I’d recommend searching to find one that can help.

But maybe after getting the data from your API, you’re frustrated with the verbose, tangled mess of code that seems to be required to convert JSON into model objects. If you feel like there must be a better way to parse JSON in Swift, this book is written for you.

If you’re still not sure, email with any questions and I’d be happy to help.

What are people saying?

“I have been running around my office like a wild woman, grabbing people by the lapels and shouting typealias JSONDictionary = [String: AnyObject] like Scrooge on Christmas morning. It has changed my life for the better. I may get a tattoo.” — Anne Cahalan

"Totally digging Parsing JSON in Swift! Thanks for the knowledge-bomb!" — Nick Kohrn

What version of Swift is this written for?

The code in Parsing JSON in Swift is written entirely in Swift 3.

What format does the book come in?

You can download Parsing JSON in Swift as a PDF.

What if I'm not happy with the book?

I want you to be happy with it, so if for some reason you're not, just send me an email and I'll give you a refund as soon as I can.

Who's the author?

Hi, I'm Josh Brown. I've been developing for iOS since 2009 and teaching it for just as long. These days, I get to help people learn iOS every day through my workshops, video courses, this book, and my blog. I love seeing what people learn and do with their new knowledge and skills, so if you use anything I've written or recorded to build something cool, I'd love to hear from you. Just send me an email or talk to me on Twitter.

In addition to teaching, I also enjoy building apps, which is how I got into iOS in the first place. I was tired of building software I couldn't really show or explain to my friends and family. I love that now I can pull my iPhone out and show them what I built - or at least point them to the App Store and say "I made that."

I have another question.

Send it my way and I'd be happy to answer it.

This product is not currently for sale.
  • Size
    749 KB
  • Length
    74 pages
  • Size749 KB
  • Length74 pages
Powered by


Parsing JSON in Swift 3

Enter your info to complete your purchase


You'll be charged US$29.