Latest 0.7.5
Homepage https://github.com/kgn/JSONMagic
License MIT
Platforms ios 8.0, requires ARC
Authors

JSONMagic makes it easy to traverse and parse JSON in Swift.

[Swift 3]()
Release
License

Build Status
[Test Coverage]()
Carthage Compatible
CocoaPods Version
CocoaPods Platforms

Twitter
Follow
Star

Installing

Carthage

github "kgn/JSONMagic"

CocoaPods

pod 'JSONMagic'

Examples

Lets say you get a JSON user profile like this from your server:

{
    "user": {
        "name": "David Keegan",
        "age": 30,
        "accounts": [
            {
                "name": "twitter",
                "user": "iamkgn"
            },
            {
                "name": "dribbble",
                "user": "kgn"
            },
            {
                "name": "github",
                "user": "kgn"
            }
        ]
    }
}

Parsing this can take a bunch of nested if statements in Swift to cast things to the right type in order to traverse down the data tree.

Before

let twitterUser: String?
if let data = serverResponse {
    if let json = try? NSJSONSerialization.JSONObjectWithData(data, options: []) as? [String: Any] {
        if let user = json?["user"] as? [String: Any] {
            if let accounts = user["accounts"] as? [Any] {
                if let twitter = accounts.first as? [String: Any] {
                    twitterUser = twitter["user"] as? String
                }
            }
        }
    }
}

After

let twitterUser = JSONMagic(data: serverResponse).get("user").get("accounts").first.get("user").string

Or, if you prefer subscripting :)

let twitterUser = JSONMagic(data: serverResponse)["user"]["accounts"][0]["user"].string

It even works with Paw key paths.

let twitterUser = JSONMagic(data: serverResponse).keypath("user.accounts.0.user").string

JSONMagic handles all of this for you with method chaining. So you’re always working with a magical wrapper JSONMagic object that you can chain as long as you want, then just call value at the end to get the ending value and cast that to the final type you want. There are helpers for all the JSON data types too: .bool, .int, .float, .double, .string, .array and .dictionary.

It’s super loosie goosie so doesn’t care about nil values going in, or anywhere in the chain.

Some more examples

let json = JSONMagic(data: serverResponse)

json.get("user").get("name").string // David Keegan
json["user"]["age"].integer // 30

let twitter = json.get("user").get("accounts").first
twitter["name"].value // twitter
twitter["user"].value // iamkgn

let dribbble = json.get("user").get("accounts").get(1)
dribbble.get("name").value // dribbble
dribbble.get("user").value // kgn

let github = json.get("user").get("accounts").last
github.get("name").value // github
github.get("user").value // kgn

let bad = json.get("user").get("accounts").get(5)
bad.get("name").value // nil
bad.get("user").value // nil

Progress

  • [X] Badges
  • [X] Tests
  • [X] Travis
  • [X] Carthage
  • [X] CocoaPods
  • [X] Description
  • [X] Documentation
  • [X] AppleTV
  • [X] AppleWatch
  • [X] Prebuilt Frameworks
  • [ ] Travis Test Matrix

Latest podspec

{
    "name": "JSONMagic",
    "version": "0.7.5",
    "authors": {
        "David Keegan": "[email protected]"
    },
    "homepage": "https://github.com/kgn/JSONMagic",
    "summary": "JSONMagic makes it easy to traverse and parse json in Swift.",
    "source": {
        "git": "https://github.com/kgn/JSONMagic.git",
        "tag": "v0.7.5"
    },
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "platforms": {
        "ios": "8.0"
    },
    "requires_arc": true,
    "source_files": "Source/JSONMagic.swift"
}

Pin It on Pinterest

Share This