<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>codable &#8211; swiftyplace</title>
	<atom:link href="https://www.swiftyplace.com/blog/tag/codable/feed" rel="self" type="application/rss+xml" />
	<link>https://www.swiftyplace.com</link>
	<description>Learn how to build amazing apps with SwiftUI and Combine</description>
	<lastBuildDate>Tue, 28 Nov 2023 15:31:34 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>

<image>
	<url>https://www.swiftyplace.com/wp-content/uploads/2023/08/cropped-logo-1-32x32.png</url>
	<title>codable &#8211; swiftyplace</title>
	<link>https://www.swiftyplace.com</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Codable: How to simplify converting JSON data to Swift objects and vice versa</title>
		<link>https://www.swiftyplace.com/blog/codable-how-to-simplify-converting-json-data-to-swift-objects-and-vice-versa?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=codable-how-to-simplify-converting-json-data-to-swift-objects-and-vice-versa</link>
					<comments>https://www.swiftyplace.com/blog/codable-how-to-simplify-converting-json-data-to-swift-objects-and-vice-versa#respond</comments>
		
		<dc:creator><![CDATA[Karin Prater]]></dc:creator>
		<pubDate>Thu, 18 May 2023 08:24:33 +0000</pubDate>
				<category><![CDATA[Swift]]></category>
		<category><![CDATA[Data persistence]]></category>
		<category><![CDATA[codable]]></category>
		<category><![CDATA[codable swift]]></category>
		<category><![CDATA[decode json swift]]></category>
		<category><![CDATA[json to swift codable]]></category>
		<category><![CDATA[swift codable example]]></category>
		<category><![CDATA[swift codable protocol]]></category>
		<category><![CDATA[swift json]]></category>
		<guid isPermaLink="false">https://swiftyplace.com/?p=792</guid>

					<description><![CDATA[<p>Discover Swift's Codable protocol to simplify converting JSON data to Swift objects, with practical examples from real apps.</p>
<p>The post <a rel="nofollow" href="https://www.swiftyplace.com/blog/codable-how-to-simplify-converting-json-data-to-swift-objects-and-vice-versa">Codable: How to simplify converting JSON data to Swift objects and vice versa</a> appeared first on <a rel="nofollow" href="https://www.swiftyplace.com">swiftyplace</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>Are you tired of writing boilerplate code just to convert JSON data to Swift objects? Or perhaps you&#8217;ve been wrestling with a complex API response that just doesn&#8217;t seem to fit into your neat Swift data structures? Well, it&#8217;s time to put all that behind you. Say hello to <strong>Codable</strong>, a protocol in Swift 4 and later that makes it a breeze to convert data between JSON format and your own Swift data types.</p>



<p>We&#8217;ll be working with real-world examples to see <strong>Codable</strong> in action. Our first example is a SwiftUI app where users can add URLs to a reading list. It also shows how to save a file to disc, which you can learn about with&nbsp;<a href="https://www.swiftyplace.com/blog/file-manager-in-swift-reading-writing-and-deleting-files-and-directories" target="_blank" rel="noopener">File Manager in Swift</a>.&nbsp;</p>



<div style="height:26px" aria-hidden="true" class="wp-block-spacer"></div>



			
			
										
			
			


<div style="height:26px" aria-hidden="true" class="wp-block-spacer"></div>



<p>The second example is a fun one: we&#8217;ll fetch data from TheCatAPI and display information about various cat breeds. By the end of this post, you&#8217;ll have a solid understanding of <strong>Codable</strong> and will be able to simplify data conversion in your own apps.</p>



<p><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/2b07.png" alt="⬇" class="wp-smiley" style="height: 1em; max-height: 1em;" /> You can find the project files for the Reading List app <a href="https://github.com/gahntpo/WebViewProject/tree/main" target="_blank" rel="noopener noreferrer">here</a> and the cat app <a href="https://github.com/gahntpo/CatAPISwiftUI" target="_blank" rel="noopener">here</a>.</p>



<div style="height:50px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">Understanding JSON</h2>



<p>JSON is common data format, that is a text based data format. JSON stands for JavaScript Object Notation. It&#8217;s a data format that&#8217;s easy for humans to read and write, and easy for machines to parse and generate.</p>



<p>Think of JSON as a way of organizing data into key-value pairs, much like a dictionary. It uses simple syntax, with data structured through the use of curly braces {} to define objects, square brackets [] for arrays, and commas to separate data elements. Let&#8217;s look at a simple example:</p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono-NL.ttf" style="font-size:1rem;font-family:Code-Pro-JetBrains-Mono-NL,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span role="button" tabindex="0" data-code="{
    &quot;name&quot;: &quot;John Doe&quot;,
    &quot;age&quot;: 30,
    &quot;isStudent&quot;: false,
    &quot;courses&quot;: [&quot;Math&quot;, &quot;Science&quot;, &quot;History&quot;]
}" style="color:#f6f6f4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dracula-soft" style="background-color: #282A36" tabindex="0"><code><span class="line"><span style="color: #F6F6F4">{</span></span>
<span class="line"><span style="color: #F6F6F4">    </span><span style="color: #DEE492">&quot;</span><span style="color: #E7EE98">name</span><span style="color: #DEE492">&quot;</span><span style="color: #F286C4">:</span><span style="color: #F6F6F4"> </span><span style="color: #DEE492">&quot;</span><span style="color: #E7EE98">John Doe</span><span style="color: #DEE492">&quot;</span><span style="color: #F6F6F4">,</span></span>
<span class="line"><span style="color: #F6F6F4">    </span><span style="color: #DEE492">&quot;</span><span style="color: #E7EE98">age</span><span style="color: #DEE492">&quot;</span><span style="color: #F286C4">:</span><span style="color: #F6F6F4"> </span><span style="color: #BF9EEE">30</span><span style="color: #F6F6F4">,</span></span>
<span class="line"><span style="color: #F6F6F4">    </span><span style="color: #DEE492">&quot;</span><span style="color: #E7EE98">isStudent</span><span style="color: #DEE492">&quot;</span><span style="color: #F286C4">:</span><span style="color: #F6F6F4"> </span><span style="color: #BF9EEE">false</span><span style="color: #F6F6F4">,</span></span>
<span class="line"><span style="color: #F6F6F4">    </span><span style="color: #DEE492">&quot;</span><span style="color: #E7EE98">courses</span><span style="color: #DEE492">&quot;</span><span style="color: #F286C4">:</span><span style="color: #F6F6F4"> [</span><span style="color: #DEE492">&quot;</span><span style="color: #E7EE98">Math</span><span style="color: #DEE492">&quot;</span><span style="color: #F6F6F4">, </span><span style="color: #DEE492">&quot;</span><span style="color: #E7EE98">Science</span><span style="color: #DEE492">&quot;</span><span style="color: #F6F6F4">, </span><span style="color: #DEE492">&quot;</span><span style="color: #E7EE98">History</span><span style="color: #DEE492">&quot;</span><span style="color: #F6F6F4">]</span></span>
<span class="line"><span style="color: #F6F6F4">}</span></span></code></pre></div>



<div style="height:22px" aria-hidden="true" class="wp-block-spacer"></div>



<p>In this JSON, you have an object (the stuff inside the curly braces). This object has four keys: &#8220;name&#8221;, &#8220;age&#8221;, &#8220;isStudent&#8221;, and &#8220;courses&#8221;. The values for these keys are a string, a number, a boolean, and an array, respectively.</p>



<p>Now, JSON is the go-to when it comes to APIs and web services. Why? Because it&#8217;s straightforward, lightweight, and can be easily sent over the internet. Let&#8217;s say you&#8217;re fetching data from a weather API. The server might send you JSON data that looks like this:</p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono-NL.ttf" style="font-size:1rem;font-family:Code-Pro-JetBrains-Mono-NL,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span role="button" tabindex="0" data-code="{
    &quot;city&quot;: &quot;San Francisco&quot;,
    &quot;temperature&quot;: 22,
    &quot;weather&quot;: &quot;sunny&quot;
}" style="color:#f6f6f4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dracula-soft" style="background-color: #282A36" tabindex="0"><code><span class="line"><span style="color: #F6F6F4">{</span></span>
<span class="line"><span style="color: #F6F6F4">    </span><span style="color: #DEE492">&quot;</span><span style="color: #E7EE98">city</span><span style="color: #DEE492">&quot;</span><span style="color: #F286C4">:</span><span style="color: #F6F6F4"> </span><span style="color: #DEE492">&quot;</span><span style="color: #E7EE98">San Francisco</span><span style="color: #DEE492">&quot;</span><span style="color: #F6F6F4">,</span></span>
<span class="line"><span style="color: #F6F6F4">    </span><span style="color: #DEE492">&quot;</span><span style="color: #E7EE98">temperature</span><span style="color: #DEE492">&quot;</span><span style="color: #F286C4">:</span><span style="color: #F6F6F4"> </span><span style="color: #BF9EEE">22</span><span style="color: #F6F6F4">,</span></span>
<span class="line"><span style="color: #F6F6F4">    </span><span style="color: #DEE492">&quot;</span><span style="color: #E7EE98">weather</span><span style="color: #DEE492">&quot;</span><span style="color: #F286C4">:</span><span style="color: #F6F6F4"> </span><span style="color: #DEE492">&quot;</span><span style="color: #E7EE98">sunny</span><span style="color: #DEE492">&quot;</span></span>
<span class="line"><span style="color: #F6F6F4">}</span></span></code></pre></div>



<div style="height:22px" aria-hidden="true" class="wp-block-spacer"></div>



<p>This JSON tells you that it&#8217;s a sunny 22 degrees in San Francisco. Neat, huh?</p>



<p>But it&#8217;s not just about online data. JSON can also be used to save user data locally on a device. Let&#8217;s imagine you&#8217;re building a reading list app. When a user adds a new book to their list, you could save that data in a JSON file like this:</p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono-NL.ttf" style="font-size:1rem;font-family:Code-Pro-JetBrains-Mono-NL,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span role="button" tabindex="0" data-code="{
    &quot;books&quot;: [
        {
            &quot;title&quot;: &quot;1984&quot;,
            &quot;author&quot;: &quot;George Orwell&quot;,
            &quot;isRead&quot;: false
        },
        {
            &quot;title&quot;: &quot;To Kill a Mockingbird&quot;,
            &quot;author&quot;: &quot;Harper Lee&quot;,
            &quot;isRead&quot;: true
        }
    ]
}
" style="color:#f6f6f4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dracula-soft" style="background-color: #282A36" tabindex="0"><code><span class="line"><span style="color: #F6F6F4">{</span></span>
<span class="line"><span style="color: #F6F6F4">    </span><span style="color: #DEE492">&quot;</span><span style="color: #E7EE98">books</span><span style="color: #DEE492">&quot;</span><span style="color: #F286C4">:</span><span style="color: #F6F6F4"> [</span></span>
<span class="line"><span style="color: #F6F6F4">        {</span></span>
<span class="line"><span style="color: #F6F6F4">            </span><span style="color: #DEE492">&quot;</span><span style="color: #E7EE98">title</span><span style="color: #DEE492">&quot;</span><span style="color: #F286C4">:</span><span style="color: #F6F6F4"> </span><span style="color: #DEE492">&quot;</span><span style="color: #E7EE98">1984</span><span style="color: #DEE492">&quot;</span><span style="color: #F6F6F4">,</span></span>
<span class="line"><span style="color: #F6F6F4">            </span><span style="color: #DEE492">&quot;</span><span style="color: #E7EE98">author</span><span style="color: #DEE492">&quot;</span><span style="color: #F286C4">:</span><span style="color: #F6F6F4"> </span><span style="color: #DEE492">&quot;</span><span style="color: #E7EE98">George Orwell</span><span style="color: #DEE492">&quot;</span><span style="color: #F6F6F4">,</span></span>
<span class="line"><span style="color: #F6F6F4">            </span><span style="color: #DEE492">&quot;</span><span style="color: #E7EE98">isRead</span><span style="color: #DEE492">&quot;</span><span style="color: #F286C4">:</span><span style="color: #F6F6F4"> </span><span style="color: #BF9EEE">false</span></span>
<span class="line"><span style="color: #F6F6F4">        },</span></span>
<span class="line"><span style="color: #F6F6F4">        {</span></span>
<span class="line"><span style="color: #F6F6F4">            </span><span style="color: #DEE492">&quot;</span><span style="color: #E7EE98">title</span><span style="color: #DEE492">&quot;</span><span style="color: #F286C4">:</span><span style="color: #F6F6F4"> </span><span style="color: #DEE492">&quot;</span><span style="color: #E7EE98">To Kill a Mockingbird</span><span style="color: #DEE492">&quot;</span><span style="color: #F6F6F4">,</span></span>
<span class="line"><span style="color: #F6F6F4">            </span><span style="color: #DEE492">&quot;</span><span style="color: #E7EE98">author</span><span style="color: #DEE492">&quot;</span><span style="color: #F286C4">:</span><span style="color: #F6F6F4"> </span><span style="color: #DEE492">&quot;</span><span style="color: #E7EE98">Harper Lee</span><span style="color: #DEE492">&quot;</span><span style="color: #F6F6F4">,</span></span>
<span class="line"><span style="color: #F6F6F4">            </span><span style="color: #DEE492">&quot;</span><span style="color: #E7EE98">isRead</span><span style="color: #DEE492">&quot;</span><span style="color: #F286C4">:</span><span style="color: #F6F6F4"> </span><span style="color: #BF9EEE">true</span></span>
<span class="line"><span style="color: #F6F6F4">        }</span></span>
<span class="line"><span style="color: #F6F6F4">    ]</span></span>
<span class="line"><span style="color: #F6F6F4">}</span></span>
<span class="line"></span></code></pre></div>



