JSON Parsing with SwiftyJSON

Samrith Yoeun
3 min readJan 14, 2020

JSON parsing is not something new, and as one of those developers, I have worked with JSON Parsing every day.

In Swift, there are so many ways to parse the JSON including Codable protocol, JSONSerialization class, or using SwiftyJSON.

Personally, I prefer using SwiftyJSON because this library helps me to archive:

  • more Human Readable JSON Parsing code writing
  • less Optional condition checking when working with JSON
  • more crash-free code thanks to its default value initialization feature

You can start using SwiftyJSON in your swift project easily.

Installation

Using Cocoapod

go to your project podfile and add the following line, then pod install your project.

pod 'SwiftyJSON', '~> 4.0'

Embedded source code to your project

If you are new to Cocoapod or concern about whether adding a new pod to your project can increase the size of your app, you can download this sourceCode, and put it anywhere in your project.

BUT, I highly recommend you to support the creator by using Cocoapod.

Using SwiftyJSON

SwiftyJSON is quite easy to use, and it is straight forward too. For example, you can parse this JSON object, with this straight forward code.

//json oject
{
"name": "Sam Yoeun",
"age": 23,
"height": 1.70
}
// swift code
let name = json["name"].stringValue
let age = json["age"].intValue
let height = json["height"].doubleValue

See? It is quite easy to use and my above code is so Human Readable.

SwiftyJSON also provides the Subscript mechanism that let the developers parse their JSON object at ease.

//json 
{
"vehicle": ["Bike", "Motorbike", "Car", "Plane"],
"owner": {
"name": "Sam Yoeun",
"age": 23
}
}
//swift
let bike = json["vehicle"][0].stringValue
let motorbike = json["vehicle"][1].stringValue
let ownerName = json["owner"]["name"].stringValue
let ownerAge = json["owner"]["age"].intValue

Where should I parse JSON?

I hope the above code can help you understand how to use the SwiftyJSON, but from my experience, I would suggest you create an object that can be inited with JSON.

Let’s say you have a JSON object Owner, here is my example of my Swift Object that will take JSON and init their properties.

//json 
"owner": {
"name": "Sam Yoeun",
"age": 23,
"height": 1.7
}
//swift
struct Owner {
var name = ""
var age = 0
var height = 0.0
init(_ json: JSON = "") {
name = json["name"].stringValue
age = json["age"].intValue
height = json["height"].doubleValue
}
}//switft - so when you get your json response you can do this:
let json = JSON(yourJsonResponse)
let owner = Owner(json)

If you think, this approach is reasonable, and want to start implementing this one approach in your next swift project. I highly recommend using this Modified of JSONExport to help you.

Conclusion

I hope you now can parse your Json with less pain, and happy to read my article. As the growth in using JSON in the web service, I can see that a lot of developers have struggled to parse the data, and I really appreciate the effort of the SwiftyJSON team that created this awesome library to save our days.

--

--