How to parse a JSON string into an object and serialize (stringify) an object back to JSON in Swift. Foundation ships JSONSerialization, and Codable with JSONDecoder/JSONEncoder for typed models. Need to clean up the JSON first? Use the formatter & validator.
let data = text.data(using: .utf8)!
let obj = try JSONSerialization
.jsonObject(with: data)
let out = try JSONSerialization.data(
withJSONObject: obj, options: .prettyPrinted)
Foundation ships JSONSerialization, and Codable with JSONDecoder/JSONEncoder for typed models. Parse with the snippet above; the result is a native object/map you can read.
Use the stringify snippet above with indentation, or paste your JSON into the formatter on this page.