<div style="height:49px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">What is Codable in Swift?</h2>



<p>Codable is a type alias in Swift that stands for two things: Decodable and Encodable protocols. If a type is Codable, that means it can convert itself into data (that&#8217;s the Encodable part), and it can also initialize itself from data (that&#8217;s the Decodable part). Super handy, right?</p>



<p>So why do we need the Codable protocols? Well, remember the JSON we talked about earlier? It&#8217;s great for sending data over the internet, but it&#8217;s not so great when we want to use that data in our Swift code. We need a way to convert that JSON into Swift objects, and that&#8217;s where Codable comes in.</p>



<p>Here&#8217;s the deal: when you fetch JSON data from an API, that data comes back as, well, data. It&#8217;s not a Swift object that you can use right away in your code. You have to decode that data into a Swift object. And if you want to send data to a server, you have to do the reverse: encode your Swift object into data. Codable makes this process a lot easier by doing most of the heavy lifting for you.</p>



<div style="height:49px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading">Making a Swift-Type Codable</h3>



<p>As an example, I am going to use a reading list app, that lets the user enter URLs for web pages he/she wants to read later. The data needs to be saved on the device so that it is still there the next time the app is launched. You can see the app interface in the following example screenshots:</p>



<figure class="wp-block-image"><img decoding="async" src="https://www.swiftyplace.com/wp-content/uploads/2023/08/swiftui_example_file_saving.jpg" alt="swiftui demo project to store user data as json with Codable."/></figure>



<div style="height:20px" aria-hidden="true" class="wp-block-spacer"></div>



<p>The first step is to define your Swift type. This could be a class, a struct, or even an enum. For now, we&#8217;ll stick with structs because they&#8217;re simple and lightweight.</p>



<p>Next, you&#8217;ll need to decide what properties your type should have. Remember, each property will map to a key in your JSON.</p>



<p>Once you&#8217;ve got your data type and its properties, you just need to add <strong>: Codable</strong> after your type&#8217;s name. This tells Swift that your type should conform to the Codable protocol.</p>



<p>Now let&#8217;s see this in action. Consider this struct, which we&#8217;ll use to store reading data in a SwiftUI app:</p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono-NL.ttf" style="font-size:1rem;font-family:Code-Pro-JetBrains-Mono-NL,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span role="button" tabindex="0" data-code="struct ReadingData: Codable, Equatable, Identifiable {
    let url: URL?
    let title: String
    let creationDate: Date
    var hasFinishedReading: Bool
    var id: Date { return creationDate }
}" style="color:#f6f6f4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dracula-soft" style="background-color: #282A36" tabindex="0"><code><span class="line"><span style="color: #F286C4">struct</span><span style="color: #F6F6F4"> </span><span style="color: #97E1F1; font-style: italic">ReadingData</span><span style="color: #F6F6F4">: Codable, Equatable, Identifiable {</span></span>
<span class="line"><span style="color: #F6F6F4">    </span><span style="color: #F286C4">let</span><span style="color: #F6F6F4"> url: URL</span><span style="color: #F286C4">?</span></span>
<span class="line"><span style="color: #F6F6F4">    </span><span style="color: #F286C4">let</span><span style="color: #F6F6F4"> title: </span><span style="color: #97E1F1; font-style: italic">String</span></span>
<span class="line"><span style="color: #F6F6F4">    </span><span style="color: #F286C4">let</span><span style="color: #F6F6F4"> creationDate: Date</span></span>
<span class="line"><span style="color: #F6F6F4">    </span><span style="color: #F286C4">var</span><span style="color: #F6F6F4"> hasFinishedReading: </span><span style="color: #97E1F1; font-style: italic">Bool</span></span>
<span class="line"><span style="color: #F6F6F4">    </span><span style="color: #F286C4">var</span><span style="color: #F6F6F4"> id: Date { </span><span style="color: #F286C4">return</span><span style="color: #F6F6F4"> creationDate }</span></span>
<span class="line"><span style="color: #F6F6F4">}</span></span></code></pre></div>



<div style="height:21px" aria-hidden="true" class="wp-block-spacer"></div>



<p>This <strong>ReadingData</strong> struct has a few properties: a URL (which is optional, hence the question mark), a title, a creation date, a boolean indicating whether the user has finished reading, and an id.</p>



<p>But notice the stuff after the colon: <strong>Codable, Equatable, Identifiable</strong>. Here&#8217;s what each one means:</p>



<ul class="wp-block-list">
<li><strong>Codable</strong>: As you know, this means the struct can be encoded into data and decoded from data.</li>



<li><strong>Equatable</strong>: This means you can compare two instances of the struct for equality.</li>



<li><strong>Identifiable</strong>: This is a requirement for certain SwiftUI features. It means each instance of the struct has a unique identifier. In this case, we&#8217;re using the creation date as the id.</li>
</ul>



<div style="height:21px" aria-hidden="true" class="wp-block-spacer"></div>



<p>So how might you use this <strong>ReadingData</strong> struct in an app? Let&#8217;s say your app lets users save web pages to a reading list. When a user adds an item, you could create a new <strong>ReadingData</strong> instance with the web page&nbsp;URL and title, the current date, and <strong>hasFinishedReading</strong> set to false. You could then encode this <strong>ReadingData</strong> instance into data and save it locally using FileManager. Read about Managing <a href="https://www.swiftyplace.com/blog/file-manager-in-swift-reading-writing-and-deleting-files-and-directories" target="_blank" rel="noopener">Swift files</a>.</p>



<p><strong><u>Important: </u></strong>As long as all types that are used inside your custom struct conform to Codable (like String, Int, and Date types), your custom types can easily conform to Codable protocol. If you use complex json data with e.g. nested custom types inside, add Codable to all of them.</p>



<div style="height:42px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">Encoding and Decoding JSON Objects</h2>



<p>Let&#8217;s roll up our sleeves and dive into encoding and decoding with Codable. Remember our <strong>ReadingData</strong> struct from the previous section? Let&#8217;s use that for our examples.</p>



<div style="height:28px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading">Encoding a Swift Object to JSON</h3>



<p>When you have a Swift object and you want to convert it to JSON, that&#8217;s called encoding. Here&#8217;s how you do it:</p>



<p>First, you&#8217;ll need an instance of the object you want to encode. Let&#8217;s create a <strong>ReadingData</strong> instance:</p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono-NL.ttf" style="font-size:1rem;font-family:Code-Pro-JetBrains-Mono-NL,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span role="button" tabindex="0" data-code="let article = ReadingData(url: URL(string: &quot;https://example.com&quot;),
                          title: &quot;Interesting Article&quot;,
                          creationDate: Date(),
                          hasFinishedReading: false)" style="color:#f6f6f4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dracula-soft" style="background-color: #282A36" tabindex="0"><code><span class="line"><span style="color: #F286C4">let</span><span style="color: #F6F6F4"> article </span><span style="color: #F286C4">=</span><span style="color: #F6F6F4"> </span><span style="color: #97E1F1">ReadingData</span><span style="color: #F6F6F4">(</span><span style="color: #97E1F1">url</span><span style="color: #F6F6F4">: </span><span style="color: #97E1F1">URL</span><span style="color: #F6F6F4">(</span><span style="color: #97E1F1">string</span><span style="color: #F6F6F4">: </span><span style="color: #DEE492">&quot;</span><span style="color: #E7EE98">https://example.com</span><span style="color: #DEE492">&quot;</span><span style="color: #F6F6F4">),</span></span>
<span class="line"><span style="color: #F6F6F4">                          </span><span style="color: #97E1F1">title</span><span style="color: #F6F6F4">: </span><span style="color: #DEE492">&quot;</span><span style="color: #E7EE98">Interesting Article</span><span style="color: #DEE492">&quot;</span><span style="color: #F6F6F4">,</span></span>
<span class="line"><span style="color: #F6F6F4">                          </span><span style="color: #97E1F1">creationDate</span><span style="color: #F6F6F4">: </span><span style="color: #97E1F1">Date</span><span style="color: #F6F6F4">(),</span></span>
<span class="line"><span style="color: #F6F6F4">                          </span><span style="color: #97E1F1">hasFinishedReading</span><span style="color: #F6F6F4">: </span><span style="color: #BF9EEE">false</span><span style="color: #F6F6F4">)</span></span></code></pre></div>



<div style="height:27px" aria-hidden="true" class="wp-block-spacer"></div>



<p>Now, to convert this <strong>ReadingData</strong> instance into JSON, you&#8217;ll use a <strong>JSONEncoder</strong>. Here&#8217;s how:</p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono-NL.ttf" style="font-size:1rem;font-family:Code-Pro-JetBrains-Mono-NL,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span role="button" tabindex="0" data-code="let encoder = JSONEncoder()
encoder.dateEncodingStrategy = .iso8601 // Use ISO 8601 date format
do {
    let jsonData = try encoder.encode(article)
    // jsonData now contains the JSON data for the article
    if let jsonString = String(data: jsonData, encoding: .utf8) {
        print(jsonString) // Prints the JSON string
    }
} catch {
    print(&quot;Error encoding article: \(error)&quot;)
}" style="color:#f6f6f4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dracula-soft" style="background-color: #282A36" tabindex="0"><code><span class="line"><span style="color: #F286C4">let</span><span style="color: #F6F6F4"> encoder </span><span style="color: #F286C4">=</span><span style="color: #F6F6F4"> </span><span style="color: #97E1F1">JSONEncoder</span><span style="color: #F6F6F4">()</span></span>
<span class="line"><span style="color: #F6F6F4">encoder.dateEncodingStrategy </span><span style="color: #F286C4">=</span><span style="color: #F6F6F4"> .iso8601 </span><span style="color: #7B7F8B">// Use ISO 8601 date format</span></span>
<span class="line"><span style="color: #F286C4">do</span><span style="color: #F6F6F4"> {</span></span>
<span class="line"><span style="color: #F6F6F4">    </span><span style="color: #F286C4">let</span><span style="color: #F6F6F4"> jsonData </span><span style="color: #F286C4">=</span><span style="color: #F6F6F4"> </span><span style="color: #F286C4">try</span><span style="color: #F6F6F4"> encoder.</span><span style="color: #97E1F1">encode</span><span style="color: #F6F6F4">(article)</span></span>
<span class="line"><span style="color: #F6F6F4">    </span><span style="color: #7B7F8B">// jsonData now contains the JSON data for the article</span></span>
<span class="line"><span style="color: #F6F6F4">    </span><span style="color: #F286C4">if</span><span style="color: #F6F6F4"> </span><span style="color: #F286C4">let</span><span style="color: #F6F6F4"> jsonString </span><span style="color: #F286C4">=</span><span style="color: #F6F6F4"> </span><span style="color: #97E1F1; font-style: italic">String</span><span style="color: #F6F6F4">(</span><span style="color: #97E1F1">data</span><span style="color: #F6F6F4">: jsonData, </span><span style="color: #97E1F1">encoding</span><span style="color: #F6F6F4">: .</span><span style="color: #BF9EEE">utf8</span><span style="color: #F6F6F4">) {</span></span>
<span class="line"><span style="color: #F6F6F4">        </span><span style="color: #97E1F1">print</span><span style="color: #F6F6F4">(jsonString) </span><span style="color: #7B7F8B">// Prints the JSON string</span></span>
<span class="line"><span style="color: #F6F6F4">    }</span></span>
<span class="line"><span style="color: #F6F6F4">} </span><span style="color: #F286C4">catch</span><span style="color: #F6F6F4"> {</span></span>
<span class="line"><span style="color: #F6F6F4">    </span><span style="color: #97E1F1">print</span><span style="color: #F6F6F4">(</span><span style="color: #DEE492">&quot;</span><span style="color: #E7EE98">Error encoding article: </span><span style="color: #F286C4">\(</span><span style="color: #E7EE98">error</span><span style="color: #F286C4">)</span><span style="color: #DEE492">&quot;</span><span style="color: #F6F6F4">)</span></span>
<span class="line"><span style="color: #F6F6F4">}</span></span></code></pre></div>



<div style="height:27px" aria-hidden="true" class="wp-block-spacer"></div>



<p>If the json encoding above is successful, <strong>jsonData</strong> will contain the JSON data for the <strong>article</strong>. You can convert this data into a string to see the JSON.</p>



<div style="height:44px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading">Decoding JSON to a Swift Object</h3>



<p>Now, what if you have some JSON and you want to convert it to a Swift object? That&#8217;s called decoding, and it&#8217;s just as easy.</p>



