Skip to main content
Convert2JSON Tools

JSON to Swift Converter

Generate Codable Swift structs from a JSON response, with CodingKeys added only where they are actually required.

Your data stays on your device

Input — JSON

characters
0
lines
0
size
0 B

Output — Swift

characters
0
lines
0
size
0 B

Options

About this tool

Swift decodes JSON through Codable, which works automatically when property names match the JSON keys exactly. The moment a key is snake_case and your property is camelCase, you need a CodingKeys enum — and writing one by hand for every model is tedious.

The generator emits CodingKeys only when at least one name differs, keeping the output as clean as possible. Optionals are used where the sample shows null or an absent key, so decoding does not fail on real-world responses.

How to use this tool

  1. Paste your JSON sample.
  2. Set the root struct name.
  3. Choose whether to convert keys to camelCase and whether to prefer struct or class output.
  4. Press Generate and copy the code into your Xcode project.

Key features

  • struct or final class output, both conforming to Codable
  • CodingKeys emitted only when a property name differs from its JSON key
  • Optional properties where the sample shows null or a missing key
  • Nested objects generated as separate Codable types
  • Int and Double distinguished, with [T] for arrays
  • Swift keywords escaped with backticks

Example

Codable struct with CodingKeys

InputJSON

{"first_name":"Ada","age":36}

OutputSwift

struct Root: Codable {
    let firstName: String
    let age: Int

    enum CodingKeys: String, CodingKey {
        case firstName = "first_name"
        case age
    }
}

Good to know

  • Date values remain String unless you enable date detection and configure a decoding strategy yourself.
  • Heterogeneous arrays cannot be typed; Swift has no untyped array equivalent that is convenient here, so such fields need manual handling.
  • No import statements are emitted beyond what a plain Foundation project needs.

Frequently asked questions

Do I always need CodingKeys?

No. If every property name matches its JSON key exactly, Codable works without them, and the generator leaves them out.

struct or class?

Struct is the Swift default: value semantics, no reference-cycle risk, and better performance for data models. Use a class only when you need identity or inheritance.

How should I handle dates?

Keep the property as String, or make it a Date and set the decoder dateDecodingStrategy to match your format. The generator cannot know the format from a sample alone.

Why is a property optional?

Because the sample contained null for it, or the key was absent from some elements. Non-optional properties cause decoding to throw when a key is missing.

Your data stays on your device

Your data is processed locally in your browser and is not uploaded to our server. This page keeps working after you go offline, because there is nothing to send.

Share this toolXLinkedInHacker News