<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/decoding-and-flattening-nested-json-with-codable-8fwfbviti" -->

---
title: Decoding and Flattening Nested JSON with Codable | daily.dev
description: Learn how to decode and flatten nested JSON objects in Swift using the Codable protocol and JSONDecoder class. Codable simplifies the process of mapping JSON...
canonical: https://daily.dev/posts/decoding-and-flattening-nested-json-with-codable-8fwfbviti
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: Decoding and Flattening Nested JSON with Codable | daily.dev
og:description: Learn how to decode and flatten nested JSON objects in Swift using the Codable protocol and JSONDecoder class. Codable simplifies the process of mapping JSON...
og:url: https://daily.dev/posts/decoding-and-flattening-nested-json-with-codable-8fwfbviti
og:image: https://api.daily.dev/og/posts/8fwfBvITi.png
og:image:alt: Decoding and Flattening Nested JSON with Codable
og:image:width: 1200
og:image:height: 630
og:locale: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Decoding and Flattening Nested JSON with Codable

**[Collections](https://daily.dev/sources/collections)** · 2 min read · 1 upvotes · 0 comments

## Summary

Learn how to decode and flatten nested JSON objects in Swift using the Codable protocol and JSONDecoder class. Codable simplifies the process of mapping JSON data to Swift models, and custom coding keys can be used to extract specific data from nested objects.

## Content

## Decoding and Flattening Nested JSON with Codable

In this article, we will explore how to decode and flatten nested JSON objects in Swift using the Codable protocol and JSONDecoder class. JSON, or JavaScript Object Notation, is a popular data interchange format used in web services. When working with nested JSON, it can be challenging to extract the required data. However, with Codable, the process becomes much simpler.

What is Codable?

The Codable protocol in Swift provides a convenient way to decode and encode data types to and from JSON. It allows us to map JSON data to our Swift models and vice versa.

Decoding Nested JSON

To decode nested JSON, we can create a structure that conforms to the Codable protocol. The structure should have properties that match the keys in the JSON. Codable does a great job of automatically mapping the JSON data to our Swift models, including nested objects.

Flattening Nested JSON

However, sometimes we may want to flatten the nested JSON and extract specific data. For example, if the JSON response contains a nested object, we may only be interested in a few properties from that object. In such cases, we can implement custom coding keys to selectively decode the required data.

In conclusion, decoding and flattening nested JSON in Swift is made easier with the Codable protocol and JSONDecoder class. By conforming to Codable and using custom coding keys, we can seamlessly map JSON data to our Swift models and extract specific data from nested objects.

## Similar posts on daily.dev

- [Don’t just attend KubeCon \+ CloudNativeCon, Merge Forward your experience\!](https://daily.dev/posts/don-t-just-attend-kubecon-cloudnativecon-merge-forward-your-experience--l0rpp73x8) · CNCF · 1 upvotes · 0 comments
- [Announcing H2 2026 KCDs](https://daily.dev/posts/announcing-h2-2026-kcds-m96goajm1) · CNCF · 1 upvotes · 0 comments
- [Two months of Open Community Groups](https://daily.dev/posts/two-months-of-open-community-groups-asf52zhbs) · CNCF · 0 upvotes · 0 comments
- [CNCF Unveils Schedule for KubeCon \+ CloudNativeCon Europe 2026](https://daily.dev/posts/cncf-unveils-schedule-for-kubecon-cloudnativecon-europe-2026-ikhcoa5cb) · CNCF · 2 upvotes · 0 comments
- [CNCF Debuts KubeCon \+ CloudNativeCon Japan 2026 Schedule](https://daily.dev/posts/cncf-debuts-kubecon-cloudnativecon-japan-2026-schedule-xp5pyudub) · CNCF · 1 upvotes · 0 comments

---

Tags: [#swift](https://daily.dev/tags/swift)

[View this post on daily.dev](https://daily.dev/posts/decoding-and-flattening-nested-json-with-codable-8fwfbviti)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"TechArticle","headline":"Decoding and Flattening Nested JSON with Codable","url":"https://daily.dev/posts/decoding-and-flattening-nested-json-with-codable-8fwfbviti","mainEntityOfPage":{"@type":"WebPage","@id":"https://daily.dev/posts/decoding-and-flattening-nested-json-with-codable-8fwfbviti"},"datePublished":"2024-04-03T13:18:56.209Z","dateModified":"2024-04-04T16:39:33.747Z","description":"Learn how to decode and flatten nested JSON objects in Swift using the Codable protocol and JSONDecoder class. Codable simplifies the process of mapping JSON...","image":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/a58edb2ba583c70284cb45d2532c0514?_a=AQAEufR","thumbnailUrl":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/a58edb2ba583c70284cb45d2532c0514?_a=AQAEufR","isAccessibleForFree":true,"articleSection":"Collections","inLanguage":"en","publisher":{"@type":"Organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180}},"author":{"@type":"Organization","name":"Collections","logo":"https://media.daily.dev/image/upload/s--fk_6ycEi--/f_auto,q_auto/v1780996001/logos/collections?_a=BAMAMiWQ0","url":"https://daily.dev/sources/collections"},"commentCount":0,"discussionUrl":"https://daily.dev/posts/decoding-and-flattening-nested-json-with-codable-8fwfbviti","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":1},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":0}],"keywords":"swift","timeRequired":"PT2M"}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"Collections","item":"https://daily.dev/sources/collections"},{"@type":"ListItem","position":3,"name":"Decoding and Flattening Nested JSON with Codable"}]}
```