<p>If we continue the above example, the JSON data would look like this:</p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono-NL.ttf" style="font-size:1rem;font-family:Code-Pro-JetBrains-Mono-NL,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span role="button" tabindex="0" data-code="{
    &quot;title&quot;: &quot;Interesting Article&quot;,
    &quot;url&quot;: &quot;https://example.com&quot;,
    &quot;creationDate&quot;: &quot;2023-05-19T14:36:00Z&quot;,
    &quot;hasFinishedReading&quot;: false
}" style="color:#f6f6f4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dracula-soft" style="background-color: #282A36" tabindex="0"><code><span class="line"><span style="color: #F6F6F4">{</span></span>
<span class="line"><span style="color: #F6F6F4">    </span><span style="color: #DEE492">&quot;</span><span style="color: #E7EE98">title</span><span style="color: #DEE492">&quot;</span><span style="color: #F286C4">:</span><span style="color: #F6F6F4"> </span><span style="color: #DEE492">&quot;</span><span style="color: #E7EE98">Interesting Article</span><span style="color: #DEE492">&quot;</span><span style="color: #F6F6F4">,</span></span>
<span class="line"><span style="color: #F6F6F4">    </span><span style="color: #DEE492">&quot;</span><span style="color: #E7EE98">url</span><span style="color: #DEE492">&quot;</span><span style="color: #F286C4">:</span><span style="color: #F6F6F4"> </span><span style="color: #DEE492">&quot;</span><span style="color: #E7EE98">https://example.com</span><span style="color: #DEE492">&quot;</span><span style="color: #F6F6F4">,</span></span>
<span class="line"><span style="color: #F6F6F4">    </span><span style="color: #DEE492">&quot;</span><span style="color: #E7EE98">creationDate</span><span style="color: #DEE492">&quot;</span><span style="color: #F286C4">:</span><span style="color: #F6F6F4"> </span><span style="color: #DEE492">&quot;</span><span style="color: #E7EE98">2023-05-19T14:36:00Z</span><span style="color: #DEE492">&quot;</span><span style="color: #F6F6F4">,</span></span>
<span class="line"><span style="color: #F6F6F4">    </span><span style="color: #DEE492">&quot;</span><span style="color: #E7EE98">hasFinishedReading</span><span style="color: #DEE492">&quot;</span><span style="color: #F286C4">:</span><span style="color: #F6F6F4"> </span><span style="color: #BF9EEE">false</span></span>
<span class="line"><span style="color: #F6F6F4">}</span></span></code></pre></div>



<div style="height:21px" aria-hidden="true" class="wp-block-spacer"></div>



<p>To convert this JSON into a <strong>ReadingData</strong> instance, you&#8217;ll use a <strong>JSONDecoder</strong>. Here&#8217;s how:</p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono-NL.ttf" style="font-size:1rem;font-family:Code-Pro-JetBrains-Mono-NL,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span role="button" tabindex="0" data-code="let decoder = JSONDecoder()
decoder.dateDecodingStrategy = .iso8601 // Use ISO 8601 date format
do {
    let article = try decoder.decode(ReadingData.self, from: jsonData)
    // article is now a ReadingData instance
    print(article.title) // Prints &quot;Interesting Article&quot;
} catch {
    print(&quot;Error decoding JSON: \(error)&quot;)
}" style="color:#f6f6f4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dracula-soft" style="background-color: #282A36" tabindex="0"><code><span class="line"><span style="color: #F286C4">let</span><span style="color: #F6F6F4"> decoder </span><span style="color: #F286C4">=</span><span style="color: #F6F6F4"> </span><span style="color: #97E1F1">JSONDecoder</span><span style="color: #F6F6F4">()</span></span>
<span class="line"><span style="color: #F6F6F4">decoder.dateDecodingStrategy </span><span style="color: #F286C4">=</span><span style="color: #F6F6F4"> .iso8601 </span><span style="color: #7B7F8B">// Use ISO 8601 date format</span></span>
<span class="line"><span style="color: #F286C4">do</span><span style="color: #F6F6F4"> {</span></span>
<span class="line"><span style="color: #F6F6F4">    </span><span style="color: #F286C4">let</span><span style="color: #F6F6F4"> article </span><span style="color: #F286C4">=</span><span style="color: #F6F6F4"> </span><span style="color: #F286C4">try</span><span style="color: #F6F6F4"> decoder.</span><span style="color: #97E1F1">decode</span><span style="color: #F6F6F4">(ReadingData.</span><span style="color: #F286C4">self</span><span style="color: #F6F6F4">, </span><span style="color: #97E1F1">from</span><span style="color: #F6F6F4">: jsonData)</span></span>
<span class="line"><span style="color: #F6F6F4">    </span><span style="color: #7B7F8B">// article is now a ReadingData instance</span></span>
<span class="line"><span style="color: #F6F6F4">    </span><span style="color: #97E1F1">print</span><span style="color: #F6F6F4">(article.title) </span><span style="color: #7B7F8B">// Prints &quot;Interesting Article&quot;</span></span>
<span class="line"><span style="color: #F6F6F4">} </span><span style="color: #F286C4">catch</span><span style="color: #F6F6F4"> {</span></span>
<span class="line"><span style="color: #F6F6F4">    </span><span style="color: #97E1F1">print</span><span style="color: #F6F6F4">(</span><span style="color: #DEE492">&quot;</span><span style="color: #E7EE98">Error decoding JSON: </span><span style="color: #F286C4">\(</span><span style="color: #E7EE98">error</span><span style="color: #F286C4">)</span><span style="color: #DEE492">&quot;</span><span style="color: #F6F6F4">)</span></span>
<span class="line"><span style="color: #F6F6F4">}</span></span></code></pre></div>



<div style="height:21px" aria-hidden="true" class="wp-block-spacer"></div>



<p>If the decoding is successful, <strong>article</strong> will be a <strong>ReadingData</strong> instance that you can use in your code.</p>



<p>Notice the <strong>do-catch</strong> blocks in both examples? Encoding and decoding can fail if the JSON doesn&#8217;t match your Swift type, so you need to handle any errors that might occur.</p>



<p>And there you have it! You&#8217;ve just encoded a Swift object to JSON and decoded JSON to a Swift object. But what about nested objects and arrays? That&#8217;s up next. Stay tuned!</p>



<div style="height:49px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">Error Handling for JSON Parsing</h2>



<p>Dealing with JSON means dealing with uncertainty. You can&#8217;t always control the data you&#8217;re working with, especially when it&#8217;s coming from an API. That&#8217;s why error handling is a crucial part of working with Codable. Let&#8217;s talk about the kinds of errors that can crop up and how you can handle them.</p>



<div style="height:34px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading">Types of Errors</h3>



<p>There are several types of errors that can occur when you&#8217;re encoding and decoding with Codable:</p>



<ul class="wp-block-list">
<li><strong>DecodingError.typeMismatch</strong>: This occurs when the type you&#8217;re trying to decode doesn&#8217;t match the actual type in the JSON. For example, if you&#8217;re expecting a string but the JSON has an integer, you&#8217;ll get a <strong>typeMismatch</strong> error.</li>



<li><strong>DecodingError.valueNotFound</strong>: This happens when a required value is missing from the JSON. If your Swift type expects a certain key, but that key is not present in the JSON, you&#8217;ll get a <strong>valueNotFound</strong> error.</li>



<li><strong>DecodingError.keyNotFound</strong>: This is similar to <strong>valueNotFound</strong>, but it occurs when a key is missing from the JSON.</li>



<li><strong>EncodingError.invalidValue</strong>: This error occurs when you&#8217;re trying to encode a Swift object, but one of the values can&#8217;t be encoded. For instance, if you&#8217;re trying to encode a URL, but the URL is not valid, you&#8217;ll get an <strong>invalidValue</strong> error.</li>
</ul>



<div style="height:34px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading">How to handle errors with a do-catch block</h3>



<p>How do you handle these errors? Well, when you&#8217;re encoding or decoding with Codable, you&#8217;ll typically use a <strong>do-catch</strong>block. This allows you to catch any errors that occur and handle them appropriately.</p>



<p>For instance, when you&#8217;re decoding, you could do something like this:</p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono-NL.ttf" style="font-size:1rem;font-family:Code-Pro-JetBrains-Mono-NL,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span role="button" tabindex="0" data-code="let decoder = JSONDecoder()
do {
    let breed = try decoder.decode(Breed.self, from: jsonData)
    // breed is now a Breed instance
} catch let DecodingError.typeMismatch(type, context) {
    print(&quot;Type mismatch for \(type): \(context.debugDescription)&quot;)
} catch let DecodingError.valueNotFound(type, context) {
    print(&quot;Value not found for \(type): \(context.debugDescription)&quot;)
} catch let DecodingError.keyNotFound(key, context) {
    print(&quot;Key not found: \(key.stringValue) - \(context.debugDescription)&quot;)
} catch let error {
    print(&quot;Error decoding JSON: \(error)&quot;)
}" style="color:#f6f6f4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dracula-soft" style="background-color: #282A36" tabindex="0"><code><span class="line"><span style="color: #F286C4">let</span><span style="color: #F6F6F4"> decoder </span><span style="color: #F286C4">=</span><span style="color: #F6F6F4"> </span><span style="color: #97E1F1">JSONDecoder</span><span style="color: #F6F6F4">()</span></span>
<span class="line"><span style="color: #F286C4">do</span><span style="color: #F6F6F4"> {</span></span>
<span class="line"><span style="color: #F6F6F4">    </span><span style="color: #F286C4">let</span><span style="color: #F6F6F4"> breed </span><span style="color: #F286C4">=</span><span style="color: #F6F6F4"> </span><span style="color: #F286C4">try</span><span style="color: #F6F6F4"> decoder.</span><span style="color: #97E1F1">decode</span><span style="color: #F6F6F4">(Breed.</span><span style="color: #F286C4">self</span><span style="color: #F6F6F4">, </span><span style="color: #97E1F1">from</span><span style="color: #F6F6F4">: jsonData)</span></span>
<span class="line"><span style="color: #F6F6F4">    </span><span style="color: #7B7F8B">// breed is now a Breed instance</span></span>
<span class="line"><span style="color: #F6F6F4">} </span><span style="color: #F286C4">catch</span><span style="color: #F6F6F4"> </span><span style="color: #F286C4">let</span><span style="color: #F6F6F4"> DecodingError.</span><span style="color: #97E1F1">typeMismatch</span><span style="color: #F6F6F4">(type, context) {</span></span>
<span class="line"><span style="color: #F6F6F4">    </span><span style="color: #97E1F1">print</span><span style="color: #F6F6F4">(</span><span style="color: #DEE492">&quot;</span><span style="color: #E7EE98">Type mismatch for </span><span style="color: #F286C4">\(</span><span style="color: #E7EE98">type</span><span style="color: #F286C4">)</span><span style="color: #E7EE98">: </span><span style="color: #F286C4">\(</span><span style="color: #E7EE98">context.</span><span style="color: #BF9EEE">debugDescription</span><span style="color: #F286C4">)</span><span style="color: #DEE492">&quot;</span><span style="color: #F6F6F4">)</span></span>
<span class="line"><span style="color: #F6F6F4">} </span><span style="color: #F286C4">catch</span><span style="color: #F6F6F4"> </span><span style="color: #F286C4">let</span><span style="color: #F6F6F4"> DecodingError.</span><span style="color: #97E1F1">valueNotFound</span><span style="color: #F6F6F4">(type, context) {</span></span>
<span class="line"><span style="color: #F6F6F4">    </span><span style="color: #97E1F1">print</span><span style="color: #F6F6F4">(</span><span style="color: #DEE492">&quot;</span><span style="color: #E7EE98">Value not found for </span><span style="color: #F286C4">\(</span><span style="color: #E7EE98">type</span><span style="color: #F286C4">)</span><span style="color: #E7EE98">: </span><span style="color: #F286C4">\(</span><span style="color: #E7EE98">context.</span><span style="color: #BF9EEE">debugDescription</span><span style="color: #F286C4">)</span><span style="color: #DEE492">&quot;</span><span style="color: #F6F6F4">)</span></span>
<span class="line"><span style="color: #F6F6F4">} </span><span style="color: #F286C4">catch</span><span style="color: #F6F6F4"> </span><span style="color: #F286C4">let</span><span style="color: #F6F6F4"> DecodingError.</span><span style="color: #97E1F1">keyNotFound</span><span style="color: #F6F6F4">(key, context) {</span></span>
<span class="line"><span style="color: #F6F6F4">    </span><span style="color: #97E1F1">print</span><span style="color: #F6F6F4">(</span><span style="color: #DEE492">&quot;</span><span style="color: #E7EE98">Key not found: </span><span style="color: #F286C4">\(</span><span style="color: #E7EE98">key.</span><span style="color: #BF9EEE">stringValue</span><span style="color: #F286C4">)</span><span style="color: #E7EE98"> - </span><span style="color: #F286C4">\(</span><span style="color: #E7EE98">context.</span><span style="color: #BF9EEE">debugDescription</span><span style="color: #F286C4">)</span><span style="color: #DEE492">&quot;</span><span style="color: #F6F6F4">)</span></span>
<span class="line"><span style="color: #F6F6F4">} </span><span style="color: #F286C4">catch</span><span style="color: #F6F6F4"> </span><span style="color: #F286C4">let</span><span style="color: #F6F6F4"> error {</span></span>
<span class="line"><span style="color: #F6F6F4">    </span><span style="color: #97E1F1">print</span><span style="color: #F6F6F4">(</span><span style="color: #DEE492">&quot;</span><span style="color: #E7EE98">Error decoding JSON: </span><span style="color: #F286C4">\(</span><span style="color: #E7EE98">error</span><span style="color: #F286C4">)</span><span style="color: #DEE492">&quot;</span><span style="color: #F6F6F4">)</span></span>
<span class="line"><span style="color: #F6F6F4">}</span></span></code></pre></div>



<div style="height:20px" aria-hidden="true" class="wp-block-spacer"></div>



