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