Working with JSON – Part 1

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)
}
  1. We instantiate JSONDecoder, which “decodes instances of a data type from JSON objects“.
  2. We use option binding (instead of force unwrapping) to unwrap the result or fail gracefully. You may opt to use try and catch the errors the decoder might throw.
  3. 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.

239 Comments

  1. Pingback: online gambling the netherlands

  2. Pingback: cheap viagra 50mg

  3. Pingback: canadian pharmacy generic cialis

  4. Pingback: cialis 20mg 60

  5. Pingback: buy viagra 100 mg

  6. Pingback: onbling casino

  7. Pingback: interactions for viagra

  8. Pingback: viagra for women

  9. Pingback: buy cialis pills

  10. Pingback: cialis dosage recommend

  11. Pingback: order viagra 50 mg

  12. Pingback: cialis cost 20mg

  13. Pingback: cialis online canada

  14. Pingback: walgreens cialis prices

  15. Pingback: where to buy real viagra cialis online

  16. Pingback: extra super viagra

  17. Pingback: goodrx gold

  18. Pingback: zithromax dosing

  19. Pingback: sildenafil and tadalafil combination tablets

  20. Pingback: sildenafil price india

  21. Pingback: meloxicam lamotrigine lidocaine cream

  22. Pingback: stromectol xr

  23. Pingback: combivent 250 mg pharmacy

  24. Pingback: ivermectin stromectol

  25. Pingback: tadalafil lilly

  26. Pingback: the viagra connect website

  27. Pingback: best generic viagra

  28. Pingback: ivermectin price uk

  29. Pingback: ivermectin study nih

  30. Pingback: where to buy ventolin nz

  31. Pingback: kory

  32. Pingback: dr pierre kory ivermectin

  33. Pingback: Anonymous

  34. Pingback: heb pharmacy

  35. Pingback: buy stromectol 3 mg

  36. Pingback: ivermectin brand

  37. Pingback: ivermectin canada

  38. Pingback: ivermectin 0.5

  39. Pingback: Anonymous

  40. Pingback: stromectol covid 19

  41. Pingback: ivermectin buy australia

  42. Pingback: ivermectin pills human

  43. Pingback: buy ivermectin stromectol

  44. Pingback: stromectol ivermectin tablets

  45. Pingback: order stromectol online

  46. Pingback: Anonymous

  47. Pingback: ivermectin buy online

  48. Pingback: stromectol price

  49. Pingback: cheap viagra deals

  50. Pingback: where to buy ivermectin

  51. Pingback: stromectol tablets

  52. Pingback: buy ivermectin online

  53. Pingback: ivermectin where to buy

  54. Pingback: ignition casino deposit

  55. Pingback: buy stromectol

  56. Pingback: cheap tadalafil

  57. Pingback: prednisone 5mg usa

  58. Pingback: doxazosin and cialis

  59. Pingback: generic tadalafil

  60. Pingback: generic provigil for sale

  61. Pingback: ivermectin 3mg tab

  62. Pingback: how to get zithromax online

  63. Pingback: ivermectin oral

  64. Pingback: buy viagra amazon

  65. Pingback: tadalafil powder bulk

  66. Pingback: cialis website

  67. Pingback: purchase viagra tablets

  68. Pingback: ivermectin in diabetic patients

  69. Pingback: generic cialis daily

  70. Pingback: medshuku cialis without prescription

  71. Pingback: tadalafil side effects

  72. Pingback: sildenafil pills generic brand

  73. Pingback: peptide tadalafil

  74. Pingback: prescription without a doctor's prescription

  75. Pingback: cost of cialis generic

  76. Pingback: buy cialis online no prescription

  77. Pingback: stromectol for scabies

  78. Pingback: buy prescription drugs

  79. Pingback: purchase sildenafil tablets

  80. Pingback: sildenafil tablets uk

  81. Pingback: cheap cialis india

  82. Pingback: cialis reviews

  83. Pingback: tadalafil cost walgreens

  84. Pingback: buy prednisone over counter

  85. Pingback: what is prednisone 20mg tablets used for

  86. Pingback: cheap cialis prescription

  87. Pingback: cost of molnupiravir

  88. Pingback: mulnopiravir

  89. Pingback: 3georgia

  90. Pingback: cialis 5mg no prescription

  91. Pingback: tadalafil walmart

  92. Pingback: cialis price

  93. Pingback: ivermectin oral 0 8

  94. Pingback: prednisone

  95. Pingback: generic viagra pills

  96. Pingback: generic

  97. Pingback: $100 no deposit usa casinos

  98. Pingback: ivermectin at tractor supply

  99. Pingback: cialis 20mg online

  100. Pingback: ivermectin 50ml

  101. Pingback: online casino play for real money

  102. Pingback: ivermectin for humans dosage

  103. Pingback: keflex sti

  104. Pingback: generic

  105. Pingback: generic viagra name

  106. Pingback: buy tadalafil online

  107. Pingback: omnicef generation

  108. Pingback: ivermectin 500ml

  109. Pingback: order azithromycin

  110. Pingback: erythromycin bp

  111. Pingback: ivermectina oral

  112. Pingback: cialis prescription online canada

  113. Pingback: cheap cialis prescription

  114. Pingback: ivermectin pyrantel pamoate

  115. Pingback: buy ivermectin 6 mg online

  116. Pingback: order stromectol

  117. Pingback: where to purchase doxycycline

  118. Pingback: ivermectin 6mg tabs

  119. Pingback: ivermectin studies

  120. Pingback: viagra how to use the first time

  121. Pingback: order ivermectin 3 mg online

  122. Pingback: buy ivermectin 12mg for humans

  123. Pingback: order ivermectin

  124. Pingback: minocycline 100 mg pills

  125. Pingback: ivermectin 0.08 oral solution

  126. Pingback: stromectol 3mg

  127. Pingback: ivermectin 6mg dosage

  128. Pingback: lasix online no prescription

  129. Pingback: buy ivermectin 6 mg tablets

  130. Pingback: buy cheap furosemide

  131. Pingback: stromectol for lice

  132. Pingback: generic cialis uk

  133. Pingback: ivermectin 10 ml

  134. Pingback: ivermectin for rabbit fur mites

  135. Pingback: ivermectin 250ml

  136. Pingback: ivermectin overdose

  137. Pingback: viagra 100 mg tablet

  138. Pingback: ivermectin 3mg tablets price

  139. Pingback: ivermectin 3mg dosage

  140. Pingback: cialis shelf life

  141. Pingback: luckylandslots

  142. Pingback: cialis mastercard

  143. Pingback: cost of stromectol medication

  144. Pingback: intermectine

  145. Pingback: cialis onlilne

  146. Pingback: ivermectin 0.5

  147. Pingback: where to buy stromectol

  148. Pingback: ivermektin

  149. Pingback: cialis headache

  150. Pingback: clomid online usa

  151. Pingback: clomid capsules

  152. Pingback: cialis walmart prescription

  153. Pingback: over the counter clomid uk

  154. Pingback: buy clomid cheap online

  155. Pingback: ivermectin tractor supply

  156. Pingback: ivermectin for dogs

  157. Pingback: buy anti biotics without prescription

  158. Pingback: india ivermectin

  159. Pingback: natural help for ed

  160. Pingback: cialis otc

  161. Pingback: ivermectin paste dosage for dogs

  162. Pingback: mazhor4sezon

  163. Pingback: purchase stromectol

  164. Pingback: filmfilmfilmes

  165. Pingback: gRh9UPV

  166. Pingback: ivermectin dosage for scabies in humans

  167. Pingback: ivermectin paste for humans

  168. Pingback: erectial dysfunction

  169. Pingback: cheap medications online

  170. Pingback: 9-05-2022

  171. Pingback: kinoteatrzarya.ru

  172. Pingback: Xvideos

  173. Pingback: XVIDEOSCOM Videos

  174. Pingback: canadian online drugstore

  175. Pingback: ivanesva

  176. Pingback: tadalafil without a doctor's prescription

  177. Pingback: ed meds online without doctor prescription

  178. Pingback: hydroxychloroquine online

  179. Pingback: tadalafilo

  180. Pingback: how long for ivermectin to work

  181. Pingback: buy generic 100mg viagra online

  182. Pingback: cialis sales in victoria canada

  183. Pingback: Netflix

  184. Pingback: ivermectin pills

  185. Pingback: psy-

  186. Pingback: ivermectin covid

  187. Pingback: online moskva psiholog

  188. Pingback: slovar po psihoanalizu laplansh

  189. Pingback: psy online

  190. Pingback: Gz92uNNH

  191. Pingback: ivermectine usa

  192. Pingback: DPTPtNqS

  193. Pingback: qQ8KZZE6

  194. Pingback: D6tuzANh

  195. Pingback: SHKALA TONOV

  196. Pingback: Øêàëà òîíîâ

  197. Pingback: molnupiravir same as ivermectin

  198. Pingback: Psikholog

  199. Pingback: ivermectin drug

  200. Pingback: buy stromectol for humans in tijuana

  201. Pingback: site

  202. Pingback: stromectol 3 mg pills

  203. Pingback: stats

  204. Pingback: UKRAINE

  205. Pingback: stromectol 6mg dosage

  206. Pingback: mir dikogo zapada 4 sezon 4 seriya

  207. Pingback: video

  208. Pingback: film

  209. Pingback: cialis on line

  210. Pingback: generic cialis at walmart

  211. Pingback: liusia-8-seriiaonlain

  212. Pingback: smotret-polnyj-film-smotret-v-khoroshem-kachestve

  213. Pingback: cialis over the counter walmart

  214. Pingback: cleantalkorg2.ru

  215. Pingback: filmgoda.ru

  216. Pingback: rodnoe-kino-ru

  217. Pingback: confeitofilm

  218. Pingback: stat.netstate.ru

  219. Pingback: sY5am

  220. Pingback: Dom drakona

  221. Pingback: JGXldbkj

  222. Pingback: aOuSjapt

  223. Pingback: ìûøëåíèå

  224. Pingback: psikholog moskva

  225. Pingback: Usik Dzhoshua 2 2022

  226. Pingback: Dim Drakona 2022

  227. Pingback: TwnE4zl6

  228. Pingback: programma peredach na segodnya

  229. Pingback: stromectol 3 mg online

  230. Pingback: link

  231. Pingback: video-2

  232. Pingback: sezons.store

  233. Pingback: socionika-eniostyle.ru

  234. Pingback: psy-news.ru

  235. Pingback: 000-1

  236. Pingback: 3SoTS32

  237. Pingback: 3DGofO7

  238. Pingback: wwwi.odnoklassniki-film.ru

  239. Pingback: rftrip.ru

Comments are closed.