{"id":17068942,"url":"https://github.com/kgn/jsonmagic","last_synced_at":"2025-04-12T18:54:07.155Z","repository":{"id":56916485,"uuid":"51990635","full_name":"kgn/JSONMagic","owner":"kgn","description":"JSONMagic makes it easy to traverse and parse JSON in Swift.","archived":false,"fork":false,"pushed_at":"2017-01-16T00:43:14.000Z","size":34,"stargazers_count":19,"open_issues_count":0,"forks_count":2,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-10T08:05:27.427Z","etag":null,"topics":["carthage","cocoapods","json","swift"],"latest_commit_sha":null,"homepage":null,"language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/kgn.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-02-18T08:01:58.000Z","updated_at":"2018-05-09T04:14:17.000Z","dependencies_parsed_at":"2022-08-21T03:50:44.057Z","dependency_job_id":null,"html_url":"https://github.com/kgn/JSONMagic","commit_stats":null,"previous_names":[],"tags_count":15,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kgn%2FJSONMagic","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kgn%2FJSONMagic/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kgn%2FJSONMagic/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kgn%2FJSONMagic/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/kgn","download_url":"https://codeload.github.com/kgn/JSONMagic/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248618262,"owners_count":21134200,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["carthage","cocoapods","json","swift"],"created_at":"2024-10-14T11:15:40.082Z","updated_at":"2025-04-12T18:54:07.134Z","avatar_url":"https://github.com/kgn.png","language":"Swift","readme":"# JSONMagic\n\n`JSONMagic` makes it easy to traverse and parse JSON in Swift.\n\n[![Swift 3](http://img.shields.io/badge/Swift-3-orange.svg)]()\n[![Release](https://img.shields.io/github/release/kgn/JSONMagic.svg)](/releases)\n[![License](http://img.shields.io/badge/License-MIT-lightgrey.svg)](/LICENSE)\n\n[![Build Status](https://travis-ci.org/kgn/JSONMagic.svg)](https://travis-ci.org/kgn/JSONMagic)\n[![Test Coverage](http://img.shields.io/badge/Tests-100%25-green.svg)]()\n[![Carthage Compatible](https://img.shields.io/badge/Carthage-Compatible-4BC51D.svg)](https://github.com/Carthage/Carthage)\n[![CocoaPods Version](https://img.shields.io/cocoapods/v/JSONMagic.svg)](https://cocoapods.org/pods/JSONMagic)\n[![CocoaPods Platforms](https://img.shields.io/cocoapods/p/JSONMagic.svg)](https://cocoapods.org/pods/JSONMagic)\n\n[![Twitter](https://img.shields.io/badge/Twitter-@iamkgn-55ACEE.svg)](http://twitter.com/iamkgn)\n[![Follow](https://img.shields.io/github/followers/kgn.svg?style=social\u0026label=Follow%20%40kgn)](https://github.com/kgn)\n[![Star](https://img.shields.io/github/stars/kgn/JSONMagic.svg?style=social\u0026label=Star)](https://github.com/kgn/JSONMagic)\n\n## Installing\n\n### Carthage\n```\ngithub \"kgn/JSONMagic\"\n```\n\n### CocoaPods\n```\npod 'JSONMagic'\n```\n\n## Examples\n\nLets say you get a JSON user profile like this from your server:\n\n``` json\n{\n    \"user\": {\n        \"name\": \"David Keegan\",\n        \"age\": 30,\n        \"accounts\": [\n            {\n                \"name\": \"twitter\",\n                \"user\": \"iamkgn\"\n            },\n            {\n                \"name\": \"dribbble\",\n                \"user\": \"kgn\"\n            },\n            {\n                \"name\": \"github\",\n                \"user\": \"kgn\"\n            }\n        ]\n    }\n}\n```\n\nParsing 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.\n\n### Before\n\n``` Swift\nlet twitterUser: String?\nif let data = serverResponse {\n    if let json = try? NSJSONSerialization.JSONObjectWithData(data, options: []) as? [String: Any] {\n        if let user = json?[\"user\"] as? [String: Any] {\n            if let accounts = user[\"accounts\"] as? [Any] {\n                if let twitter = accounts.first as? [String: Any] {\n                    twitterUser = twitter[\"user\"] as? String\n                }\n            }\n        }\n    }\n}\n```\n\n### After\n\n``` Swift\nlet twitterUser = JSONMagic(data: serverResponse).get(\"user\").get(\"accounts\").first.get(\"user\").string\n```\n\nOr, if you prefer subscripting :)\n\n``` Swift\nlet twitterUser = JSONMagic(data: serverResponse)[\"user\"][\"accounts\"][0][\"user\"].string\n```\n\nIt even works with [Paw](https://paw.cloud) key paths.\n\n``` Swift\nlet twitterUser = JSONMagic(data: serverResponse).keypath(\"user.accounts.0.user\").string\n```\n\n`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`.\n\nIt’s super *loosie goosie* so doesn’t care about `nil` values going in, or anywhere in the chain.\n\n### Some more examples\n\n``` Swift\nlet json = JSONMagic(data: serverResponse)\n\njson.get(\"user\").get(\"name\").string // David Keegan\njson[\"user\"][\"age\"].integer // 30\n\nlet twitter = json.get(\"user\").get(\"accounts\").first\ntwitter[\"name\"].value // twitter\ntwitter[\"user\"].value // iamkgn\n\nlet dribbble = json.get(\"user\").get(\"accounts\").get(1)\ndribbble.get(\"name\").value // dribbble\ndribbble.get(\"user\").value // kgn\n\nlet github = json.get(\"user\").get(\"accounts\").last\ngithub.get(\"name\").value // github\ngithub.get(\"user\").value // kgn\n\nlet bad = json.get(\"user\").get(\"accounts\").get(5)\nbad.get(\"name\").value // nil\nbad.get(\"user\").value // nil\n```\n\n## Progress\n- [X] Badges\n- [X] Tests\n- [X] Travis\n- [X] Carthage\n- [X] CocoaPods\n- [X] Description\n- [X] Documentation\n- [X] AppleTV\n- [X] AppleWatch\n- [X] Prebuilt Frameworks\n- [ ] Travis Test Matrix\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkgn%2Fjsonmagic","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkgn%2Fjsonmagic","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkgn%2Fjsonmagic/lists"}