How to prettify JSON in swift

Without dependencies on other third-party frameworks

Just a function that receives a string, outputs a string and throws

Try :

let encoder = JSONEncoder()
encoder.outputFormatting = .prettyPrinted

Additional Infos:

I'm thinking about decoding the JSON to a [String: Any] and then encoding that again, prettified, but the type won't conform to Codable:

let json = "..."
let decoder = JSONDecoder()
let encoder = JSONEncoder()
encoder.outputFormatting = .prettyPrinted
// then decode json with decoder and encode again

How to prettify JSON in swift
 
 
Q