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 } ] */
125 comments on “Print pretty JSON in Swift 5”
Comments are closed.
Pingback: cialis promise program
Pingback: generic viagra for sale cheap
Pingback: centurion laboratories tadalafil
Pingback: cheap cialis with dapoxetine
Pingback: viagra gel in india
Pingback: tadalafil tablets usp
Pingback: us online pharmacy hydrocodone
Pingback: sildenafil tablets 100mg online
Pingback: buy viagra online cheap india
Pingback: sildenafil tablets 100mg price
Pingback: sildenafil tablets 100mg buy
Pingback: where can you buy viagra over the counter in uk
Pingback: viagra india price
Pingback: cialis from canadian pharmacy registerd
Pingback: iron dragon tadalafil
Pingback: cialis manufacturer coupon 2018
Pingback: cialis wikipedia
Pingback: metronidazole giardiasis
Pingback: gabapentin prodrug
Pingback: does sulfamethoxazole affect birth control
Pingback: tamoxifen distribution
Pingback: lyrica for sciatica reviews
Pingback: rapivir valacyclovir
Pingback: metformin farting
Pingback: furosemide syringes
Pingback: lisinopril hct2
Pingback: rybelsus 14mg 90 comprimidos
Pingback: jardiance semaglutide
Pingback: semaglutide headache reddit
Pingback: max dose of zoloft
Pingback: nizole metronidazole
Pingback: keflex 3 times a day how far apart
Pingback: sildenafil 5 mg tablet
Pingback: duloxetine addiction
Pingback: cephalexin and metronidazole taken together
Pingback: can you drink alcohol while on fluoxetine
Pingback: lexapro insomnia
Pingback: cymbalta for degenerative disc disease
Pingback: gabapentin diverticulitis
Pingback: lamotrigine and escitalopram
Pingback: is ciprofloxacin a strong antibiotic?
Pingback: cefdinir vs cephalexin
Pingback: antibiotic bactrim
Pingback: bactrim and coumadin
Pingback: amoxicillin for bacterial vaginosis
Pingback: maximum flomax dosage
Pingback: depakote overdose
Pingback: generic name for flexeril
Pingback: diltiazem blood pressure
Pingback: how much in contrave
Pingback: citalopram engorda o adelgaza
Pingback: ezetimibe simvastatin sharp
Pingback: diclofenac sodium gel
Pingback: cozaar manufacturer
Pingback: augmentin for diverticulitis
Pingback: is effexor a controlled substance
Pingback: ddavp nasal spray von willebrand
Pingback: aripiprazole interactions
Pingback: is advil aspirin
Pingback: amitriptyline 10 mg para que sirve
Pingback: alternatives to allopurinol
Pingback: celecoxib vs.celebrex
Pingback: buspar weight loss
Pingback: celebrex and sulfa
Pingback: celexa alcohol
Pingback: ashwagandha health benefits
Pingback: augmentin yeast infection
Pingback: lamictal vs abilify
Pingback: repaglinide uv absorbance
Pingback: obat acarbose.pdf
Pingback: remeron drugs
Pingback: protonix pricing
Pingback: what are the side effects of semaglutide
Pingback: actos bochornosos
Pingback: can you take robaxin and ibuprofen together
Pingback: synthroid eutirox
Pingback: tizanidine is it a narcotic
Pingback: half life of venlafaxine
Pingback: tamsulosin hypotension
Pingback: how long does the drug voltaren stay in your system
Pingback: ivermectin 0.5% brand name
Pingback: sitagliptin januvia 100 mg
Pingback: spironolactone 100 mg used for
Pingback: vicodin online pharmacy no prescription
Pingback: levitra online pharmacy
Pingback: generic sildenafil
Pingback: sildenafil 20 mg side effects
Pingback: tadalafil / tadacip 20 dosage
Pingback: cialis vs viagra vs levitra
Pingback: inhouse pharmacy motilium
Pingback: cialis online pills
Pingback: vardenafil troches
Pingback: stromectol 12mg online
Pingback: ivermectin pills
Pingback: ivermectin 400 mg
Pingback: ivermectin 2%
Pingback: best place to buy liquid tadalafil
Pingback: ivermectin lotion cost
Pingback: cialis tadalafil patent
Pingback: order viagra online australia
Pingback: stromectol for sale
Pingback: vardenafil wikipedia
Pingback: price of sildenafil citrate
Pingback: what does amoxicillin treat
Pingback: can i take zoloft in the morning and trazodone at night
Pingback: generic name for lyrica
Pingback: how long does lisinopril take to work
Pingback: doxycycline classification
Pingback: metformin weight loss dosage
Pingback: what is cephalexin used for in dogs
Pingback: is keflex a cephalosporin
Pingback: valtrex over the counter cvs
Pingback: travelers diarrhea cipro
Pingback: about provigil
Pingback: nolvadex pct for sale
Pingback: online levitra prescription
Pingback: what happens if a woman takes tadalafil
Pingback: trusted overseas pharmacies
Pingback: viagra dosage
Pingback: vardenafil ed il tadalafil
Pingback: how long does it take sildenafil to work
Pingback: how to reduce side effects of sildenafil
Pingback: sildenafil oral jelly 100mg kamagra
Pingback: viagra vs sildenafil
Pingback: what happens if you take expired sildenafil