Working with JSON in Swift
Time to read: 4 minutes
Often developers need to deal with data in various different formats and JSON, short for JavaScript Object Notation, is one of the most popular choices. This is the syntax that the JavaScript language uses to denote objects.
For this post, we are going to use the following modified JSON data from NASA's Astronomy Picture of the Day API to cover some basic examples of scenarios where you'd need to parse JSON. You will need to be running Swift 4.0 or greater. The following JSON is the example that we are going to be working with:
Let's gear up to parse some JSON!
Codable in Swift 4
For a long time, working with JSON in Swift was thought of as a relatively painful and annoying experience. But with the release of Swift 4 and the Codable protocol, which combines the functionality of Encodable and Decodable, we now have an out-of-the-box solution and don't have to use external JSON libraries.
For this example, we'll create a Codable
struct that represents the data we would get back from the Astronomy Picture of the Day API and try to decode the JSON data using this struct. Create a file called test.swift
and paste the following code into it:
Notice that this code is using a struct that contains all of the keys from our JSON example. We are also using a try!
statement just to show how this example works, but in any production-level code, you will want to catch whatever possible errors could arise.
In your terminal, run your code with the following command from the directory test.swift
is in:
You should see the explanation of the Astronomy Picture of the Day print to the terminal.
One of the upsides about using a Codable
struct is that you don't have to install any third-party libraries, allowing you to require minimal dependencies.
SwiftyJSON
Before Swift 4, SwiftyJSON was one of the most popular libraries for working with JSON. The developers of SwiftyJSON argue that since JSON data is implicit about types by nature, that it is very well-suited to dealing with this data in general. SwiftyJSON takes care of optional-wrapping by default and decodes your JSON data into a Dictionary, behaving very similarly to JSON libraries in other popular programming languages such as request in Python.
Because SwiftyJSON is a third-party library, we will install it using Swift Package Manager. Open your terminal, navigate to where you want this code to live, and run the following command to initiate a Swift project to use Swift Package Manager:
This will generate a Swift project with a directory structure for a command-line executable. To add SwiftyJSON to this project, open up the Package.swift
file that was generated and add the following line to the dependencies
array:
Now add the string "SwiftyJSON"
to the dependencies
array in the targets
section to make sure that your code will be able to use this library.
With the dependencies taken care of, open the Sources/project/main.swift
file (where "project" in this case will be replaced by whatever your project's name is, defaulting to the name of the directory it is contained in) and replace the code in it, which just has a "Hello World" print statement, with the following:
This code uses SwiftyJSON to parse the JSON in our jsonString
and decodes it into a dictionary. After this, we're printing the explanation of the Astronomy Picture of the Day, just as we did in the last example.
Run this with the following command in the Sources/project directory to see ______:
Using Codable
structs is great when the data you're working with has a consistent structure that you know about ahead of time, but SwiftyJSON works well in more general cases where you aren't as sure about the structure of the data you're dealing with.
Alamofire
Dealing with hard-coded JSON strings is fine and all, but often in the real world there is more context around why you have to deal with JSON data. One of the most common scenarios that requires decoding JSON would be when making HTTP requests to third-party REST APIs.
The Alamofire library is a popular tool for making HTTP requests, and it has a built-in method for parsing JSON when you receive an HTTP response. It's great to have a quick solution built into your networking library.
As we did with SwiftyJSON, add Alamofire to the dependencies in Package.swift
, and don't forget to add "Alamofire" to your targets
as well:
In this example, we are actually going to make an HTTP request to the Astronomy Picture of the Day API rather than using the hard-coded .json
string from the other examples.
Replace the code in your main.swift
with the following:
This code makes an HTTP GET
request to NASA's API, then parses and prints out the JSON data returned from it. From here, you can use the data however you want.
Run your code with the following command:
The output should look something like this:
Conclusion
There are different solutions to working with JSON in Swift, and you may want to use these in different contexts, or even combine them. In this post I've shown you the basics of just a few of the more popular examples.
You can also check out this post to learn how to make HTTP requests in Swift.
Feel free to reach out for any questions or to show off what you build:
- Email: Sagnew@twilio.com
- Twitter: @Sagnewshreds
- GitHub: Sagnew
- Twitch (streaming live code): Sagnewshreds
Related Posts
Related Resources
Twilio Docs
From APIs to SDKs to sample apps
API reference documentation, SDKs, helper libraries, quickstarts, and tutorials for your language and platform.
Resource Center
The latest ebooks, industry reports, and webinars
Learn from customer engagement experts to improve your own communication.
Ahoy
Twilio's developer community hub
Best practices, code samples, and inspiration to build communications and digital engagement experiences.