Turn JSON into Swift models
Paste some JSON and SwiftyModel writes a clean Swift model as you type. Nested objects, array types and property names are taken care of, with support for SwiftyJSON and Codable.
JSON in, model out
The code on the right was generated by SwiftyModel with its default settings. Only the model name was changed, to User.
{
"id": 42,
"user_name": "Ada",
"is_active": true,
"score": 9.5,
"tags": ["swift", "json"],
"self": "keyword",
"profile": { "home_page": "example.com" }
}import Foundation
import SwiftyJSON
struct User {
struct Profile {
let homePage: String
init(json: JSON) {
homePage = json["home_page"].stringValue
}
}
let id: Int
let isActive: Bool
let profile: Profile
let score: Double
let keySelf: String
let tags: [String]
let userName: String
init(json: JSON) {
id = json["id"].intValue
isActive = json["is_active"].boolValue
profile = Profile(json: json["profile"])
score = json["score"].doubleValue
keySelf = json["self"].stringValue
tags = json["tags"].arrayValue.map { $0.stringValue }
userName = json["user_name"].stringValue
}
}user_name became userName, the keyword self became keySelf, and the nested profile got its own Profile type.
Features
Live results
The Swift result updates as you edit the JSON. If the JSON is invalid, SwiftyModel tells you why and points to the line and column.
SwiftyJSON and Codable
Every model gets an init(json:) initializer. Turn on Codable to add Codable conformance and CodingKeys.
Nested objects and arrays
Nested objects become nested types, and arrays such as [[Int]] are typed at every level.
Names that compile
snake_case becomes camelCase, keywords such as self and class get a prefix or suffix, and clashing names are numbered.
Your code style
struct or class, access levels, let or var, 2- or 4-space indentation, and an optional memberwise initializer.
Stays on your Mac
Conversion happens on your Mac. There is no account, no data collection, and your JSON is never sent to a server.