DISCLAIMER: In this series of posts you’ll learn to work with JSON, not with JASON, the evil character from Friday the 13th series. Besides, working with JSON has become a pleasure since Swift 4… while JASON keeps being mean. So, drop your hockey mask and start coding!
Basics
Let’s asume you have the following JSON file:
{
"productId": 1423,
"name": "Hockey Mask",
"color": "white",
"price": 39.90
}
You probably received this JSON response from an API request or you read a file content, which is a .json file. Either way, the common scenario is that you ended up having a Data
type.
First, let’s create a Struct
which contains our data structure:
struct Product: Codable {
let productId: Int
let name: String
let color: String?
let price: Float
}
The keyword here is Codable
, which is a protocol. Actually it’s a protocol composition type since it contains two protocols: Encodable
and Decodable
. Basically, Encodable
lets you convert a type back to an external representation (i.e. Data
), while Decodable
does the opposite thing.
In this example we want to convert from Data
to Product
, we could have used just Decodable
. But later on we might want to do the other way around (i.e. send something back to an API, store the data in UserDefaults
, etc). So let’s keep it as Codable
.
Now, let’s convert our Data
to Product
:
let decoder = JSONDecoder()
if let product = try? decoder.decode(Product.self, from: jsonData) {
print(product)
}
- We instantiate
JSONDecoder
, which “decodes instances of a data type from JSON objects“. - We use option binding (instead of force unwrapping) to unwrap the result or fail gracefully. You may opt to use
try
andcatch
the errors the decoder might throw. - Output the result.
Here’s what we get:
Product(productId: 1423, name: "Hockey Mask", color: Optional("white"), price: 39.9)
What’s next?
In the next post, you’ll learn how to deal with custom keys and custom objects.
Pingback: online gambling the netherlands
Pingback: cheap viagra 50mg
Pingback: canadian pharmacy generic cialis
Pingback: cialis 20mg 60
Pingback: buy viagra 100 mg
Pingback: onbling casino
Pingback: interactions for viagra
Pingback: viagra for women
Pingback: buy cialis pills
Pingback: cialis dosage recommend
Pingback: order viagra 50 mg
Pingback: cialis cost 20mg
Pingback: cialis online canada
Pingback: walgreens cialis prices
Pingback: where to buy real viagra cialis online
Pingback: extra super viagra
Pingback: goodrx gold
Pingback: zithromax dosing
Pingback: sildenafil and tadalafil combination tablets
Pingback: sildenafil price india
Pingback: meloxicam lamotrigine lidocaine cream
Pingback: stromectol xr
Pingback: combivent 250 mg pharmacy
Pingback: ivermectin stromectol
Pingback: tadalafil lilly
Pingback: the viagra connect website
Pingback: best generic viagra
Pingback: ivermectin price uk
Pingback: ivermectin study nih
Pingback: where to buy ventolin nz
Pingback: kory
Pingback: dr pierre kory ivermectin
Pingback: Anonymous
Pingback: heb pharmacy
Pingback: buy stromectol 3 mg
Pingback: ivermectin brand
Pingback: ivermectin canada
Pingback: ivermectin 0.5
Pingback: Anonymous
Pingback: stromectol covid 19
Pingback: ivermectin buy australia
Pingback: ivermectin pills human
Pingback: buy ivermectin stromectol
Pingback: stromectol ivermectin tablets
Pingback: order stromectol online
Pingback: Anonymous
Pingback: ivermectin buy online
Pingback: stromectol price
Pingback: cheap viagra deals
Pingback: where to buy ivermectin
Pingback: stromectol tablets
Pingback: buy ivermectin online
Pingback: ivermectin where to buy
Pingback: ignition casino deposit
Pingback: buy stromectol
Pingback: cheap tadalafil
Pingback: prednisone 5mg usa
Pingback: doxazosin and cialis
Pingback: generic tadalafil
Pingback: generic provigil for sale
Pingback: ivermectin 3mg tab
Pingback: how to get zithromax online
Pingback: ivermectin oral
Pingback: buy viagra amazon
Pingback: tadalafil powder bulk
Pingback: cialis website
Pingback: purchase viagra tablets
Pingback: ivermectin in diabetic patients
Pingback: generic cialis daily
Pingback: medshuku cialis without prescription
Pingback: tadalafil side effects
Pingback: sildenafil pills generic brand
Pingback: peptide tadalafil
Pingback: prescription without a doctor's prescription
Pingback: cost of cialis generic
Pingback: buy cialis online no prescription
Pingback: stromectol for scabies
Pingback: buy prescription drugs
Pingback: purchase sildenafil tablets
Pingback: sildenafil tablets uk
Pingback: cheap cialis india
Pingback: cialis reviews
Pingback: tadalafil cost walgreens
Pingback: buy prednisone over counter
Pingback: what is prednisone 20mg tablets used for
Pingback: cheap cialis prescription
Pingback: cost of molnupiravir
Pingback: mulnopiravir
Pingback: 3georgia
Pingback: cialis 5mg no prescription
Pingback: tadalafil walmart
Pingback: cialis price
Pingback: ivermectin oral 0 8
Pingback: prednisone
Pingback: generic viagra pills
Pingback: generic
Pingback: $100 no deposit usa casinos
Pingback: ivermectin at tractor supply
Pingback: cialis 20mg online
Pingback: ivermectin 50ml
Pingback: online casino play for real money
Pingback: ivermectin for humans dosage
Pingback: keflex sti
Pingback: generic
Pingback: generic viagra name
Pingback: buy tadalafil online
Pingback: omnicef generation
Pingback: ivermectin 500ml
Pingback: order azithromycin
Pingback: erythromycin bp
Pingback: ivermectina oral
Pingback: cialis prescription online canada
Pingback: cheap cialis prescription
Pingback: ivermectin pyrantel pamoate
Pingback: buy ivermectin 6 mg online
Pingback: order stromectol
Pingback: where to purchase doxycycline
Pingback: ivermectin 6mg tabs
Pingback: ivermectin studies
Pingback: viagra how to use the first time
Pingback: order ivermectin 3 mg online
Pingback: buy ivermectin 12mg for humans
Pingback: order ivermectin
Pingback: minocycline 100 mg pills
Pingback: ivermectin 0.08 oral solution
Pingback: stromectol 3mg
Pingback: ivermectin 6mg dosage
Pingback: lasix online no prescription
Pingback: buy ivermectin 6 mg tablets
Pingback: buy cheap furosemide
Pingback: stromectol for lice
Pingback: generic cialis uk
Pingback: ivermectin 10 ml
Pingback: ivermectin for rabbit fur mites
Pingback: ivermectin 250ml
Pingback: ivermectin overdose
Pingback: viagra 100 mg tablet
Pingback: ivermectin 3mg tablets price
Pingback: ivermectin 3mg dosage
Pingback: cialis shelf life
Pingback: luckylandslots
Pingback: cialis mastercard
Pingback: cost of stromectol medication
Pingback: intermectine
Pingback: cialis onlilne
Pingback: ivermectin 0.5
Pingback: where to buy stromectol
Pingback: ivermektin
Pingback: cialis headache
Pingback: clomid online usa
Pingback: clomid capsules
Pingback: cialis walmart prescription
Pingback: over the counter clomid uk
Pingback: buy clomid cheap online
Pingback: ivermectin tractor supply
Pingback: ivermectin for dogs
Pingback: buy anti biotics without prescription
Pingback: india ivermectin
Pingback: natural help for ed
Pingback: cialis otc
Pingback: ivermectin paste dosage for dogs
Pingback: mazhor4sezon
Pingback: purchase stromectol
Pingback: filmfilmfilmes
Pingback: gRh9UPV
Pingback: ivermectin dosage for scabies in humans
Pingback: ivermectin paste for humans
Pingback: erectial dysfunction
Pingback: cheap medications online
Pingback: 9-05-2022
Pingback: kinoteatrzarya.ru
Pingback: Xvideos
Pingback: XVIDEOSCOM Videos
Pingback: canadian online drugstore
Pingback: ivanesva
Pingback: tadalafil without a doctor's prescription
Pingback: ed meds online without doctor prescription
Pingback: hydroxychloroquine online
Pingback: tadalafilo
Pingback: how long for ivermectin to work
Pingback: buy generic 100mg viagra online
Pingback: cialis sales in victoria canada
Pingback: Netflix
Pingback: ivermectin pills
Pingback: psy-
Pingback: ivermectin covid
Pingback: online moskva psiholog
Pingback: slovar po psihoanalizu laplansh
Pingback: psy online
Pingback: Gz92uNNH
Pingback: ivermectine usa
Pingback: DPTPtNqS
Pingback: qQ8KZZE6
Pingback: D6tuzANh
Pingback: SHKALA TONOV
Pingback: Øêàëà òîíîâ
Pingback: molnupiravir same as ivermectin
Pingback: Psikholog
Pingback: ivermectin drug
Pingback: buy stromectol for humans in tijuana
Pingback: site
Pingback: stromectol 3 mg pills
Pingback: stats
Pingback: UKRAINE
Pingback: stromectol 6mg dosage
Pingback: mir dikogo zapada 4 sezon 4 seriya
Pingback: video
Pingback: film
Pingback: cialis on line
Pingback: generic cialis at walmart
Pingback: liusia-8-seriiaonlain
Pingback: smotret-polnyj-film-smotret-v-khoroshem-kachestve
Pingback: cialis over the counter walmart
Pingback: cleantalkorg2.ru
Pingback: filmgoda.ru
Pingback: rodnoe-kino-ru
Pingback: confeitofilm
Pingback: stat.netstate.ru
Pingback: sY5am
Pingback: Dom drakona
Pingback: JGXldbkj
Pingback: aOuSjapt
Pingback: ìûøëåíèå
Pingback: psikholog moskva
Pingback: Usik Dzhoshua 2 2022
Pingback: Dim Drakona 2022
Pingback: TwnE4zl6
Pingback: programma peredach na segodnya
Pingback: stromectol 3 mg online
Pingback: link
Pingback: video-2
Pingback: sezons.store
Pingback: socionika-eniostyle.ru
Pingback: psy-news.ru
Pingback: 000-1
Pingback: 3SoTS32
Pingback: 3DGofO7
Pingback: wwwi.odnoklassniki-film.ru
Pingback: rftrip.ru
Pingback: dolpsy.ru
Pingback: kin0shki.ru
Pingback: 3o9cpydyue4s8.ru
Pingback: mb588.ru
Pingback: history-of-ukraine.ru news ukraine
Pingback: newsukraine.ru
Pingback: kamagra oral jelly
Pingback: edu-design.ru
Pingback: tftl.ru
Pingback: brutv
Pingback: site 2023
Pingback: buy generic propecia in usa
Pingback: buy propecia online hong kong
Pingback: sitestats01
Pingback: 1c789.ru
Pingback: cttdu.ru
Pingback: hdserial2023.ru
Pingback: cialis over the counter at walmart
Pingback: matchonline2022.ru
Pingback: cenforce 150
Pingback: kamagra online
Pingback: bit.ly/3OEzOZR
Pingback: bit.ly/3gGFqGq
Pingback: bit.ly/3ARFdXA
Pingback: bit.ly/3ig2UT5
Pingback: bit.ly/3GQNK0J
Pingback: vardenafil 10mg
Pingback: bep5w0Df
Pingback: www
Pingback: icf
Pingback: 24hours-news
Pingback: rusnewsweek
Pingback: uluro-ado
Pingback: irannews.ru
Pingback: klondayk2022
Pingback: legal online pharmacies in the us
Pingback: treatment for ed
Pingback: x
Pingback: over the counter asthma inhalers
Pingback: 9xflix
Pingback: xnxx
Pingback: 123movies
Pingback: appetite suppressants over the counter
Pingback: econazole nitrate cream over the counter
Pingback: buy zithromax 500mg online