Print pretty JSON in Swift 5

When dealing with REST APIs and JSON data in iOS it can be very useful to be able to see the data in raw string format. With this small extension you can print JSON data as an optional string from a Data object:

extension Data {
    var prettyJSON: String? {
        guard let object = try? JSONSerialization.jsonObject(with: self, options: []),
              let data = try? JSONSerialization.data(withJSONObject: object, options: [.prettyPrinted])
        else { return nil }
        
        return String(data: data, encoding: .utf8)
    }
}

Now, consider the following struct:

struct Person: Codable {
    var name: String
    var age: Float
}

And then lets create an array of people and encode it into data:

let people = [
    Person(name: "Gunther", age: 24.5),
    Person(name: "Viktor", age: 33)
]

let data = try? JSONEncoder().encode(people)

Now we can use the extension and print the data in pretty json:

print(data?.prettyJSON ?? "")

/*
[
  {
    "name" : "Gunther",
    "age" : 24.5
  },
  {
    "name" : "Viktor",
    "age" : 33
  }
]
*/

124 comments on “Print pretty JSON in Swift 5”

  1. Pingback: cialis promise program

  2. Pingback: generic viagra for sale cheap

  3. Pingback: centurion laboratories tadalafil

  4. Pingback: cheap cialis with dapoxetine

  5. Pingback: viagra gel in india

  6. Pingback: tadalafil tablets usp

  7. Pingback: us online pharmacy hydrocodone

  8. Pingback: sildenafil tablets 100mg online

  9. Pingback: buy viagra online cheap india

  10. Pingback: sildenafil tablets 100mg price

  11. Pingback: sildenafil tablets 100mg buy

  12. Pingback: where can you buy viagra over the counter in uk

  13. Pingback: viagra india price

  14. Pingback: cialis from canadian pharmacy registerd

  15. Pingback: iron dragon tadalafil

  16. Pingback: cialis manufacturer coupon 2018

  17. Pingback: cialis wikipedia

  18. Pingback: metronidazole giardiasis

  19. Pingback: gabapentin prodrug

  20. Pingback: does sulfamethoxazole affect birth control

  21. Pingback: tamoxifen distribution

  22. Pingback: lyrica for sciatica reviews

  23. Pingback: rapivir valacyclovir

  24. Pingback: metformin farting

  25. Pingback: furosemide syringes

  26. Pingback: lisinopril hct2

  27. Pingback: rybelsus 14mg 90 comprimidos

  28. Pingback: jardiance semaglutide

  29. Pingback: semaglutide headache reddit

  30. Pingback: max dose of zoloft

  31. Pingback: nizole metronidazole

  32. Pingback: keflex 3 times a day how far apart

  33. Pingback: sildenafil 5 mg tablet

  34. Pingback: duloxetine addiction

  35. Pingback: cephalexin and metronidazole taken together

  36. Pingback: can you drink alcohol while on fluoxetine

  37. Pingback: lexapro insomnia

  38. Pingback: cymbalta for degenerative disc disease

  39. Pingback: gabapentin diverticulitis

  40. Pingback: lamotrigine and escitalopram

  41. Pingback: is ciprofloxacin a strong antibiotic?

  42. Pingback: cefdinir vs cephalexin

  43. Pingback: antibiotic bactrim

  44. Pingback: bactrim and coumadin

  45. Pingback: amoxicillin for bacterial vaginosis

  46. Pingback: maximum flomax dosage

  47. Pingback: depakote overdose

  48. Pingback: generic name for flexeril

  49. Pingback: diltiazem blood pressure

  50. Pingback: how much in contrave

  51. Pingback: citalopram engorda o adelgaza

  52. Pingback: ezetimibe simvastatin sharp

  53. Pingback: diclofenac sodium gel

  54. Pingback: cozaar manufacturer

  55. Pingback: augmentin for diverticulitis

  56. Pingback: is effexor a controlled substance

  57. Pingback: ddavp nasal spray von willebrand

  58. Pingback: aripiprazole interactions

  59. Pingback: is advil aspirin

  60. Pingback: amitriptyline 10 mg para que sirve

  61. Pingback: alternatives to allopurinol

  62. Pingback: celecoxib vs.celebrex

  63. Pingback: buspar weight loss

  64. Pingback: celebrex and sulfa

  65. Pingback: celexa alcohol

  66. Pingback: ashwagandha health benefits

  67. Pingback: augmentin yeast infection

  68. Pingback: lamictal vs abilify

  69. Pingback: repaglinide uv absorbance

  70. Pingback: obat acarbose.pdf

  71. Pingback: remeron drugs

  72. Pingback: protonix pricing

  73. Pingback: what are the side effects of semaglutide

  74. Pingback: actos bochornosos

  75. Pingback: can you take robaxin and ibuprofen together

  76. Pingback: synthroid eutirox

  77. Pingback: tizanidine is it a narcotic

  78. Pingback: half life of venlafaxine

  79. Pingback: tamsulosin hypotension

  80. Pingback: how long does the drug voltaren stay in your system

  81. Pingback: ivermectin 0.5% brand name

  82. Pingback: sitagliptin januvia 100 mg

  83. Pingback: spironolactone 100 mg used for

  84. Pingback: vicodin online pharmacy no prescription

  85. Pingback: levitra online pharmacy

  86. Pingback: generic sildenafil

  87. Pingback: sildenafil 20 mg side effects

  88. Pingback: tadalafil / tadacip 20 dosage

  89. Pingback: cialis vs viagra vs levitra

  90. Pingback: inhouse pharmacy motilium

  91. Pingback: cialis online pills

  92. Pingback: vardenafil troches

  93. Pingback: stromectol 12mg online

  94. Pingback: ivermectin pills

  95. Pingback: ivermectin 400 mg

  96. Pingback: ivermectin 2%

  97. Pingback: best place to buy liquid tadalafil

  98. Pingback: ivermectin lotion cost

  99. Pingback: cialis tadalafil patent

  100. Pingback: order viagra online australia

  101. Pingback: stromectol for sale

  102. Pingback: vardenafil wikipedia

  103. Pingback: price of sildenafil citrate

  104. Pingback: what does amoxicillin treat

  105. Pingback: can i take zoloft in the morning and trazodone at night

  106. Pingback: generic name for lyrica

  107. Pingback: how long does lisinopril take to work

  108. Pingback: doxycycline classification

  109. Pingback: metformin weight loss dosage

  110. Pingback: what is cephalexin used for in dogs

  111. Pingback: is keflex a cephalosporin

  112. Pingback: valtrex over the counter cvs

  113. Pingback: travelers diarrhea cipro

  114. Pingback: about provigil

  115. Pingback: nolvadex pct for sale

  116. Pingback: online levitra prescription

  117. Pingback: what happens if a woman takes tadalafil

  118. Pingback: trusted overseas pharmacies

  119. Pingback: viagra dosage

  120. Pingback: vardenafil ed il tadalafil

  121. Pingback: how long does it take sildenafil to work

  122. Pingback: how to reduce side effects of sildenafil

  123. Pingback: sildenafil oral jelly 100mg kamagra

  124. Pingback: viagra vs sildenafil

Comments are closed.