Working with JSON in Swift: Parsing and Serialization for Beginners

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Working with JSON in Swift: Parsing and Serialization for Beginners

\n\n

JSON (JavaScript Object Notation) is the most popular data exchange format in modern mobile and web applications. If you are developing for iOS or macOS in Swift, the ability to parse and serialize JSON is a mandatory skill. In this article, we will cover all the main ways to work with JSON: from the classic JSONSerialization to the modern Codable protocol. The material is aimed at beginner developers who are already familiar with the basics of Swift.

\n\n

What is JSON and why is it needed in Swift?

\n\n

JSON is a text format based on JavaScript syntax. It consists of key-value pairs, nested objects, and arrays. In iOS development, JSON is used for:

\n\n
    \n
  • Retrieving data from REST API servers
  • \n
  • Saving application configurations and settings
  • \n
  • Exchanging data between application modules
  • \n
  • Working with databases (e.g., Firebase or CoreData + JSON)
  • \n
\n\n

In Swift, there are two main approaches to working with JSON: the old way using JSONSerialization (from Foundation) and the modern way using the Codable, Encodable, and Decodable protocols. We will start with the simpler JSONSerialization to understand the mechanics, and then move on to the powerful Codable.

\n\n

Parsing JSON with JSONSerialization (Foundation)

\n\n

JSONSerialization is a built-in Foundation class that converts JSON data into Swift objects (dictionaries and arrays). This method has existed since the early versions of Swift and is still used in legacy projects. Let's look at an example.

\n\n

Suppose we have a JSON string with user information:

\n\n
let jsonString = """\n{\n    "name": "Ivan",\n    "age": 30,\n    "email": "ivan@example.com",\n    "isActive": true,\n    "hobbies": ["reading", "programming", "sports"]\n}\n"""
\n\n

To parse this JSON, we first convert the string to Data, and then use JSONSerialization:

\n\n
import Foundation\n\nif let jsonData = jsonString.data(using: .utf8) {\n    do {\n        if let dictionary = try JSONSerialization.jsonObject(with: jsonData, options: []) as? [String: Any] {\n            print("Name: \\(dictionary["name"] ?? "No data")")\n            print("Age: \\(dictionary["age"] ?? "No data")")\n            if let hobbies = dictionary["hobbies"] as? [String] {\n                print("Hobbies: \\(hobbies.joined(separator: ", "))")\n            }\n        }\n    } catch {\n        print("JSON parsing error: \\(error.localizedDescription)")\n    }\n}
\n\n

Important points:

\n
    \n
  • Always use try-catch — parsing can throw an error.
  • \n
  • The result comes as Any, so you need to cast it to a specific type ([String: Any] for an object, [Any] for an array).
  • \n
  • The options .mutableContainers and .fragmentsAllowed allow you to control parsing flexibility.
  • \n
\n\n

Modern approach: the Codable protocol

\n\n

Starting with Swift 4, Apple introduced the Codable protocol (combining Encodable and Decodable). This is real "magic" — you just declare a structure, and Swift knows how to parse and serialize data on its own. Let's rewrite the previous example using Codable.

\n\n

1. Declaring the data model

\n\n
struct User: Codable {\n    let name: String\n    let age: Int\n    let email: String\n    let isActive: Bool\n    let hobbies: [String]\n}
\n\n

Note: the field names in the structure must match the JSON keys (case-sensitive). If the keys differ (e.g., snake_case), use CodingKeys.

\n\n

2. Parsing J

Blogs

Book Recommendations