CuteBaby: JSON Model Generator for Swift

CuteBaby can generate Swift models (struct or class) from a JSON string.

CuteBaby: JSON Model Generator for Swift

It has some nice features to customize the models that it generated.

  1. It can infer property’s type from JSON such as String, Int, Double, URL and Date.
  2. It can handle nested JSON, it will generate nested models.
  3. It supports Codable from Swift 4.
  4. You can specify property’s name with Property Map, array’s object name with Array Object Map, property’s type with Property Type Map, or enum’s definition with Enum Properties.
  5. It can also handle array root JSON, it will automatically merge properties for objects in the array.
  6. It can format JSON with pretty printed.
  7. It can load JSON from a URL.
  8. You can Drag & Drop text files to create new items.

For JSON:

{
"id": 42,
"name": "nixzhu",
"twitter": {
"profile_url": "https://twitter.com/nixzhu",
"created_at": "2009-05-12T10:25:43.511Z"
}
}

It’ll generate Swift code (with Codable):

struct User: Codable {
let id: Int
let name: String
struct Twitter: Codable {
let profileURL: URL
let createdAt: Date
private enum CodingKeys: String, CodingKey {
case profileURL = "profile_url"
case createdAt = "created_at"
}
}
let twitter: Twitter
}

Note that there use Property Map profile_url: profileURL to change the property name (Automatically generated will be profileUrl).

Swift code without Codable:

struct User {
let id: Int
let name: String
struct Twitter {
let profileURL: URL
let createdAt: Date
init(profileURL: URL, createdAt: Date) {
self.profileURL = profileURL
self.createdAt = createdAt
}
init?(json: [String: Any]) {
guard let profileURLString = json["profile_url"] as? String else { return nil }
guard let profileURL = URL(string: profileURLString) else { return nil }
guard let createdAtString = json["created_at"] as? String else { return nil }
guard let createdAt = DateFormatter.iso8601.date(from: createdAtString) else { return nil }
self.init(profileURL: profileURL, createdAt: createdAt)
}
}
let twitter: Twitter
init(id: Int, name: String, twitter: Twitter) {
self.id = id
self.name = name
self.twitter = twitter
}
init?(json: [String: Any]) {
guard let id = json["id"] as? Int else { return nil }
guard let name = json["name"] as? String else { return nil }
guard let twitterJSONDictionary = json["twitter"] as? [String: Any] else { return nil }
guard let twitter = Twitter(json: twitterJSONDictionary) else { return nil }
self.init(id: id, name: name, twitter: twitter)
}
}

You may need a DateFormatter extension if you do not use Codable:

extension DateFormatter {
    static let iso8601: DateFormatter = {
let formatter = DateFormatter()
formatter.timeZone = TimeZone(abbreviation: "UTC")
formatter.locale = Locale(identifier: "en_US_POSIX")
formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSSSSZ"
return formatter
}()
}

For JSON:

{
"id": 42,
"name": "nixzhu",
"draft_posts": [
{
"id": 1,
"title": "Hello World!",
"body": "Hi, I'm CuteBaby."
},
{
"id": 2,
"title": "How do you do?",
"body": null
}
]
}

It’ll generate Swift code (with Codable):

struct User: Codable {
let id: Int
let name: String
struct Post: Codable {
let id: Int
let title: String
let body: String?
}
let draftPosts: [Post]
private enum CodingKeys: String, CodingKey {
case id
case name
case draftPosts = "draft_posts"
}
}

Note that there use Array Object Map draft_posts: Post to change the object name (Automatically generated will be DraftPost).

For JSON:

{
"new_users": [
{
"id": 2,
"name": "bigbigchai",
"gender": "female",
"age": 16
}
]
}

It’ll generate Swift code (with Codable):

struct NewModel: Codable {
struct User: Codable {
let id: Int
let name: String
enum Gender: String, Codable {
case male
case female
}
let gender: Gender
let age: Int
}
let newUsers: [User]
private enum CodingKeys: String, CodingKey {
case newUsers = "new_users"
}
}

Note that there use Enum Properties gender[male, female] to generate enum Gender for gender property. Also use Array Object Map new_users: User to change the object name (Automatically generated will be NewUser).


You can also paste a URL in the top right text view, CuteBaby will try to load JSON from it automatically.


Create models from a JSON string, even a CuteBaby can do it.

Get the CuteBaby on the Mac App Store.

Or SmartBaby on the Mac App Store. It’s smarter.

I wish you like it. 😀💕