<p>In this example, each <strong>catch</strong> block handles a different type of error. If a <strong>typeMismatch</strong> error occurs, you&#8217;ll print a message with the mismatched type and a description of the error. If a <strong>valueNotFound</strong> or <strong>keyNotFound</strong> error occurs, you&#8217;ll print a similar message with the missing value or key. If any other error occurs, you&#8217;ll simply print a message with the error.</p>



<p>Error handling might not be the most glamorous part of working with Codable, but it&#8217;s one of the most important. By handling errors properly, you can ensure that your app runs smoothly, even when the data you&#8217;re working with is less than perfect.</p>



<div style="height:20px" aria-hidden="true" class="wp-block-spacer"></div>



<div class="wp-block-group alignfull has-medium-font-size" style="border-style:none;border-width:0px;border-radius:0px"><div class="wp-block-group__inner-container is-layout-constrained wp-block-group-is-layout-constrained"><div class="gb-container gb-container-c5dedc2e">
<div class="gb-container gb-container-f7a433c7">
<div class="gb-container gb-container-7a8dc7a3">

<figure class="gb-block-image gb-block-image-f04fbbad"><a href="https://www.swiftyplace.com/free-swiftui-layout-cookbook" target="_blank" rel="noopener noreferrer"><img fetchpriority="high" decoding="async" width="640" height="500" class="gb-image gb-image-f04fbbad" src="http://www.swiftyplace.com/wp-content/uploads/2024/01/swiftui_roadmap_preview.webp" alt="swiftui roadmap " title="swiftui_roadmap_preview" srcset="https://www.swiftyplace.com/wp-content/uploads/2024/01/swiftui_roadmap_preview.webp 640w, https://www.swiftyplace.com/wp-content/uploads/2024/01/swiftui_roadmap_preview-300x234.webp 300w" sizes="(max-width: 640px) 100vw, 640px" /></a></figure>

</div>

<div class="gb-container gb-container-98352fe6">

<h2 class="gb-headline gb-headline-4aacd94c gb-headline-text">Feeling Lost in SwiftUI?</h2>



<p class="gb-headline gb-headline-5e6c4f85 gb-headline-text">This SwiftUI roadmap shows you what to learn next.</p>



<ul style="margin-top:0;margin-bottom:0;font-size:18px" class="wp-block-list">
<li>Key concepts at a glance</li>



<li>Spot your knowledge gaps</li>



<li>Guide your learning path</li>
</ul>


<div class="gb-container gb-container-34a8ad02">

<a class="gb-button gb-button-63e90e76 gb-button-blue" href="https://school.swiftyplace.com/f/swiftui-roadmap" target="_blank" rel="noopener noreferrer"><span class="gb-button-text">Get the FREE PDF</span><span class="gb-icon"><svg aria-hidden="true" role="img" height="1em" width="1em" viewBox="0 0 256 512" xmlns="http://www.w3.org/2000/svg"><path fill="currentColor" d="M224.3 273l-136 136c-9.4 9.4-24.6 9.4-33.9 0l-22.6-22.6c-9.4-9.4-9.4-24.6 0-33.9l96.4-96.4-96.4-96.4c-9.4-9.4-9.4-24.6 0-33.9L54.3 103c9.4-9.4 24.6-9.4 33.9 0l136 136c9.5 9.4 9.5 24.6.1 34z"></path></svg></span></a>

</div>


<p class="gb-headline gb-headline-7ef80e86 gb-headline-text">Ideal for beginners and self-learners.</p>

</div>
</div>
</div></div></div>



<div style="height:50px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">Working with an API and Advanced JSON Format</h2>



<p>Alright, you&#8217;ve got the basics down. Now let&#8217;s dive into some of the more advanced techniques you can use with Codable. For this, I&#8217;ll use another real-world example project: an app that fetches data about cat breeds from the <a href="https://thecatapi.com/" target="_blank" rel="noopener noreferrer">TheCatAPI</a>.</p>



<figure class="wp-block-image size-large"><img decoding="async" width="1024" height="788" src="https://www.swiftyplace.com/wp-content/uploads/2023/10/cat_breed_ios_app-1024x788.webp" alt="iOS app where data is fetched from the catapi and transformed from Json to swift models with Codable" class="wp-image-3823" srcset="https://www.swiftyplace.com/wp-content/uploads/2023/10/cat_breed_ios_app-1024x788.webp 1024w, https://www.swiftyplace.com/wp-content/uploads/2023/10/cat_breed_ios_app-300x231.webp 300w, https://www.swiftyplace.com/wp-content/uploads/2023/10/cat_breed_ios_app-768x591.webp 768w, https://www.swiftyplace.com/wp-content/uploads/2023/10/cat_breed_ios_app-1536x1182.webp 1536w, https://www.swiftyplace.com/wp-content/uploads/2023/10/cat_breed_ios_app.webp 1855w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>



<div style="height:19px" aria-hidden="true" class="wp-block-spacer"></div>



