✕
Forbes India Leader Recognition
e.preventDefault(); // Prevent form submission input.blur(); // remove focus input.value = ""; // optional: clear the input

Easy JSON parsing with Codable

Easy JSON parsing with Codable

Featured Image

Tudip

Swift codable protocol has a combination of Encodable and Decodable protocol that can be used to work with JSON data in both directions. In short, Swift Codable protocol has offered the following features:

  • Using Codable, by writing very few lines of code, we can model JSONObject or PropertyList file into equivalent struct or classes. For the properties in the objects we don’t have to write the constructor. Codable hands it all. We simply need to extend our model to conform to the protocol for coding, decoding or encoding.
  • Swift compiler handled the mismatch between the strong data types of Swift and lose data types of JSON internally. We can now manage types of Swift Data such as Date, URL, Float, etc. Complex JSON can be easily modelled using readability Nesting Structs.
  • Complex JSON can be easily modelled using readability Nesting Structs.
  • Use JSONDecoder to parse actual JSON to become one-liner.

Now Let’s Get Into The Fun Part of coding

There is an API for displaying Zomato cities, and the endpoint is for Pune:

If you want to try you can try that out on link.
So, the response for this endpoint will look like this:

This is a lot of information, but we’re only going to use the following properties for the demo.

  • location_suggestions
  • status
  • id
  • name
  • country_flag_url
  • country_name

Using this simple Swift Struct, we can easily create a model for this information.

I can probably go ahead and write a builder for every property and so on. I will stop at this stage, however, as I have already noticed the problems.

  • The constant country_flag_url is of type URL, but in JSON it is String.
  • Note also that constant is declared as a camel case that is not a standard convention for Swift.

Luckily, Codable has a response to both issues.

  • We need to make our Struct conform to the Data Type Mismatch coding protocol. It will be taken care of by the Swift compiler under the hood. We also don’t need to write the builder.
  • We can declare Coding Keys enum to solve camel case problem and tell JSON to use snake case for Swift constant and camel case.

This will look like the resulting structure:

We have now achieved snake casing for Swift and we also have Swift types in our model by complying with the Codable protocol.
Now we’ve got our JSON endpoints and model based on the JSON, let’s see how easy this JSON is to parse. First, we will apply to the endpoint and record the JSON and use JSONDecoder to parse the JSON with a single line of code.

This is it! We’ve parsed JSON with one or several lines of code. We can now use the ZomatoCities object to print all the properties.

For more details please refer the following links:

Related Posts

Latest Posts

  • All Posts
  • AI Powered Knowledge
  • ai/ml
  • CEO India Magazine
  • CMMI level 5 Certification
  • e-learning
  • Fintech
  • gaming
  • Generative AI
  • healthcare
  • manufacturing
  • News
  • OTT
  • Portfolio
  • supply chain
  • travel and hospitality
  • Tudip's AI Hackathon
  • Voxlearn Enterprises
    •   Back
    • Android
    • iOS
    • Java
    • PHP
    • MEAN
    • Ruby
    • DotNet
    • IoT
    • Cloud
    • Testing
    • Roku
    • CMS
    • Python
Artificial Intelligence in Healthcare

Artificial Intelligence in Healthcare

September 23, 2026

Explore how artificial intelligence in healthcare is transforming clinical decision-making, and patient care .

Read More
AI in Manufacturing: Machine Intelligence  Meets Industrial Innovation

AI in Manufacturing: Machine Intelligence Meets Industrial Innovation

September 23, 2026

Explore how AI is transforming manufacturing through predictive maintenance, intelligent quality control, data-driven operations.

Read More
Artificial Intelligence in Manufacturing: Enabling Intelligent, Flexible and Cost-Effective Production Through AI

Artificial Intelligence in Manufacturing: Enabling Intelligent, Flexible and Cost-Effective Production Through AI

September 23, 2026

Discover how AI is transforming manufacturing through intelligent automation.

Read More

India

Plot No. 11/2, Phase 3, Hinjewadi Rajiv Gandhi Infotech Park, Pune, India – 411057.
info@tudip.com
+91-96-8990-0537

United States

1999 S. Bascom Ave Suite 700, Campbell CA. 95008, USA.
info@tudip.com
+1-408-216-8162

Canada

64 Caracas Road North York, Toronto Ontario M2K 1B1, Canada.
info@tudip.com

Mexico

Calle Amado Nervo #785 Interior B Colonia Ladron De Guevara 44600 Guadalajara, Jalisco, Mexico.
info@tudip.com

Singapore

77 High Street, #10-12B High Street Plaza, Singapore 179433.
info@tudip.com

Colombia

Cra. 9 # 113-53 Of. 1405 Bogotá D.C., Colombia.
info@tudip.com

UAE

Tudip Information Technologies L.L.C Office No 109, ABU HAIL BUILDING 13, Abu Hail, Dubai, UAE.
info@tudip.com

Nigeria

22 Kumasi Crescent, Wuse 2, Abuja, Nigeria.
info@tudip.com