Utilo

JSON to Swift Converter – Free, Online, Private

Generate Swift Codable structs from a JSON sample, with CodingKeys only when a property name has to differ from the JSON key.

Input
Output

How to convert JSON to Swift

  1. Paste the JSON you decoded with JSONDecoder.
  2. Copy the structs. Nested objects become their own named structs.
  3. If a key is optional in the real API but present in this sample, mark the property optional yourself.

Codable without extra keys

A struct conforms to Codable and stores each field as a let. When every JSON key is already a Swift identifier in the naming you chose, no CodingKeys enum is emitted, because the synthesized one would be identical. Keys such as content-type cannot be identifiers, so the property becomes contentType and a CodingKeys case maps it back. Integers become Int, fractional numbers become Double, booleans become Bool, and strings stay String. Arrays use Swift array syntax. An empty array becomes [JSONValue], a stand-in that means the sample never showed an element. You will want to replace that with the real element type. Null and missing keys become optional properties.

Dates and numbers

An ISO date in JSON is a string, and the struct keeps it as String. JSONDecoder's dateDecodingStrategy is your choice later; this page cannot see that you intended a Date. A whole number becomes Int. JSONDecoder will not decode a JSON number that does not fit Int into an Int property, and it will not decode 1.5 into Int either. If the API sometimes sends a float, change the property to Double. Very large integers are already imprecise once JSON.parse has read them in the browser. Treat those ids as strings in the sample if you need every digit.

Worked example

The sample produces struct Root with id: Int, name: String, active: Bool, tags: [String], and address: Address. Address has city and zip. No CodingKeys enum appears, because every key is a valid identifier and the camelCase form matches the key. If you rename nothing and the key was user_name, the property becomes userName and CodingKeys carries the original spelling. Optional properties are declared with ? and are not required in the memberwise initializer's sense of a default; you still pass them or use the synthesized decoder. The page does not emit an init(from:) by hand.

Limits

There is no CodingKeys for a custom key strategy such as convertFromSnakeCase. The names are written out instead, so the struct does not depend on a decoder strategy you might forget to set. Discriminated unions are not enums. A field that is sometimes a string and sometimes a number becomes JSONValue, which will not compile until you define that type or replace it. The generator does not produce Foundation imports. Codable is in the Swift standard library. Generation stays in the tab, so a private payload never needs to be uploaded to produce a struct. For Kotlin data classes or Dart models, use the neighboring converters.

Frequently asked questions

Why is there no CodingKeys enum sometimes?

It is omitted when every property name already matches its JSON key. Adding an identical enum would only add noise.

Does this use JSONDecoder key strategies?

No. Renames are explicit CodingKeys, so the struct decodes with the default strategy.

What about optional fields that were present?

They stay required. Optionality is inferred only from nulls and from keys missing on some objects in an array.

Can I decode the sample with the struct?

After you paste it into a Swift file, yes, for a payload that matches the sample. Extra keys are ignored by JSONDecoder unless you change that.