<p>The following example is a JSON response from the server:</p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono-NL.ttf" style="font-size:1rem;font-family:Code-Pro-JetBrains-Mono-NL,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span role="button" tabindex="0" data-code="[{&quot;weight&quot;:{&quot;imperial&quot;:&quot;7  -  10&quot;,&quot;metric&quot;:&quot;3 - 5&quot;},&quot;id&quot;:&quot;abys&quot;,&quot;name&quot;:&quot;Abyssinian&quot;,&quot;cfa_url&quot;:&quot;http://cfa.org/Breeds/BreedsAB/Abyssinian.aspx&quot;,&quot;vetstreet_url&quot;:&quot;http://www.vetstreet.com/cats/abyssinian&quot;,&quot;vcahospitals_url&quot;:&quot;https://vcahospitals.com/know-your-pet/cat-breeds/abyssinian&quot;,&quot;temperament&quot;:&quot;Active, Energetic, Independent, Intelligent, Gentle&quot;,&quot;origin&quot;:&quot;Egypt&quot;,&quot;country_codes&quot;:&quot;EG&quot;,&quot;country_code&quot;:&quot;EG&quot;,&quot;description&quot;:&quot;The Abyssinian is easy to care for, and a joy to have in your home. They’re affectionate cats and love both people and other animals.&quot;,&quot;life_span&quot;:&quot;14 - 15&quot;,&quot;indoor&quot;:0,&quot;lap&quot;:1,&quot;alt_names&quot;:&quot;&quot;,&quot;adaptability&quot;:5,&quot;affection_level&quot;:5,&quot;child_friendly&quot;:3,&quot;dog_friendly&quot;:4,&quot;energy_level&quot;:5,&quot;grooming&quot;:1,&quot;health_issues&quot;:2,&quot;intelligence&quot;:5,&quot;shedding_level&quot;:2,&quot;social_needs&quot;:5,&quot;stranger_friendly&quot;:5,&quot;vocalisation&quot;:1,&quot;experimental&quot;:0,&quot;hairless&quot;:0,&quot;natural&quot;:1,&quot;rare&quot;:0,&quot;rex&quot;:0,&quot;suppressed_tail&quot;:0,&quot;short_legs&quot;:0,&quot;wikipedia_url&quot;:&quot;https://en.wikipedia.org/wiki/Abyssinian_(cat)&quot;,&quot;hypoallergenic&quot;:0,&quot;reference_image_id&quot;:&quot;0XYvRd7oD&quot;,&quot;image&quot;:{&quot;id&quot;:&quot;0XYvRd7oD&quot;,&quot;width&quot;:1204,&quot;height&quot;:1445,&quot;url&quot;:&quot;https://cdn2.thecatapi.com/images/0XYvRd7oD.jpg&quot;}}]" style="color:#f6f6f4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dracula-soft" style="background-color: #282A36" tabindex="0"><code><span class="line"><span style="color: #F6F6F4">[{</span><span style="color: #DEE492">&quot;</span><span style="color: #E7EE98">weight</span><span style="color: #DEE492">&quot;</span><span style="color: #F286C4">:</span><span style="color: #F6F6F4">{</span><span style="color: #DEE492">&quot;</span><span style="color: #E7EE98">imperial</span><span style="color: #DEE492">&quot;</span><span style="color: #F286C4">:</span><span style="color: #DEE492">&quot;</span><span style="color: #E7EE98">7  -  10</span><span style="color: #DEE492">&quot;</span><span style="color: #F6F6F4">,</span><span style="color: #DEE492">&quot;</span><span style="color: #E7EE98">metric</span><span style="color: #DEE492">&quot;</span><span style="color: #F286C4">:</span><span style="color: #DEE492">&quot;</span><span style="color: #E7EE98">3 - 5</span><span style="color: #DEE492">&quot;</span><span style="color: #F6F6F4">},</span><span style="color: #DEE492">&quot;</span><span style="color: #E7EE98">id</span><span style="color: #DEE492">&quot;</span><span style="color: #F286C4">:</span><span style="color: #DEE492">&quot;</span><span style="color: #E7EE98">abys</span><span style="color: #DEE492">&quot;</span><span style="color: #F6F6F4">,</span><span style="color: #DEE492">&quot;</span><span style="color: #E7EE98">name</span><span style="color: #DEE492">&quot;</span><span style="color: #F286C4">:</span><span style="color: #DEE492">&quot;</span><span style="color: #E7EE98">Abyssinian</span><span style="color: #DEE492">&quot;</span><span style="color: #F6F6F4">,</span><span style="color: #DEE492">&quot;</span><span style="color: #E7EE98">cfa_url</span><span style="color: #DEE492">&quot;</span><span style="color: #F286C4">:</span><span style="color: #DEE492">&quot;</span><span style="color: #E7EE98">http://cfa.org/Breeds/BreedsAB/Abyssinian.aspx</span><span style="color: #DEE492">&quot;</span><span style="color: #F6F6F4">,</span><span style="color: #DEE492">&quot;</span><span style="color: #E7EE98">vetstreet_url</span><span style="color: #DEE492">&quot;</span><span style="color: #F286C4">:</span><span style="color: #DEE492">&quot;</span><span style="color: #E7EE98">http://www.vetstreet.com/cats/abyssinian</span><span style="color: #DEE492">&quot;</span><span style="color: #F6F6F4">,</span><span style="color: #DEE492">&quot;</span><span style="color: #E7EE98">vcahospitals_url</span><span style="color: #DEE492">&quot;</span><span style="color: #F286C4">:</span><span style="color: #DEE492">&quot;</span><span style="color: #E7EE98">https://vcahospitals.com/know-your-pet/cat-breeds/abyssinian</span><span style="color: #DEE492">&quot;</span><span style="color: #F6F6F4">,</span><span style="color: #DEE492">&quot;</span><span style="color: #E7EE98">temperament</span><span style="color: #DEE492">&quot;</span><span style="color: #F286C4">:</span><span style="color: #DEE492">&quot;</span><span style="color: #E7EE98">Active, Energetic, Independent, Intelligent, Gentle</span><span style="color: #DEE492">&quot;</span><span style="color: #F6F6F4">,</span><span style="color: #DEE492">&quot;</span><span style="color: #E7EE98">origin</span><span style="color: #DEE492">&quot;</span><span style="color: #F286C4">:</span><span style="color: #DEE492">&quot;</span><span style="color: #E7EE98">Egypt</span><span style="color: #DEE492">&quot;</span><span style="color: #F6F6F4">,</span><span style="color: #DEE492">&quot;</span><span style="color: #E7EE98">country_codes</span><span style="color: #DEE492">&quot;</span><span style="color: #F286C4">:</span><span style="color: #DEE492">&quot;</span><span style="color: #E7EE98">EG</span><span style="color: #DEE492">&quot;</span><span style="color: #F6F6F4">,</span><span style="color: #DEE492">&quot;</span><span style="color: #E7EE98">country_code</span><span style="color: #DEE492">&quot;</span><span style="color: #F286C4">:</span><span style="color: #DEE492">&quot;</span><span style="color: #E7EE98">EG</span><span style="color: #DEE492">&quot;</span><span style="color: #F6F6F4">,</span><span style="color: #DEE492">&quot;</span><span style="color: #E7EE98">description</span><span style="color: #DEE492">&quot;</span><span style="color: #F286C4">:</span><span style="color: #DEE492">&quot;</span><span style="color: #E7EE98">The Abyssinian is easy to care for, and a joy to have in your home. They’re affectionate cats and love both people and other animals.</span><span style="color: #DEE492">&quot;</span><span style="color: #F6F6F4">,</span><span style="color: #DEE492">&quot;</span><span style="color: #E7EE98">life_span</span><span style="color: #DEE492">&quot;</span><span style="color: #F286C4">:</span><span style="color: #DEE492">&quot;</span><span style="color: #E7EE98">14 - 15</span><span style="color: #DEE492">&quot;</span><span style="color: #F6F6F4">,</span><span style="color: #DEE492">&quot;</span><span style="color: #E7EE98">indoor</span><span style="color: #DEE492">&quot;</span><span style="color: #F286C4">:</span><span style="color: #BF9EEE">0</span><span style="color: #F6F6F4">,</span><span style="color: #DEE492">&quot;</span><span style="color: #E7EE98">lap</span><span style="color: #DEE492">&quot;</span><span style="color: #F286C4">:</span><span style="color: #BF9EEE">1</span><span style="color: #F6F6F4">,</span><span style="color: #DEE492">&quot;</span><span style="color: #E7EE98">alt_names</span><span style="color: #DEE492">&quot;</span><span style="color: #F286C4">:</span><span style="color: #DEE492">&quot;&quot;</span><span style="color: #F6F6F4">,</span><span style="color: #DEE492">&quot;</span><span style="color: #E7EE98">adaptability</span><span style="color: #DEE492">&quot;</span><span style="color: #F286C4">:</span><span style="color: #BF9EEE">5</span><span style="color: #F6F6F4">,</span><span style="color: #DEE492">&quot;</span><span style="color: #E7EE98">affection_level</span><span style="color: #DEE492">&quot;</span><span style="color: #F286C4">:</span><span style="color: #BF9EEE">5</span><span style="color: #F6F6F4">,</span><span style="color: #DEE492">&quot;</span><span style="color: #E7EE98">child_friendly</span><span style="color: #DEE492">&quot;</span><span style="color: #F286C4">:</span><span style="color: #BF9EEE">3</span><span style="color: #F6F6F4">,</span><span style="color: #DEE492">&quot;</span><span style="color: #E7EE98">dog_friendly</span><span style="color: #DEE492">&quot;</span><span style="color: #F286C4">:</span><span style="color: #BF9EEE">4</span><span style="color: #F6F6F4">,</span><span style="color: #DEE492">&quot;</span><span style="color: #E7EE98">energy_level</span><span style="color: #DEE492">&quot;</span><span style="color: #F286C4">:</span><span style="color: #BF9EEE">5</span><span style="color: #F6F6F4">,</span><span style="color: #DEE492">&quot;</span><span style="color: #E7EE98">grooming</span><span style="color: #DEE492">&quot;</span><span style="color: #F286C4">:</span><span style="color: #BF9EEE">1</span><span style="color: #F6F6F4">,</span><span style="color: #DEE492">&quot;</span><span style="color: #E7EE98">health_issues</span><span style="color: #DEE492">&quot;</span><span style="color: #F286C4">:</span><span style="color: #BF9EEE">2</span><span style="color: #F6F6F4">,</span><span style="color: #DEE492">&quot;</span><span style="color: #E7EE98">intelligence</span><span style="color: #DEE492">&quot;</span><span style="color: #F286C4">:</span><span style="color: #BF9EEE">5</span><span style="color: #F6F6F4">,</span><span style="color: #DEE492">&quot;</span><span style="color: #E7EE98">shedding_level</span><span style="color: #DEE492">&quot;</span><span style="color: #F286C4">:</span><span style="color: #BF9EEE">2</span><span style="color: #F6F6F4">,</span><span style="color: #DEE492">&quot;</span><span style="color: #E7EE98">social_needs</span><span style="color: #DEE492">&quot;</span><span style="color: #F286C4">:</span><span style="color: #BF9EEE">5</span><span style="color: #F6F6F4">,</span><span style="color: #DEE492">&quot;</span><span style="color: #E7EE98">stranger_friendly</span><span style="color: #DEE492">&quot;</span><span style="color: #F286C4">:</span><span style="color: #BF9EEE">5</span><span style="color: #F6F6F4">,</span><span style="color: #DEE492">&quot;</span><span style="color: #E7EE98">vocalisation</span><span style="color: #DEE492">&quot;</span><span style="color: #F286C4">:</span><span style="color: #BF9EEE">1</span><span style="color: #F6F6F4">,</span><span style="color: #DEE492">&quot;</span><span style="color: #E7EE98">experimental</span><span style="color: #DEE492">&quot;</span><span style="color: #F286C4">:</span><span style="color: #BF9EEE">0</span><span style="color: #F6F6F4">,</span><span style="color: #DEE492">&quot;</span><span style="color: #E7EE98">hairless</span><span style="color: #DEE492">&quot;</span><span style="color: #F286C4">:</span><span style="color: #BF9EEE">0</span><span style="color: #F6F6F4">,</span><span style="color: #DEE492">&quot;</span><span style="color: #E7EE98">natural</span><span style="color: #DEE492">&quot;</span><span style="color: #F286C4">:</span><span style="color: #BF9EEE">1</span><span style="color: #F6F6F4">,</span><span style="color: #DEE492">&quot;</span><span style="color: #E7EE98">rare</span><span style="color: #DEE492">&quot;</span><span style="color: #F286C4">:</span><span style="color: #BF9EEE">0</span><span style="color: #F6F6F4">,</span><span style="color: #DEE492">&quot;</span><span style="color: #E7EE98">rex</span><span style="color: #DEE492">&quot;</span><span style="color: #F286C4">:</span><span style="color: #BF9EEE">0</span><span style="color: #F6F6F4">,</span><span style="color: #DEE492">&quot;</span><span style="color: #E7EE98">suppressed_tail</span><span style="color: #DEE492">&quot;</span><span style="color: #F286C4">:</span><span style="color: #BF9EEE">0</span><span style="color: #F6F6F4">,</span><span style="color: #DEE492">&quot;</span><span style="color: #E7EE98">short_legs</span><span style="color: #DEE492">&quot;</span><span style="color: #F286C4">:</span><span style="color: #BF9EEE">0</span><span style="color: #F6F6F4">,</span><span style="color: #DEE492">&quot;</span><span style="color: #E7EE98">wikipedia_url</span><span style="color: #DEE492">&quot;</span><span style="color: #F286C4">:</span><span style="color: #DEE492">&quot;</span><span style="color: #E7EE98">https://en.wikipedia.org/wiki/Abyssinian_(cat)</span><span style="color: #DEE492">&quot;</span><span style="color: #F6F6F4">,</span><span style="color: #DEE492">&quot;</span><span style="color: #E7EE98">hypoallergenic</span><span style="color: #DEE492">&quot;</span><span style="color: #F286C4">:</span><span style="color: #BF9EEE">0</span><span style="color: #F6F6F4">,</span><span style="color: #DEE492">&quot;</span><span style="color: #E7EE98">reference_image_id</span><span style="color: #DEE492">&quot;</span><span style="color: #F286C4">:</span><span style="color: #DEE492">&quot;</span><span style="color: #E7EE98">0XYvRd7oD</span><span style="color: #DEE492">&quot;</span><span style="color: #F6F6F4">,</span><span style="color: #DEE492">&quot;</span><span style="color: #E7EE98">image</span><span style="color: #DEE492">&quot;</span><span style="color: #F286C4">:</span><span style="color: #F6F6F4">{</span><span style="color: #DEE492">&quot;</span><span style="color: #E7EE98">id</span><span style="color: #DEE492">&quot;</span><span style="color: #F286C4">:</span><span style="color: #DEE492">&quot;</span><span style="color: #E7EE98">0XYvRd7oD</span><span style="color: #DEE492">&quot;</span><span style="color: #F6F6F4">,</span><span style="color: #DEE492">&quot;</span><span style="color: #E7EE98">width</span><span style="color: #DEE492">&quot;</span><span style="color: #F286C4">:</span><span style="color: #BF9EEE">1204</span><span style="color: #F6F6F4">,</span><span style="color: #DEE492">&quot;</span><span style="color: #E7EE98">height</span><span style="color: #DEE492">&quot;</span><span style="color: #F286C4">:</span><span style="color: #BF9EEE">1445</span><span style="color: #F6F6F4">,</span><span style="color: #DEE492">&quot;</span><span style="color: #E7EE98">url</span><span style="color: #DEE492">&quot;</span><span style="color: #F286C4">:</span><span style="color: #DEE492">&quot;</span><span style="color: #E7EE98">https://cdn2.thecatapi.com/images/0XYvRd7oD.jpg</span><span style="color: #DEE492">&quot;</span><span style="color: #F6F6F4">}}]</span></span></code></pre></div>



<div style="height:21px" aria-hidden="true" class="wp-block-spacer"></div>



<p>It is just a long text based data format. To make this more readable, you can use pretty print JSON in Swift like so:</p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono-NL.ttf" style="font-size:1rem;font-family:Code-Pro-JetBrains-Mono-NL,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span role="button" tabindex="0" data-code="if let data = try? JSONSerialization.data(withJSONObject: jsonFileData, options: .prettyPrinted) {
    if let prettyPrinted = String(data: data, encoding: .utf8) {
        print(prettyPrinted)
    }
}" style="color:#f6f6f4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dracula-soft" style="background-color: #282A36" tabindex="0"><code><span class="line"><span style="color: #F286C4">if</span><span style="color: #F6F6F4"> </span><span style="color: #F286C4">let</span><span style="color: #F6F6F4"> data </span><span style="color: #F286C4">=</span><span style="color: #F6F6F4"> </span><span style="color: #F286C4">try?</span><span style="color: #F6F6F4"> JSONSerialization.</span><span style="color: #97E1F1">data</span><span style="color: #F6F6F4">(</span><span style="color: #97E1F1">withJSONObject</span><span style="color: #F6F6F4">: jsonFileData, </span><span style="color: #97E1F1">options</span><span style="color: #F6F6F4">: .prettyPrinted) {</span></span>
<span class="line"><span style="color: #F6F6F4">    </span><span style="color: #F286C4">if</span><span style="color: #F6F6F4"> </span><span style="color: #F286C4">let</span><span style="color: #F6F6F4"> prettyPrinted </span><span style="color: #F286C4">=</span><span style="color: #F6F6F4"> </span><span style="color: #97E1F1; font-style: italic">String</span><span style="color: #F6F6F4">(</span><span style="color: #97E1F1">data</span><span style="color: #F6F6F4">: data, </span><span style="color: #97E1F1">encoding</span><span style="color: #F6F6F4">: .</span><span style="color: #BF9EEE">utf8</span><span style="color: #F6F6F4">) {</span></span>
<span class="line"><span style="color: #F6F6F4">        </span><span style="color: #97E1F1">print</span><span style="color: #F6F6F4">(prettyPrinted)</span></span>
<span class="line"><span style="color: #F6F6F4">    }</span></span>
<span class="line"><span style="color: #F6F6F4">}</span></span></code></pre></div>



<div style="height:21px" aria-hidden="true" class="wp-block-spacer"></div>



