Parsing JSON in Swift The Cheat Sheet
Parsing JSON in Swift The Cheat Sheet
Page 1 of 36
Parsing JSON in Swift: The Ultimate Cheat Sheet
A Small Promise 3
Basic JSON Parsing in Swift 4
JSON parsing with Decodable types 4
Page 2 of 36
Parsing JSON in Swift: The Ultimate Cheat Sheet
A Small Promise
It took me weeks of research and many hours of work to create this guide. But I
am happy to offer it to you completely free of charge.
If you think I did a good job, please share this with someone who can benefit
using this link:
https://matteomanferdini.com/parsing-json-in-swift-cheat-sheet/
That way, I can spend less time looking for people I can help and more
producing great free material.
Let’s start.
Page 3 of 36
Parsing JSON in Swift: The Ultimate Cheat Sheet
import Foundation
Further reading: Parsing JSON in Swift: The Complete Guide [With Examples]
Page 4 of 36
Parsing JSON in Swift: The Ultimate Cheat Sheet
import Foundation
Page 5 of 36
Parsing JSON in Swift: The Ultimate Cheat Sheet
import Foundation
Further reading: Parse JSON in Swift without Codable [Arrays and Dictionaries]
Page 6 of 36
Parsing JSON in Swift: The Ultimate Cheat Sheet
import Foundation
Page 7 of 36
Parsing JSON in Swift: The Ultimate Cheat Sheet
import Foundation
Page 8 of 36
Parsing JSON in Swift: The Ultimate Cheat Sheet
import Foundation
Page 9 of 36
fi
Parsing JSON in Swift: The Ultimate Cheat Sheet
import Foundation
Page 10 of 36
Parsing JSON in Swift: The Ultimate Cheat Sheet
import Foundation
Page 11 of 36
Parsing JSON in Swift: The Ultimate Cheat Sheet
Further reading: Coding Keys in Swift with Decodable: How and When to Use
Them
Page 12 of 36
Parsing JSON in Swift: The Ultimate Cheat Sheet
import Foundation
Page 13 of 36
Parsing JSON in Swift: The Ultimate Cheat Sheet
import Foundation
Page 14 of 36
Parsing JSON in Swift: The Ultimate Cheat Sheet
import Foundation
Page 15 of 36
Parsing JSON in Swift: The Ultimate Cheat Sheet
import Foundation
Page 16 of 36
Parsing JSON in Swift: The Ultimate Cheat Sheet
If dates are all represented using the same data type, e.g., String, first
decode the date value and then try multiple date formatters in sequence until
the conversion succeeds.
import Foundation
Page 17 of 36
Parsing JSON in Swift: The Ultimate Cheat Sheet
if codingKey.stringValue == MyStruct.CodingKeys.ISODate.stringValue {
let dateString = try container.decode(String.self)
let formatter = ISO8601DateFormatter()
formatter.formatOptions = [.withInternetDateTime]
return formatter.date(from: dateString)!
} else {
let UNIXTime = try container.decode(TimeInterval.self)
return Date(timeIntervalSince1970: UNIXTime)
}
})
Page 18 of 36
Parsing JSON in Swift: The Ultimate Cheat Sheet
import Foundation
Page 19 of 36
Parsing JSON in Swift: The Ultimate Cheat Sheet
Page 20 of 36
Parsing JSON in Swift: The Ultimate Cheat Sheet
import Foundation
Page 21 of 36
Parsing JSON in Swift: The Ultimate Cheat Sheet
Page 22 of 36
Parsing JSON in Swift: The Ultimate Cheat Sheet
The parent object can instead be decoded as a dictionary with String or Int
keys, depending on the kind of keys the JSON data uses.
You can then loop through the dictionary and transform its content into an
array.
import Foundation
Page 23 of 36
Parsing JSON in Swift: The Ultimate Cheat Sheet
Then, iterate through the container’s allKeys property and use the keys to
decode each nested JSON object.
import Foundation
Page 24 of 36
Parsing JSON in Swift: The Ultimate Cheat Sheet
init?(stringValue: String) {
self.stringValue = stringValue
}
init?(intValue: Int) {
return nil
}
}
Page 25 of 36
Parsing JSON in Swift: The Ultimate Cheat Sheet
Further reading: Decode JSON with Dynamic Keys in Swift [Dictionaries and
Arrays]
Page 26 of 36
Parsing JSON in Swift: The Ultimate Cheat Sheet
Then, determine the dynamic type of the nested objects and decode them in a
decoding initializer in the Swift type for the parent JSON object.
import Foundation
Page 27 of 36
Parsing JSON in Swift: The Ultimate Cheat Sheet
Page 28 of 36
Parsing JSON in Swift: The Ultimate Cheat Sheet
default:
throw DecodingError.dataCorruptedError(
forKey: .value,
in: postContainer,
debugDescription: "Unknown post type"
)
}
posts.append(post)
}
self.posts = posts
}
}
Further reading: Decoding JSON with Dynamic Types in Swift [With Codable]
Page 29 of 36
Parsing JSON in Swift: The Ultimate Cheat Sheet
import Foundation
Page 30 of 36
Parsing JSON in Swift: The Ultimate Cheat Sheet
With JSONSerialization
import Foundation
Further reading: Swift Data to JSON String: Formatting and Pretty Printing
Page 31 of 36
Parsing JSON in Swift: The Ultimate Cheat Sheet
import Foundation
Page 32 of 36
fi
Parsing JSON in Swift: The Ultimate Cheat Sheet
Reading and writing a JSON file in the app’s document directory
extension User {
static let documentsURL = URL.documentsDirectory
.appendingPathComponent("User")
.appendingPathExtension("json")
}
Page 33 of 36
Parsing JSON in Swift: The Ultimate Cheat Sheet
Page 34 of 36
Parsing JSON in Swift: The Ultimate Cheat Sheet
Further reading: Parse JSON from an API URL in Swift [Codable and
URLSession]
Page 35 of 36
Parsing JSON in Swift: The Ultimate Cheat Sheet
I hope you enjoyed this cheat sheet and that it helped you improve your
understanding of decoding JSON data in Swift. It took me several hours to put
it together, but I am happy to offer it to you free of charge.
If you found it useful, please take a moment to share it with someone who
might also find it useful. This way, I can dedicate more time to creating more
free articles and guides to help you in your iOS development journey.
Think about colleagues and friends who could find this guide useful and send
them this link through email or private message, so that they can get the cheat
sheet together with all the other material I only share with my email list:
https://matteomanferdini.com/parsing-json-in-swift-cheat-sheet/
You can also share this link on your favorite social network.
Thank you.
Matteo
Page 36 of 36