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
  }
]
*/

61 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

Comments are closed.