<p>This will give you a more readable output:</p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono-NL.ttf" style="font-size:1rem;font-family:Code-Pro-JetBrains-Mono-NL,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span role="button" tabindex="0" data-code="[
    {
        &quot;weight&quot;: {
            &quot;imperial&quot;: &quot;7 - 10&quot;,
            &quot;metric&quot;: &quot;3 - 5&quot;
        },
        &quot;id&quot;: &quot;abys&quot;,
        &quot;name&quot;: &quot;Abyssinian&quot;,
        &quot;cfa_url&quot;: &quot;http://cfa.org/Breeds/BreedsAB/Abyssinian.aspx&quot;,
        &quot;vetstreet_url&quot;: &quot;http://www.vetstreet.com/cats/abyssinian&quot;,
        &quot;vcahospitals_url&quot;: &quot;https://vcahospitals.com/know-your-pet/cat-breeds/abyssinian&quot;,
        &quot;temperament&quot;: &quot;Active, Energetic, Independent, Intelligent, Gentle&quot;,
        &quot;origin&quot;: &quot;Egypt&quot;,
        &quot;country_codes&quot;: &quot;EG&quot;,
        &quot;country_code&quot;: &quot;EG&quot;,
        &quot;description&quot;: &quot;The Abyssinian is easy to care for, and a joy to have in your home. They’re affectionate cats and love both people and other animals.&quot;,
        &quot;life_span&quot;: &quot;14 - 15&quot;,
        &quot;indoor&quot;: 0,
        &quot;lap&quot;: 1,
        &quot;alt_names&quot;: &quot;&quot;,
        &quot;adaptability&quot;: 5,
        &quot;affection_level&quot;: 5,
        &quot;child_friendly&quot;: 3,
        &quot;dog_friendly&quot;: 4,
        &quot;energy_level&quot;: 5,
        &quot;grooming&quot;: 1,
        &quot;health_issues&quot;: 2,
        &quot;intelligence&quot;: 5,
        &quot;shedding_level&quot;: 2,
        &quot;social_needs&quot;: 5,
        &quot;stranger_friendly&quot;: 5,
        &quot;vocalisation&quot;: 1,
        &quot;experimental&quot;: 0,
        &quot;hairless&quot;: 0,
        &quot;natural&quot;: 1,
        &quot;rare&quot;: 0,
        &quot;rex&quot;: 0,
        &quot;suppressed_tail&quot;: 0,
        &quot;short_legs&quot;: 0,
        &quot;wikipedia_url&quot;: &quot;https://en.wikipedia.org/wiki/Abyssinian_(cat)&quot;,
        &quot;hypoallergenic&quot;: 0,
        &quot;reference_image_id&quot;: &quot;0XYvRd7oD&quot;,
        &quot;image&quot;: {
            &quot;id&quot;: &quot;0XYvRd7oD&quot;,
            &quot;width&quot;: 1204,
            &quot;height&quot;: 1445,
            &quot;url&quot;: &quot;https://cdn2.thecatapi.com/images/0XYvRd7oD.jpg&quot;
        }
    }
]" style="color:#f6f6f4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dracula-soft" style="background-color: #282A36" tabindex="0"><code><span class="line"><span style="color: #F6F6F4">[</span></span>
<span class="line"><span style="color: #F6F6F4">    {</span></span>
<span class="line"><span style="color: #F6F6F4">        </span><span style="color: #DEE492">&quot;</span><span style="color: #E7EE98">weight</span><span style="color: #DEE492">&quot;</span><span style="color: #F286C4">:</span><span style="color: #F6F6F4"> {</span></span>
<span class="line"><span style="color: #F6F6F4">            </span><span style="color: #DEE492">&quot;</span><span style="color: #E7EE98">imperial</span><span style="color: #DEE492">&quot;</span><span style="color: #F286C4">:</span><span style="color: #F6F6F4"> </span><span style="color: #DEE492">&quot;</span><span style="color: #E7EE98">7 - 10</span><span style="color: #DEE492">&quot;</span><span style="color: #F6F6F4">,</span></span>
<span class="line"><span style="color: #F6F6F4">            </span><span style="color: #DEE492">&quot;</span><span style="color: #E7EE98">metric</span><span style="color: #DEE492">&quot;</span><span style="color: #F286C4">:</span><span style="color: #F6F6F4"> </span><span style="color: #DEE492">&quot;</span><span style="color: #E7EE98">3 - 5</span><span style="color: #DEE492">&quot;</span></span>
<span class="line"><span style="color: #F6F6F4">        },</span></span>
<span class="line"><span style="color: #F6F6F4">        </span><span style="color: #DEE492">&quot;</span><span style="color: #E7EE98">id</span><span style="color: #DEE492">&quot;</span><span style="color: #F286C4">:</span><span style="color: #F6F6F4"> </span><span style="color: #DEE492">&quot;</span><span style="color: #E7EE98">abys</span><span style="color: #DEE492">&quot;</span><span style="color: #F6F6F4">,</span></span>
<span class="line"><span style="color: #F6F6F4">        </span><span style="color: #DEE492">&quot;</span><span style="color: #E7EE98">name</span><span style="color: #DEE492">&quot;</span><span style="color: #F286C4">:</span><span style="color: #F6F6F4"> </span><span style="color: #DEE492">&quot;</span><span style="color: #E7EE98">Abyssinian</span><span style="color: #DEE492">&quot;</span><span style="color: #F6F6F4">,</span></span>
<span class="line"><span style="color: #F6F6F4">        </span><span style="color: #DEE492">&quot;</span><span style="color: #E7EE98">cfa_url</span><span style="color: #DEE492">&quot;</span><span style="color: #F286C4">:</span><span style="color: #F6F6F4"> </span><span style="color: #DEE492">&quot;</span><span style="color: #E7EE98">http://cfa.org/Breeds/BreedsAB/Abyssinian.aspx</span><span style="color: #DEE492">&quot;</span><span style="color: #F6F6F4">,</span></span>
<span class="line"><span style="color: #F6F6F4">        </span><span style="color: #DEE492">&quot;</span><span style="color: #E7EE98">vetstreet_url</span><span style="color: #DEE492">&quot;</span><span style="color: #F286C4">:</span><span style="color: #F6F6F4"> </span><span style="color: #DEE492">&quot;</span><span style="color: #E7EE98">http://www.vetstreet.com/cats/abyssinian</span><span style="color: #DEE492">&quot;</span><span style="color: #F6F6F4">,</span></span>
<span class="line"><span style="color: #F6F6F4">        </span><span style="color: #DEE492">&quot;</span><span style="color: #E7EE98">vcahospitals_url</span><span style="color: #DEE492">&quot;</span><span style="color: #F286C4">:</span><span style="color: #F6F6F4"> </span><span style="color: #DEE492">&quot;</span><span style="color: #E7EE98">https://vcahospitals.com/know-your-pet/cat-breeds/abyssinian</span><span style="color: #DEE492">&quot;</span><span style="color: #F6F6F4">,</span></span>
<span class="line"><span style="color: #F6F6F4">        </span><span style="color: #DEE492">&quot;</span><span style="color: #E7EE98">temperament</span><span style="color: #DEE492">&quot;</span><span style="color: #F286C4">:</span><span style="color: #F6F6F4"> </span><span style="color: #DEE492">&quot;</span><span style="color: #E7EE98">Active, Energetic, Independent, Intelligent, Gentle</span><span style="color: #DEE492">&quot;</span><span style="color: #F6F6F4">,</span></span>
<span class="line"><span style="color: #F6F6F4">        </span><span style="color: #DEE492">&quot;</span><span style="color: #E7EE98">origin</span><span style="color: #DEE492">&quot;</span><span style="color: #F286C4">:</span><span style="color: #F6F6F4"> </span><span style="color: #DEE492">&quot;</span><span style="color: #E7EE98">Egypt</span><span style="color: #DEE492">&quot;</span><span style="color: #F6F6F4">,</span></span>
<span class="line"><span style="color: #F6F6F4">        </span><span style="color: #DEE492">&quot;</span><span style="color: #E7EE98">country_codes</span><span style="color: #DEE492">&quot;</span><span style="color: #F286C4">:</span><span style="color: #F6F6F4"> </span><span style="color: #DEE492">&quot;</span><span style="color: #E7EE98">EG</span><span style="color: #DEE492">&quot;</span><span style="color: #F6F6F4">,</span></span>
<span class="line"><span style="color: #F6F6F4">        </span><span style="color: #DEE492">&quot;</span><span style="color: #E7EE98">country_code</span><span style="color: #DEE492">&quot;</span><span style="color: #F286C4">:</span><span style="color: #F6F6F4"> </span><span style="color: #DEE492">&quot;</span><span style="color: #E7EE98">EG</span><span style="color: #DEE492">&quot;</span><span style="color: #F6F6F4">,</span></span>
<span class="line"><span style="color: #F6F6F4">        </span><span style="color: #DEE492">&quot;</span><span style="color: #E7EE98">description</span><span style="color: #DEE492">&quot;</span><span style="color: #F286C4">:</span><span style="color: #F6F6F4"> </span><span style="color: #DEE492">&quot;</span><span style="color: #E7EE98">The Abyssinian is easy to care for, and a joy to have in your home. They’re affectionate cats and love both people and other animals.</span><span style="color: #DEE492">&quot;</span><span style="color: #F6F6F4">,</span></span>
<span class="line"><span style="color: #F6F6F4">        </span><span style="color: #DEE492">&quot;</span><span style="color: #E7EE98">life_span</span><span style="color: #DEE492">&quot;</span><span style="color: #F286C4">:</span><span style="color: #F6F6F4"> </span><span style="color: #DEE492">&quot;</span><span style="color: #E7EE98">14 - 15</span><span style="color: #DEE492">&quot;</span><span style="color: #F6F6F4">,</span></span>
<span class="line"><span style="color: #F6F6F4">        </span><span style="color: #DEE492">&quot;</span><span style="color: #E7EE98">indoor</span><span style="color: #DEE492">&quot;</span><span style="color: #F286C4">:</span><span style="color: #F6F6F4"> </span><span style="color: #BF9EEE">0</span><span style="color: #F6F6F4">,</span></span>
<span class="line"><span style="color: #F6F6F4">        </span><span style="color: #DEE492">&quot;</span><span style="color: #E7EE98">lap</span><span style="color: #DEE492">&quot;</span><span style="color: #F286C4">:</span><span style="color: #F6F6F4"> </span><span style="color: #BF9EEE">1</span><span style="color: #F6F6F4">,</span></span>
<span class="line"><span style="color: #F6F6F4">        </span><span style="color: #DEE492">&quot;</span><span style="color: #E7EE98">alt_names</span><span style="color: #DEE492">&quot;</span><span style="color: #F286C4">:</span><span style="color: #F6F6F4"> </span><span style="color: #DEE492">&quot;&quot;</span><span style="color: #F6F6F4">,</span></span>
<span class="line"><span style="color: #F6F6F4">        </span><span style="color: #DEE492">&quot;</span><span style="color: #E7EE98">adaptability</span><span style="color: #DEE492">&quot;</span><span style="color: #F286C4">:</span><span style="color: #F6F6F4"> </span><span style="color: #BF9EEE">5</span><span style="color: #F6F6F4">,</span></span>
<span class="line"><span style="color: #F6F6F4">        </span><span style="color: #DEE492">&quot;</span><span style="color: #E7EE98">affection_level</span><span style="color: #DEE492">&quot;</span><span style="color: #F286C4">:</span><span style="color: #F6F6F4"> </span><span style="color: #BF9EEE">5</span><span style="color: #F6F6F4">,</span></span>
<span class="line"><span style="color: #F6F6F4">        </span><span style="color: #DEE492">&quot;</span><span style="color: #E7EE98">child_friendly</span><span style="color: #DEE492">&quot;</span><span style="color: #F286C4">:</span><span style="color: #F6F6F4"> </span><span style="color: #BF9EEE">3</span><span style="color: #F6F6F4">,</span></span>
<span class="line"><span style="color: #F6F6F4">        </span><span style="color: #DEE492">&quot;</span><span style="color: #E7EE98">dog_friendly</span><span style="color: #DEE492">&quot;</span><span style="color: #F286C4">:</span><span style="color: #F6F6F4"> </span><span style="color: #BF9EEE">4</span><span style="color: #F6F6F4">,</span></span>
<span class="line"><span style="color: #F6F6F4">        </span><span style="color: #DEE492">&quot;</span><span style="color: #E7EE98">energy_level</span><span style="color: #DEE492">&quot;</span><span style="color: #F286C4">:</span><span style="color: #F6F6F4"> </span><span style="color: #BF9EEE">5</span><span style="color: #F6F6F4">,</span></span>
<span class="line"><span style="color: #F6F6F4">        </span><span style="color: #DEE492">&quot;</span><span style="color: #E7EE98">grooming</span><span style="color: #DEE492">&quot;</span><span style="color: #F286C4">:</span><span style="color: #F6F6F4"> </span><span style="color: #BF9EEE">1</span><span style="color: #F6F6F4">,</span></span>
<span class="line"><span style="color: #F6F6F4">        </span><span style="color: #DEE492">&quot;</span><span style="color: #E7EE98">health_issues</span><span style="color: #DEE492">&quot;</span><span style="color: #F286C4">:</span><span style="color: #F6F6F4"> </span><span style="color: #BF9EEE">2</span><span style="color: #F6F6F4">,</span></span>
<span class="line"><span style="color: #F6F6F4">        </span><span style="color: #DEE492">&quot;</span><span style="color: #E7EE98">intelligence</span><span style="color: #DEE492">&quot;</span><span style="color: #F286C4">:</span><span style="color: #F6F6F4"> </span><span style="color: #BF9EEE">5</span><span style="color: #F6F6F4">,</span></span>
<span class="line"><span style="color: #F6F6F4">        </span><span style="color: #DEE492">&quot;</span><span style="color: #E7EE98">shedding_level</span><span style="color: #DEE492">&quot;</span><span style="color: #F286C4">:</span><span style="color: #F6F6F4"> </span><span style="color: #BF9EEE">2</span><span style="color: #F6F6F4">,</span></span>
<span class="line"><span style="color: #F6F6F4">        </span><span style="color: #DEE492">&quot;</span><span style="color: #E7EE98">social_needs</span><span style="color: #DEE492">&quot;</span><span style="color: #F286C4">:</span><span style="color: #F6F6F4"> </span><span style="color: #BF9EEE">5</span><span style="color: #F6F6F4">,</span></span>
<span class="line"><span style="color: #F6F6F4">        </span><span style="color: #DEE492">&quot;</span><span style="color: #E7EE98">stranger_friendly</span><span style="color: #DEE492">&quot;</span><span style="color: #F286C4">:</span><span style="color: #F6F6F4"> </span><span style="color: #BF9EEE">5</span><span style="color: #F6F6F4">,</span></span>
<span class="line"><span style="color: #F6F6F4">        </span><span style="color: #DEE492">&quot;</span><span style="color: #E7EE98">vocalisation</span><span style="color: #DEE492">&quot;</span><span style="color: #F286C4">:</span><span style="color: #F6F6F4"> </span><span style="color: #BF9EEE">1</span><span style="color: #F6F6F4">,</span></span>
<span class="line"><span style="color: #F6F6F4">        </span><span style="color: #DEE492">&quot;</span><span style="color: #E7EE98">experimental</span><span style="color: #DEE492">&quot;</span><span style="color: #F286C4">:</span><span style="color: #F6F6F4"> </span><span style="color: #BF9EEE">0</span><span style="color: #F6F6F4">,</span></span>
<span class="line"><span style="color: #F6F6F4">        </span><span style="color: #DEE492">&quot;</span><span style="color: #E7EE98">hairless</span><span style="color: #DEE492">&quot;</span><span style="color: #F286C4">:</span><span style="color: #F6F6F4"> </span><span style="color: #BF9EEE">0</span><span style="color: #F6F6F4">,</span></span>
<span class="line"><span style="color: #F6F6F4">        </span><span style="color: #DEE492">&quot;</span><span style="color: #E7EE98">natural</span><span style="color: #DEE492">&quot;</span><span style="color: #F286C4">:</span><span style="color: #F6F6F4"> </span><span style="color: #BF9EEE">1</span><span style="color: #F6F6F4">,</span></span>
<span class="line"><span style="color: #F6F6F4">        </span><span style="color: #DEE492">&quot;</span><span style="color: #E7EE98">rare</span><span style="color: #DEE492">&quot;</span><span style="color: #F286C4">:</span><span style="color: #F6F6F4"> </span><span style="color: #BF9EEE">0</span><span style="color: #F6F6F4">,</span></span>
<span class="line"><span style="color: #F6F6F4">        </span><span style="color: #DEE492">&quot;</span><span style="color: #E7EE98">rex</span><span style="color: #DEE492">&quot;</span><span style="color: #F286C4">:</span><span style="color: #F6F6F4"> </span><span style="color: #BF9EEE">0</span><span style="color: #F6F6F4">,</span></span>
<span class="line"><span style="color: #F6F6F4">        </span><span style="color: #DEE492">&quot;</span><span style="color: #E7EE98">suppressed_tail</span><span style="color: #DEE492">&quot;</span><span style="color: #F286C4">:</span><span style="color: #F6F6F4"> </span><span style="color: #BF9EEE">0</span><span style="color: #F6F6F4">,</span></span>
<span class="line"><span style="color: #F6F6F4">        </span><span style="color: #DEE492">&quot;</span><span style="color: #E7EE98">short_legs</span><span style="color: #DEE492">&quot;</span><span style="color: #F286C4">:</span><span style="color: #F6F6F4"> </span><span style="color: #BF9EEE">0</span><span style="color: #F6F6F4">,</span></span>
<span class="line"><span style="color: #F6F6F4">        </span><span style="color: #DEE492">&quot;</span><span style="color: #E7EE98">wikipedia_url</span><span style="color: #DEE492">&quot;</span><span style="color: #F286C4">:</span><span style="color: #F6F6F4"> </span><span style="color: #DEE492">&quot;</span><span style="color: #E7EE98">https://en.wikipedia.org/wiki/Abyssinian_(cat)</span><span style="color: #DEE492">&quot;</span><span style="color: #F6F6F4">,</span></span>
<span class="line"><span style="color: #F6F6F4">        </span><span style="color: #DEE492">&quot;</span><span style="color: #E7EE98">hypoallergenic</span><span style="color: #DEE492">&quot;</span><span style="color: #F286C4">:</span><span style="color: #F6F6F4"> </span><span style="color: #BF9EEE">0</span><span style="color: #F6F6F4">,</span></span>
<span class="line"><span style="color: #F6F6F4">        </span><span style="color: #DEE492">&quot;</span><span style="color: #E7EE98">reference_image_id</span><span style="color: #DEE492">&quot;</span><span style="color: #F286C4">:</span><span style="color: #F6F6F4"> </span><span style="color: #DEE492">&quot;</span><span style="color: #E7EE98">0XYvRd7oD</span><span style="color: #DEE492">&quot;</span><span style="color: #F6F6F4">,</span></span>
<span class="line"><span style="color: #F6F6F4">        </span><span style="color: #DEE492">&quot;</span><span style="color: #E7EE98">image</span><span style="color: #DEE492">&quot;</span><span style="color: #F286C4">:</span><span style="color: #F6F6F4"> {</span></span>
<span class="line"><span style="color: #F6F6F4">            </span><span style="color: #DEE492">&quot;</span><span style="color: #E7EE98">id</span><span style="color: #DEE492">&quot;</span><span style="color: #F286C4">:</span><span style="color: #F6F6F4"> </span><span style="color: #DEE492">&quot;</span><span style="color: #E7EE98">0XYvRd7oD</span><span style="color: #DEE492">&quot;</span><span style="color: #F6F6F4">,</span></span>
<span class="line"><span style="color: #F6F6F4">            </span><span style="color: #DEE492">&quot;</span><span style="color: #E7EE98">width</span><span style="color: #DEE492">&quot;</span><span style="color: #F286C4">:</span><span style="color: #F6F6F4"> </span><span style="color: #BF9EEE">1204</span><span style="color: #F6F6F4">,</span></span>
<span class="line"><span style="color: #F6F6F4">            </span><span style="color: #DEE492">&quot;</span><span style="color: #E7EE98">height</span><span style="color: #DEE492">&quot;</span><span style="color: #F286C4">:</span><span style="color: #F6F6F4"> </span><span style="color: #BF9EEE">1445</span><span style="color: #F6F6F4">,</span></span>
<span class="line"><span style="color: #F6F6F4">            </span><span style="color: #DEE492">&quot;</span><span style="color: #E7EE98">url</span><span style="color: #DEE492">&quot;</span><span style="color: #F286C4">:</span><span style="color: #F6F6F4"> </span><span style="color: #DEE492">&quot;</span><span style="color: #E7EE98">https://cdn2.thecatapi.com/images/0XYvRd7oD.jpg</span><span style="color: #DEE492">&quot;</span></span>
<span class="line"><span style="color: #F6F6F4">        }</span></span>
<span class="line"><span style="color: #F6F6F4">    }</span></span>
<span class="line"><span style="color: #F6F6F4">]</span></span></code></pre></div>



