Reading in a JSON File Using Swift
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Reading a JSON file in Swift is usually a two-step task: load the file's bytes as Data, then decode that data into either a strongly typed model or a dynamic dictionary-like structure. For most app code, Codable plus JSONDecoder is the right default because it gives you compile-time structure and better errors.
The important choice is not "can Swift parse JSON." It is whether the JSON schema is known ahead of time. If it is, use models. If it is highly dynamic, fall back to JSONSerialization.
Load the File Data First
If the JSON file is bundled with the app, start by locating it in the bundle and reading it into memory:
This works well for sample content, bundled configuration, and test fixtures that ship inside the app target.
If the file lives elsewhere, such as the documents directory, the decode step stays the same and only the URL changes.
Decode Into a Codable Model
For known JSON structure, define a type that matches the file and decode directly:
This is the safest and most maintainable approach because the compiler checks the expected shape. If the JSON changes, decoding errors usually point straight to the mismatch.
Decode Arrays and Nested Objects
Codable scales well when the file contains arrays or nested models:
Once your model structure mirrors the JSON structure, Swift handles the traversal for you.
Map JSON Names to Swift Names
JSON keys do not always match Swift naming style. Use CodingKeys when the wire format uses names such as full_name but your Swift model should use fullName.
That keeps your Swift code idiomatic without forcing you to rename keys in the source file.
Use JSONSerialization for Dynamic Payloads
If the schema is not known in advance, JSONSerialization can parse the file into dictionaries and arrays:
This is flexible, but it is also less safe. You lose type checking and have to cast values manually. Use it only when the payload is truly dynamic.
Handle Errors Usefully
Do not collapse every failure into a generic message. Swift decoding errors are often specific enough to tell you what went wrong.
That makes bad fixtures and schema mismatches much easier to diagnose than a flat "JSON parsing failed."
Common Pitfalls
The most common mistake is forgetting to include the file in the app target. The JSON file may appear in Xcode, but if it is not part of the bundle, Bundle.main.url will still return nil.
Another common issue is reaching for JSONSerialization by default when the structure is already known. That throws away type safety and makes later code harder to maintain.
Developers also often ignore decoding errors and then debug the wrong layer. If a model field is named incorrectly or typed incorrectly, the decoder is usually already telling you exactly what failed.
Finally, do not assume a bundled JSON file and a documents-directory JSON file should be loaded the same way. The decoding logic is the same, but the file lookup path is not.
Summary
- Reading JSON in Swift means loading
Datafirst and decoding second. - Use
Bundle.main.urlfor JSON files that ship with the app. - Prefer
CodableandJSONDecoderwhen the JSON structure is known. - Use
CodingKeyswhen JSON names and Swift property names differ. - Reserve
JSONSerializationfor genuinely dynamic payloads.
Related reading
- Reading NFC Tags with iPhone 6 / iOS 8
- Read/Write String from/to a File in Android
- Read/Write String from/to a File in Android
- Realm object has been deleted or invalidated
- RealmSwift Convert Results to Swift Array
- RealmSwift Convert Results to Swift Array
- Rearranging Tab Bar Controller Order in StoryBoard
- Receive result from DialogFragment
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.