format · validate · convert · runs locally

Parse JSON in Swift

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.

Parse JSON (string → object)

let data = text.data(using: .utf8)!
let obj = try JSONSerialization
    .jsonObject(with: data)

Stringify (object → JSON string)

let out = try JSONSerialization.data(
    withJSONObject: obj, options: .prettyPrinted)

Frequently asked questions

How do I parse JSON in Swift?

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.

How do I pretty-print JSON in Swift?

Use the stringify snippet above with indentation, or paste your JSON into the formatter on this page.