<div style="height:21px" aria-hidden="true" class="wp-block-spacer"></div>



<p>This JSON object represents a single cat breed, the Abyssinian, along with various details about the breed. As you can see, the JSON structure is a nested combination of arrays and dictionaries (objects), which is a common structure for JSON data. The <a href="https://www.swiftyplace.com/blog/codable-how-to-simplify-converting-json-data-to-swift-objects-and-vice-versa" data-type="link" data-id="https://www.swiftyplace.com/blog/codable-how-to-simplify-converting-json-data-to-swift-objects-and-vice-versa">Codable protocol in Swift</a> can handle such nested structures with ease, which is one of its major advantages.</p>



<p>Here is how we could transfer this data into a Swift data type:</p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono-NL.ttf" style="font-size:1rem;font-family:Code-Pro-JetBrains-Mono-NL,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span role="button" tabindex="0" data-code="struct Breed: Codable, CustomStringConvertible, Identifiable {
    let id: String
    let name: String
    let temperament: String
    let breedExplaination: String
    let energyLevel: Int
    let isHairless: Bool
    let image: BreedImage?

    var description: String {
        return &quot;breed with name: \(name) and id \(id), energy level: \(energyLevel) isHairless: \(isHairless ? &quot;YES&quot; : &quot;NO&quot;)&quot;
    }

    enum CodingKeys: String, CodingKey {
        case id
        case name
        case temperament
        case breedExplaination = &quot;description&quot;
        case energyLevel = &quot;energy_level&quot;
        case isHairless = &quot;hairless&quot;
        case image
    }

    public init(from decoder: Decoder) throws {
        let values = try decoder.container(keyedBy: CodingKeys.self)

        id = try values.decode(String.self, forKey: .id)
        name = try values.decode(String.self, forKey: .name)
        temperament = try values.decode(String.self, forKey: .temperament)
        breedExplaination = try values.decode(String.self, forKey: .breedExplaination)
        energyLevel = try values.decode(Int.self, forKey: .energyLevel)

        let hairless = try values.decode(Int.self, forKey: .isHairless)
        isHairless = hairless == 1

        image = try values.decodeIfPresent(BreedImage.self, forKey: .image)
    }
}" style="color:#f6f6f4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dracula-soft" style="background-color: #282A36" tabindex="0"><code><span class="line"><span style="color: #F286C4">struct</span><span style="color: #F6F6F4"> </span><span style="color: #97E1F1; font-style: italic">Breed</span><span style="color: #F6F6F4">: Codable, CustomStringConvertible, Identifiable {</span></span>
<span class="line"><span style="color: #F6F6F4">    </span><span style="color: #F286C4">let</span><span style="color: #F6F6F4"> id: </span><span style="color: #97E1F1; font-style: italic">String</span></span>
<span class="line"><span style="color: #F6F6F4">    </span><span style="color: #F286C4">let</span><span style="color: #F6F6F4"> name: </span><span style="color: #97E1F1; font-style: italic">String</span></span>
<span class="line"><span style="color: #F6F6F4">    </span><span style="color: #F286C4">let</span><span style="color: #F6F6F4"> temperament: </span><span style="color: #97E1F1; font-style: italic">String</span></span>
<span class="line"><span style="color: #F6F6F4">    </span><span style="color: #F286C4">let</span><span style="color: #F6F6F4"> breedExplaination: </span><span style="color: #97E1F1; font-style: italic">String</span></span>
<span class="line"><span style="color: #F6F6F4">    </span><span style="color: #F286C4">let</span><span style="color: #F6F6F4"> energyLevel: </span><span style="color: #97E1F1; font-style: italic">Int</span></span>
<span class="line"><span style="color: #F6F6F4">    </span><span style="color: #F286C4">let</span><span style="color: #F6F6F4"> isHairless: </span><span style="color: #97E1F1; font-style: italic">Bool</span></span>
<span class="line"><span style="color: #F6F6F4">    </span><span style="color: #F286C4">let</span><span style="color: #F6F6F4"> image: BreedImage</span><span style="color: #F286C4">?</span></span>
<span class="line"></span>
<span class="line"><span style="color: #F6F6F4">    </span><span style="color: #F286C4">var</span><span style="color: #F6F6F4"> description: </span><span style="color: #97E1F1; font-style: italic">String</span><span style="color: #F6F6F4"> {</span></span>
<span class="line"><span style="color: #F6F6F4">        </span><span style="color: #F286C4">return</span><span style="color: #F6F6F4"> </span><span style="color: #DEE492">&quot;</span><span style="color: #E7EE98">breed with name: </span><span style="color: #F286C4">\(</span><span style="color: #E7EE98">name</span><span style="color: #F286C4">)</span><span style="color: #E7EE98"> and id </span><span style="color: #F286C4">\(</span><span style="color: #E7EE98">id</span><span style="color: #F286C4">)</span><span style="color: #E7EE98">, energy level: </span><span style="color: #F286C4">\(</span><span style="color: #E7EE98">energyLevel</span><span style="color: #F286C4">)</span><span style="color: #E7EE98"> isHairless: </span><span style="color: #F286C4">\(</span><span style="color: #E7EE98">isHairless </span><span style="color: #F286C4">?</span><span style="color: #E7EE98"> </span><span style="color: #DEE492">&quot;</span><span style="color: #E7EE98">YES</span><span style="color: #DEE492">&quot;</span><span style="color: #E7EE98"> </span><span style="color: #F286C4">:</span><span style="color: #E7EE98"> </span><span style="color: #DEE492">&quot;</span><span style="color: #E7EE98">NO</span><span style="color: #DEE492">&quot;</span><span style="color: #F286C4">)</span><span style="color: #DEE492">&quot;</span></span>
<span class="line"><span style="color: #F6F6F4">    }</span></span>
<span class="line"></span>
<span class="line"><span style="color: #F6F6F4">    </span><span style="color: #F286C4">enum</span><span style="color: #F6F6F4"> </span><span style="color: #97E1F1; font-style: italic">CodingKeys</span><span style="color: #F6F6F4">: String, CodingKey {</span></span>
<span class="line"><span style="color: #F6F6F4">        </span><span style="color: #F286C4">case</span><span style="color: #F6F6F4"> id</span></span>
<span class="line"><span style="color: #F6F6F4">        </span><span style="color: #F286C4">case</span><span style="color: #F6F6F4"> name</span></span>
<span class="line"><span style="color: #F6F6F4">        </span><span style="color: #F286C4">case</span><span style="color: #F6F6F4"> temperament</span></span>
<span class="line"><span style="color: #F6F6F4">        </span><span style="color: #F286C4">case</span><span style="color: #F6F6F4"> breedExplaination </span><span style="color: #F286C4">=</span><span style="color: #F6F6F4"> </span><span style="color: #DEE492">&quot;</span><span style="color: #E7EE98">description</span><span style="color: #DEE492">&quot;</span></span>
<span class="line"><span style="color: #F6F6F4">        </span><span style="color: #F286C4">case</span><span style="color: #F6F6F4"> energyLevel </span><span style="color: #F286C4">=</span><span style="color: #F6F6F4"> </span><span style="color: #DEE492">&quot;</span><span style="color: #E7EE98">energy_level</span><span style="color: #DEE492">&quot;</span></span>
<span class="line"><span style="color: #F6F6F4">        </span><span style="color: #F286C4">case</span><span style="color: #F6F6F4"> isHairless </span><span style="color: #F286C4">=</span><span style="color: #F6F6F4"> </span><span style="color: #DEE492">&quot;</span><span style="color: #E7EE98">hairless</span><span style="color: #DEE492">&quot;</span></span>
<span class="line"><span style="color: #F6F6F4">        </span><span style="color: #F286C4">case</span><span style="color: #F6F6F4"> image</span></span>
<span class="line"><span style="color: #F6F6F4">    }</span></span>
<span class="line"></span>
<span class="line"><span style="color: #F6F6F4">    </span><span style="color: #F286C4">public</span><span style="color: #F6F6F4"> </span><span style="color: #F286C4">init</span><span style="color: #F6F6F4">(</span><span style="color: #62E884">from</span><span style="color: #F6F6F4"> </span><span style="color: #FFB86C; font-style: italic">decoder</span><span style="color: #F6F6F4">: Decoder) </span><span style="color: #F286C4">throws</span><span style="color: #F6F6F4"> {</span></span>
<span class="line"><span style="color: #F6F6F4">        </span><span style="color: #F286C4">let</span><span style="color: #F6F6F4"> values </span><span style="color: #F286C4">=</span><span style="color: #F6F6F4"> </span><span style="color: #F286C4">try</span><span style="color: #F6F6F4"> decoder.</span><span style="color: #97E1F1">container</span><span style="color: #F6F6F4">(</span><span style="color: #97E1F1">keyedBy</span><span style="color: #F6F6F4">: CodingKeys.</span><span style="color: #F286C4">self</span><span style="color: #F6F6F4">)</span></span>
<span class="line"></span>
<span class="line"><span style="color: #F6F6F4">        id </span><span style="color: #F286C4">=</span><span style="color: #F6F6F4"> </span><span style="color: #F286C4">try</span><span style="color: #F6F6F4"> values.</span><span style="color: #97E1F1">decode</span><span style="color: #F6F6F4">(</span><span style="color: #97E1F1; font-style: italic">String</span><span style="color: #F6F6F4">.</span><span style="color: #F286C4">self</span><span style="color: #F6F6F4">, </span><span style="color: #97E1F1">forKey</span><span style="color: #F6F6F4">: .id)</span></span>
<span class="line"><span style="color: #F6F6F4">        name </span><span style="color: #F286C4">=</span><span style="color: #F6F6F4"> </span><span style="color: #F286C4">try</span><span style="color: #F6F6F4"> values.</span><span style="color: #97E1F1">decode</span><span style="color: #F6F6F4">(</span><span style="color: #97E1F1; font-style: italic">String</span><span style="color: #F6F6F4">.</span><span style="color: #F286C4">self</span><span style="color: #F6F6F4">, </span><span style="color: #97E1F1">forKey</span><span style="color: #F6F6F4">: .name)</span></span>
<span class="line"><span style="color: #F6F6F4">        temperament </span><span style="color: #F286C4">=</span><span style="color: #F6F6F4"> </span><span style="color: #F286C4">try</span><span style="color: #F6F6F4"> values.</span><span style="color: #97E1F1">decode</span><span style="color: #F6F6F4">(</span><span style="color: #97E1F1; font-style: italic">String</span><span style="color: #F6F6F4">.</span><span style="color: #F286C4">self</span><span style="color: #F6F6F4">, </span><span style="color: #97E1F1">forKey</span><span style="color: #F6F6F4">: .temperament)</span></span>
<span class="line"><span style="color: #F6F6F4">        breedExplaination </span><span style="color: #F286C4">=</span><span style="color: #F6F6F4"> </span><span style="color: #F286C4">try</span><span style="color: #F6F6F4"> values.</span><span style="color: #97E1F1">decode</span><span style="color: #F6F6F4">(</span><span style="color: #97E1F1; font-style: italic">String</span><span style="color: #F6F6F4">.</span><span style="color: #F286C4">self</span><span style="color: #F6F6F4">, </span><span style="color: #97E1F1">forKey</span><span style="color: #F6F6F4">: .breedExplaination)</span></span>
<span class="line"><span style="color: #F6F6F4">        energyLevel </span><span style="color: #F286C4">=</span><span style="color: #F6F6F4"> </span><span style="color: #F286C4">try</span><span style="color: #F6F6F4"> values.</span><span style="color: #97E1F1">decode</span><span style="color: #F6F6F4">(</span><span style="color: #97E1F1; font-style: italic">Int</span><span style="color: #F6F6F4">.</span><span style="color: #F286C4">self</span><span style="color: #F6F6F4">, </span><span style="color: #97E1F1">forKey</span><span style="color: #F6F6F4">: .energyLevel)</span></span>
<span class="line"></span>
<span class="line"><span style="color: #F6F6F4">        </span><span style="color: #F286C4">let</span><span style="color: #F6F6F4"> hairless </span><span style="color: #F286C4">=</span><span style="color: #F6F6F4"> </span><span style="color: #F286C4">try</span><span style="color: #F6F6F4"> values.</span><span style="color: #97E1F1">decode</span><span style="color: #F6F6F4">(</span><span style="color: #97E1F1; font-style: italic">Int</span><span style="color: #F6F6F4">.</span><span style="color: #F286C4">self</span><span style="color: #F6F6F4">, </span><span style="color: #97E1F1">forKey</span><span style="color: #F6F6F4">: .isHairless)</span></span>
<span class="line"><span style="color: #F6F6F4">        isHairless </span><span style="color: #F286C4">=</span><span style="color: #F6F6F4"> hairless </span><span style="color: #F286C4">==</span><span style="color: #F6F6F4"> </span><span style="color: #BF9EEE">1</span></span>
<span class="line"></span>
<span class="line"><span style="color: #F6F6F4">        image </span><span style="color: #F286C4">=</span><span style="color: #F6F6F4"> </span><span style="color: #F286C4">try</span><span style="color: #F6F6F4"> values.</span><span style="color: #97E1F1">decodeIfPresent</span><span style="color: #F6F6F4">(BreedImage.</span><span style="color: #F286C4">self</span><span style="color: #F6F6F4">, </span><span style="color: #97E1F1">forKey</span><span style="color: #F6F6F4">: .</span><span style="color: #BF9EEE">image</span><span style="color: #F6F6F4">)</span></span>
<span class="line"><span style="color: #F6F6F4">    }</span></span>
<span class="line"><span style="color: #F6F6F4">}</span></span></code></pre></div>



