Simple and clean way to convert JSON string to Object in Swift
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
In modern Swift, the clean answer to "convert a JSON string to an object" is usually Decodable plus JSONDecoder. That approach is concise, type-safe, and easier to maintain than building objects manually from dictionaries returned by JSONSerialization.
Decode into a typed model with Codable
The usual pattern is:
- turn the JSON string into
Data - define a model that matches the JSON structure
- decode with
JSONDecoder
Here is a complete example.
This is the simplest and cleanest solution for most app code. The result is a real Swift type, so the compiler can help you catch mistakes early.
Nested JSON maps naturally to nested Swift types
If the JSON contains objects inside objects, mirror that structure in your model definitions.
That is another reason Decodable is preferred. The model definitions stay close to the shape of the incoming data instead of forcing you to navigate loosely typed dictionaries everywhere.
Use CodingKeys when JSON names do not match Swift names
Real payloads often use snake case or field names that do not match your preferred Swift naming. You can map them explicitly with CodingKeys.
If your entire payload consistently uses snake case, JSONDecoder can also convert keys automatically.
That removes a lot of boilerplate when the naming mismatch is systematic.
Build a reusable helper when you decode often
If the app decodes many JSON strings, wrap the pattern in a generic helper. This keeps calling code small without hiding the important failure points.
A helper like this is fine as long as it stays thin. The actual decoding should still be done by JSONDecoder, not by custom reflection or manual key walking.
When JSONSerialization still makes sense
If the JSON shape is truly dynamic and cannot be represented cleanly as models, JSONSerialization may still be appropriate.
That returns Any, usually backed by Dictionary<String, Any> or Array<Any>. It is flexible, but you lose much of Swift's type safety. For application models, Decodable is usually the better default.
Common Pitfalls
A common mistake is trying to decode a JSON string directly without converting it to Data. JSONDecoder works with bytes, not with String.
Another mistake is defining model property types that do not match the payload. If the JSON field can be missing or null, the corresponding Swift property may need to be optional.
A third mistake is reaching for JSONSerialization first. That usually leads to long chains of casts and runtime errors that Decodable would have avoided.
Summary
- In Swift, the cleanest path from JSON string to object is usually
DecodablewithJSONDecoder. - Convert the string to
Data, then decode into a typed model. - Use nested models and
CodingKeysto match the payload cleanly. - Use
keyDecodingStrategywhen the API consistently uses snake case. - Keep
JSONSerializationfor truly dynamic JSON, not as the default for app models.
Related reading
- Simple and clean way to convert JSON string to Object in Swift
- Simple Android grid example using RecyclerView with GridLayoutManager like the old GridView
- Simple Android RecyclerView example
- Simplest way to throw an error/exception with a custom message in Swift?
- Simplest way to throw an error/exception with a custom message in Swift?
- Simulate force touch / 3D touch on iPhone 6S or iPhone 6S Plus simulators
- Simulate low network connectivity for Android
- Simulator error FBSSystemServiceDomain code 4
.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.