Help us understand the problem. What is going on with this article?


More than 5 years have passed since last update.




※ String, Int, Float, Double, Array, Dictinary, etc

SwiftyJSON/SwiftyJSON ★3526

The better way to deal with JSON data in Swift.

Non-optional getterVALUEnilの場合でもデフォルト値で扱えるのは便利です。Arrayに要素数を超えた添字でアクセスしてもクラッシュせずにerrorオブジェクトを返すところも親切 です。


let json = JSON(data: dataFromNetworking)

// or

let json = JSON(jsonObject)

//With a int from JSON supposed to an Array
let name = json[0].double

//With a string from JSON supposed to an Dictionary
let name = json["name"].stringValue

//With an array like path to the element
let path = [1,"list",2,"name"]
let name = json[path].string 
//Just the same
let name = json[1]["list"][2]["name"].string

//With a literal array to the element
let name = json[1,"list",2,"name"].string 
//Just the same
let name = json[1]["list"][2]["name"].string

//With a Hard Way
let name = json[[1,"list",2,"name"]].string

SwiftyJSON/Alamofire-SwiftyJSON ★308

Alamofire extension for serialize NSData to SwiftyJSON



Alamofire.request(.GET, "", parameters: ["foo": "bar"])
         .responseSwiftyJSON { (request, response, json, error) in

owensd/json-swift ★546

A basic library for working with JSON in Swift.



if let stat = json["stat"].string {
    println("stat = '\(stat)'")
    // prints: stat = 'ok'

// Retrieve error information from a missing key lookup
let stat = json["stats"]
if let value = stat.string {
    println("stat = '\(value)'")
else if let error = stat.error {
    println("code: \(error.code), domain: '\(error.domain)', info: '\(error.userInfo[LocalizedDescriptionKey]!)'")
    // prints: code: 6, domain: 'com.kiadsoftware.json.error', info: 'There is no value stored with key: 'stats'.'

// Iterate over the contents of an array**
if let blogs = json["blogs"]["blog"].array {
    for blog in blogs {
        println("blog: \(blog)")

thoughtbot/Argo ★490

Functional JSON parsing library for Swift




struct User {
  let id: Int
  let name: String
  let email: String?
  let role: Role
  let companyName: String
  let friends: [User]

extension User: JSONDecodable {
  static func create(id: Int)(name: String)(email: String?)(role: Role)(companyName: String)(friends: [User]) -> User {
    return User(id: id, name: name, email: email, role: role, companyName: companyName, friends: friends)

  static func decode(j: JSONValue) -> User? {
    return User.create
      <^> j <| "id"
      <*> j <| "name"
      <*> j <|? "email" // Use ? for parsing optional values
      <*> j <| "role" // Custom types that also conform to JSONDecodable just work
      <*> j <| ["company", "name"] // Parse nested objects
      <*> j <|| "friends" // parse arrays of objects

// Wherever you receive JSON data:

let json: AnyObject? = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions(0), error: .None)

if let j: AnyObject = json {
  let value = JSONValue.parse(j)
  let user = User.decode(value)

Hearst-DD/ObjectMapper ★428

JSON Object mapping written in Swift.



class User: Mappable {
    var username: String?
    var age: Int?
    var weight: Double!
    var array: [AnyObject]?
    var dictionary: [String : AnyObject] = [:]
    var bestFriend: User?                       // Nested User object
    var friends: [User]?                        // Array of Users
    var birthday: NSDate?

    required init?(_ map: Map) {

    // Mappable
    func mapping(map: Map) {
        username    <- map["username"]
        age         <- map["age"]
        weight      <- map["weight"]
        arrary      <- map["arr"]
        dictionary  <- map["dict"]
        best_friend <- map["best_friend"]
        friends     <- map["friends"]
        birthday    <- (map["birthday"], DateTransform())

let user = Mapper<User>().map(string: JSONString)

struct Temperature: Mappable {
    var celcius: Double?
    var fahrenheit: Double?


    init?(_ map: Map) {

    mutating func mapping(map: Map) {
        celcius     <- map["celcius"]
        fahrenheit  <- map["fahrenheit"]

isair/JSONHelper ★375

Lightning fast JSON deserialization and value conversion library for iOS & OS X written in Swift.




    "books": [
            "author": "Irvine Welsh",
            "name": "Filth"     
            "author": "Bret Easton Ellis",
            "name": "American Psycho"
class Book: Deserializable {
    var author: String? // You can also use let instead of var if you want.
    var name: String?

    required init(data: [String: AnyObject]) {
        author <<< data["author"]
        name <<< data["name"]
    parameters: nil,
    success: { operation, data in
        var books: [Book]?
        books <<<<* data["books"]

        if let books = books {
            // Response contained a books array, and we deserialized it. Do what you want here.
        } else {
            // Server gave us a response but there was no books key in it, so the books variable
            // is equal to nil. Do some error handling here.
    failure: { operation, error in
        // Handle error.

dankogai/swift-json ★373

Even Swiftier JSON Handler




let obj:[String:AnyObject] = [
    "array": [JSON.null, false, 0, "", [], [:]],
        "null":   JSON.null,
        "bool":   true,
        "int":    42,
        "double": 3.141592653589793,
        "string": "a α\t\n?",
        "array":  [],
        "object": [:]

let json = JSON(obj)

json["object"]["null"].asNull       // NSNull()
json["object"]["bool"].asBool       // true
json["object"]["int"].asInt         // 42
json["object"]["double"].asDouble   // 3.141592653589793
json["object"]["string"].asString   // "a α\t弾\n?"

json["array"][0].asNull             // NSNull()
json["array"][1].asBool             // false
json["array"][2].asInt              // 0
json["array"][3].asString           // ""

Ahmed-Ali/JSONExport ★410

JSONExport is a desktop application for Mac OS X which enables you to export JSON objects as model classes


  1. Swift Classes.
  2. Swift Classes - To use with SwiftyJSON library.
  3. Swift Classes - To use with Realm.
  4. Swift - CoreData.
  5. Swift Sturcutres.
  6. Objective-C - iOS.
  7. Objective-C - MAC.
  8. Objective-C - CoreData.
  9. Objective-C - To use with Realm.


Ref Swift Networking Library

Ref ObjC JSON Library

フリーランス - スタートアップエンジニアリングアドバイザー - iOS技術顧問 - プロトタイプ開発
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away