<div style="height:21px" aria-hidden="true" class="wp-block-spacer"></div>



<p>I am not using all the properties that are included in the JSON file. I only pick the ones that I want to use for my app.</p>



<p>This <strong>Breed</strong> custom type is a bit more complex than our <strong>ReadingData</strong> struct from the above example. It has more properties, and it uses a few techniques that we haven&#8217;t seen yet.</p>


<div class="gb-container gb-container-c5dedc2e">
<div class="gb-container gb-container-f7a433c7">
<div class="gb-container gb-container-7a8dc7a3">

<figure class="gb-block-image gb-block-image-f04fbbad"><a href="https://www.swiftyplace.com/free-swiftui-layout-cookbook" target="_blank" rel="noopener noreferrer"><img fetchpriority="high" decoding="async" width="640" height="500" class="gb-image gb-image-f04fbbad" src="http://www.swiftyplace.com/wp-content/uploads/2024/01/swiftui_roadmap_preview.webp" alt="swiftui roadmap " title="swiftui_roadmap_preview" srcset="https://www.swiftyplace.com/wp-content/uploads/2024/01/swiftui_roadmap_preview.webp 640w, https://www.swiftyplace.com/wp-content/uploads/2024/01/swiftui_roadmap_preview-300x234.webp 300w" sizes="(max-width: 640px) 100vw, 640px" /></a></figure>

</div>

<div class="gb-container gb-container-98352fe6">

<h2 class="gb-headline gb-headline-4aacd94c gb-headline-text">Feeling Lost in SwiftUI?</h2>



<p class="gb-headline gb-headline-5e6c4f85 gb-headline-text">This SwiftUI roadmap shows you what to learn next.</p>



<ul style="margin-top:0;margin-bottom:0;font-size:18px" class="wp-block-list">
<li>Key concepts at a glance</li>



<li>Spot your knowledge gaps</li>



<li>Guide your learning path</li>
</ul>


<div class="gb-container gb-container-34a8ad02">

<a class="gb-button gb-button-63e90e76 gb-button-blue" href="https://school.swiftyplace.com/f/swiftui-roadmap" target="_blank" rel="noopener noreferrer"><span class="gb-button-text">Get the FREE PDF</span><span class="gb-icon"><svg aria-hidden="true" role="img" height="1em" width="1em" viewBox="0 0 256 512" xmlns="http://www.w3.org/2000/svg"><path fill="currentColor" d="M224.3 273l-136 136c-9.4 9.4-24.6 9.4-33.9 0l-22.6-22.6c-9.4-9.4-9.4-24.6 0-33.9l96.4-96.4-96.4-96.4c-9.4-9.4-9.4-24.6 0-33.9L54.3 103c9.4-9.4 24.6-9.4 33.9 0l136 136c9.5 9.4 9.5 24.6.1 34z"></path></svg></span></a>

</div>


<p class="gb-headline gb-headline-7ef80e86 gb-headline-text">Ideal for beginners and self-learners.</p>

</div>
</div>
</div>


<div style="height:52px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading">Coding Keys</h3>



<p>The <strong>Coding Keys</strong> enum is a big one. This is how you handle JSON data that doesn&#8217;t map directly to a Swift object. Each case in the enum corresponds to a property of the struct, and the raw value of each case is the coding key that should be used in the JSON.</p>



<p>During the decoding process, the decoder looks for all coding keys in the JSON. For example, the JSON data object has a field with the name &#8220;description&#8221;. The property names in Swift should not include this name. I have to use a different property name, which is <strong>breedExplaination. </strong>In order for the decoder to know how to map from the json keys in the JSON data to my Swift struct, it uses the coding keys&#8217; raw values. In his case, it uses the case <strong>breedExplaination </strong>to look for the property name in the JSON and adds the value the Swift structs property <strong>breedExplaination.</strong></p>



<div style="height:52px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading">Custom Initializer</h3>



<p>The custom initializer is where the magic happens. This is where you decode the JSON data and assign it to the properties of the struct.</p>



<p>The <strong>init(from:)</strong> method is required by the <strong>Decodable</strong> protocol. It takes a <strong>Decoder</strong> as an argument, and it throws an error if the decoding fails.</p>



<p>In this initializer, you first create a container using the <strong>CodingKeys</strong> enum. Then, for each property of the struct, you decode a value from the container. If the decoding is successful, you assign the decoded value to the property.</p>



<div style="height:39px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading">How to transform different types from JSON to Swift</h3>



<p>One interesting thing to note here is how the <strong>Breed</strong> struct handles the <strong>isHairless</strong> property. The API represents &#8220;hairless&#8221; as an integer (1 for true, 0 for false), but in our Swift struct, we want to use a boolean. The initializer handles this by decoding the value as an integer, then converting it to a boolean.</p>



<div style="height:30px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading">Optional fields: How to deal with properties that are only sometimes passed?</h3>



<p>Another technique is dealing with optional fields. The image property is optional because not all breeds may have images. It attempts to decode a value, but if the value is missing, it assigns nil to the property.</p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono-NL.ttf" style="font-size:1rem;font-family:Code-Pro-JetBrains-Mono-NL,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span role="button" tabindex="0" data-code="image = try values.decodeIfPresent(BreedImage.self, forKey: .image)" style="color:#f6f6f4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dracula-soft" style="background-color: #282A36" tabindex="0"><code><span class="line"><span style="color: #F6F6F4">image </span><span style="color: #F286C4">=</span><span style="color: #F6F6F4"> </span><span style="color: #F286C4">try</span><span style="color: #F6F6F4"> values.</span><span style="color: #97E1F1">decodeIfPresent</span><span style="color: #F6F6F4">(BreedImage.</span><span style="color: #F286C4">self</span><span style="color: #F6F6F4">, </span><span style="color: #97E1F1">forKey</span><span style="color: #F6F6F4">: .</span><span style="color: #BF9EEE">image</span><span style="color: #F6F6F4">)</span></span></code></pre></div>



<div style="height:16px" aria-hidden="true" class="wp-block-spacer"></div>



<p>These are just a few of the advanced techniques you can use with Codable. Remember, your goal is to bridge the gap between the JSON data and your Swift objects. Sometimes that&#8217;s straightforward, but other times you&#8217;ll need to be a bit more creative.</p>



<div style="height:49px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">Conclusion</h2>



<p>So far, we&#8217;ve explored Swift&#8217;s `Codable` protocol and how it simplifies the process of converting JSON data into Swift objects and vice versa. We started with understanding what JSON is and its importance in data transfer, especially in APIs. Then, we delved into the `Codable` protocol in Swift and its constituents: `Encodable` and `Decodable`.</p>



<p>We learned how to make our Swift types conform to `Codable`, using our reading list app as an example. We explored how to encode and decode JSON data, handling arrays and nested objects along the way.</p>



<p>We didn&#8217;t stop there, though. We saw how to handle errors that might occur during encoding and decoding, preparing us for real-world scenarios where the data might not always be perfect. Finally, we ventured into some advanced techniques with `Codable`. We looked at another example app that fetches cat breed data from an API, handling optional fields and customizing our types to match the structure of the JSON data.</p>



<p>By now, you should feel more comfortable working with JSON data in Swift. Remember, practice makes perfect. Don&#8217;t hesitate to experiment with different JSON structures and Swift types. Happy coding!</p>



<p><strong>How to go from here:</strong></p>



<ul class="wp-block-list">
<li>learn about&nbsp;<a href="https://www.swiftyplace.com/blog/file-manager-in-swift-reading-writing-and-deleting-files-and-directories" target="_blank" rel="noopener">File Manager in Swift: Reading, Writing, and Deleting Files and Directories</a></li>



<li>watch a tutorial about <a href="https://youtu.be/ggEcSzPbVr4" target="_blank" rel="noopener">Working with the web</a>&nbsp;to get to know&nbsp;APIs and http requests</li>



<li>see how to use a REST Api with JSON in a SwiftUI app in this <a href="https://youtu.be/ZHK5TwKwcE4" target="_blank" rel="noopener">Youtube tutorial</a></li>
</ul>
<p>The post <a rel="nofollow" href="https://www.swiftyplace.com/blog/codable-how-to-simplify-converting-json-data-to-swift-objects-and-vice-versa">Codable: How to simplify converting JSON data to Swift objects and vice versa</a> appeared first on <a rel="nofollow" href="https://www.swiftyplace.com">swiftyplace</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.swiftyplace.com/blog/codable-how-to-simplify-converting-json-data-to-swift-objects-and-vice-versa/feed</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>

<!--
Performance optimized by W3 Total Cache. Learn more: https://www.boldgrid.com/w3-total-cache/?utm_source=w3tc&utm_medium=footer_comment&utm_campaign=free_plugin

Page Caching using Disk: Enhanced 

Served from: www.swiftyplace.com @ 2026-04-13 16:30:43 by W3 Total Cache
-->