{"id":13618356,"url":"https://github.com/saoudrizwan/DynamicJSON","last_synced_at":"2025-04-14T10:31:43.549Z","repository":{"id":56909208,"uuid":"163785510","full_name":"saoudrizwan/DynamicJSON","owner":"saoudrizwan","description":"Access JSON properties dynamically like JavaScript using Swift 4.2's new @dynamicMemberLookup feature","archived":false,"fork":false,"pushed_at":"2022-04-03T01:52:53.000Z","size":48,"stargazers_count":716,"open_issues_count":3,"forks_count":22,"subscribers_count":23,"default_branch":"master","last_synced_at":"2024-10-30T03:40:16.493Z","etag":null,"topics":["ios","ios-swift","macos","swift","swift4","swift4-2","tvos","watchos"],"latest_commit_sha":null,"homepage":"https://twitter.com/sdrzn/status/1080340307802173440","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/saoudrizwan.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":"2019-01-02T02:35:54.000Z","updated_at":"2024-10-25T08:40:22.000Z","dependencies_parsed_at":"2022-08-20T19:50:37.232Z","dependency_job_id":null,"html_url":"https://github.com/saoudrizwan/DynamicJSON","commit_stats":null,"previous_names":[],"tags_count":9,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/saoudrizwan%2FDynamicJSON","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/saoudrizwan%2FDynamicJSON/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/saoudrizwan%2FDynamicJSON/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/saoudrizwan%2FDynamicJSON/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/saoudrizwan","download_url":"https://codeload.github.com/saoudrizwan/DynamicJSON/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248862682,"owners_count":21173860,"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":["ios","ios-swift","macos","swift","swift4","swift4-2","tvos","watchos"],"created_at":"2024-08-01T20:01:59.451Z","updated_at":"2025-04-14T10:31:42.742Z","avatar_url":"https://github.com/saoudrizwan.png","language":"Swift","readme":"\u003cp align=\"center\"\u003e\n    \u003cimg src=\"https://user-images.githubusercontent.com/7799382/50582031-179fe380-0e14-11e9-9f16-bc8b8c69b41b.png\" alt=\"DynamicJSON\" /\u003e\n\u003c/p\u003e\n\u003cbr\u003e\n\u003cp align=\"center\"\u003e\n    \u003cimg src=\"https://user-images.githubusercontent.com/7799382/50578326-80279a00-0ded-11e9-8cfc-5cf45f70bbab.png\" alt=\"Platform: iOS, macOS, watchOS, tvOS\" /\u003e\n    \u003ca href=\"https://developer.apple.com/swift\" target=\"_blank\"\u003e\u003cimg src=\"https://user-images.githubusercontent.com/7799382/50578324-80279a00-0ded-11e9-9526-5e548f86e500.png\" alt=\"Language: Swift 4.2\" /\u003e\u003c/a\u003e\n    \u003cimg src=\"https://user-images.githubusercontent.com/7799382/50578325-80279a00-0ded-11e9-8a53-2c56bd762880.png\" alt=\"License: MIT\" /\u003e\n\u003c/p\u003e\n\n\u003cp align=\"center\"\u003e\n    \u003ca href=\"#installation\"\u003eInstallation\u003c/a\u003e\n  • \u003ca href=\"#usage\"\u003eUsage\u003c/a\u003e\n  • \u003ca href=\"#license\"\u003eLicense\u003c/a\u003e\n\u003c/p\u003e\n\nDynamicJSON is a **dynamically typed** JSON parser built upon the new \u003ca href=\"https://github.com/apple/swift-evolution/blob/master/proposals/0195-dynamic-member-lookup.md\" target=\"_blank\"\u003e`@dynamicMemberLookup`\u003c/a\u003e feature introduced by Chris Lattner in Swift 4.2. This allows us to access arbitrary object members which are resolved at runtime, allowing Swift to be as flexible as JavaScript when it comes to JSON.\n\n### Before\n\n```swift\nif let jsonObject = try? JSONSerialization.jsonObject(with: data, options: []) as? [String: Any],\n   let user = jsonObject[\"user\"] as? [String: Any],\n   let username = user[\"username\"] as? String {\n\t// ...\n}\n```\n\n### After\n\n```swift\nlet username = JSON(data).user.username.string\n```\n\n## Installation\n\n-   \u003ca href=\"https://guides.cocoapods.org/using/using-cocoapods.html\" target=\"_blank\"\u003eCocoaPods\u003c/a\u003e:\n\n```ruby\npod 'DynamicJSON', '~\u003e 2.0.2'\n```\n*(if you run into problems, `pod repo update` and try again)*\n\n-   \u003ca href=\"https://github.com/Carthage/Carthage\" target=\"_blank\"\u003eCarthage\u003c/a\u003e:\n\n```ruby\ngithub \"saoudrizwan/DynamicJSON\"\n```\n\n-   Or drag and drop `DynamicJSON.swift` into your project.\n\nAnd `import DynamicJSON` in the files you'd like to use it.\n\n## Usage\n\n### 1. Initialize 🐣\n\nThrow `Any`thing into a `JSON` object to get started\n\n```swift\nlet json = JSON(Data())\n           JSON(123)\n           JSON([\"key\": \"value\"])\n           JSON([\"element\", 1])\n           JSON(\"Hello world\")\n           JSON(false)\n```\n\n...or cast a literal as `JSON`\n\n```swift\nlet json = \"Hello world\" as JSON\n           123 as JSON\n           [1, 2, 3] as JSON\n\nlet user: JSON = [\n\t\"username\": \"Saoud\",\n\t\"age\": 21,\n\t\"address\": [\n\t    \"zip\": \"12345\",\n\t    \"city\": \"San Diego\"\n\t]\n]\n```\n\n### 2. Drill in ⛏\n\nTreat `JSON` objects like you're in JavaScript Land\n\n```swift\nlet dictionary = json.dictionary\nlet array = json[0].cars.array\nlet string = json.users[1].username.string\nlet nsnumber = json.creditCard.pin.number\nlet double = json[3][1].height.double\nlet int = json[0].age.int\nlet bool = json.biography.isHuman.bool\n```\n\nNote how `JSON` doesn't actually have properties like `cars` or `users`, instead it uses dynamic member lookup to traverse through its associated JSON data to find the object you're looking for.\n\nIn case you have a key that's an actual property of `JSON`, like `number` or `description` for example, just use the string subscript accessor like so:\n```swift\nlet number = json.account.contact[\"number\"].number\nlet description = json.user.biography[\"description\"].string\n```\n\n### 3. Have fun 🤪\n\n`JSON` conforms to `Comparable`\n\n```swift\nlet json1 = JSON(jsonData1)\nlet json2 = JSON(jsonData2)\n\n// Equality applies to all types (Dictionary, Array, String, NSNumber, Bool, NSNull)\nlet isEqual = json1 == json2\n\n// Less/greater than only applies to NSNumbers (Double, Int) and Strings\nlet isLessThan = json1 \u003c json2\nlet isLessThanOrEqual = json1 \u003c= json2\nlet isGreaterThan = json1 \u003e json2\nlet isGreaterThanOrEqual = json1 \u003e= json2\n```\n\nPretty print for debug purposes\n\n```swift\nprint(json)\n```\n\nConvert to raw object\n\n```swift\nlet anyObject = json.object\n```\n\nConvert to `Data`\n\n```swift\nlet data = json.data() // optionally specify options...\n```\n\n## License\n\nDynamicJSON uses the MIT license. Please file an issue if you have any questions or if you'd like to share how you're using DynamicJSON.\n\n## Contribute\n\nDynamicJSON is in its infancy, but provides the barebones of a revolutionary new way to work with JSON in Swift. Please feel free to send pull requests of any features you think would add to DynamicJSON and its philosophy.\n\n## Questions?\n\nContact me by email \u003ca href=\"mailto:hello@saoudmr.com\"\u003ehello@saoudmr.com\u003c/a\u003e, or by twitter \u003ca href=\"https://twitter.com/sdrzn\" target=\"_blank\"\u003e@sdrzn\u003c/a\u003e. Please create an \u003ca href=\"https://github.com/saoudrizwan/DynamicJSON/issues\"\u003eissue\u003c/a\u003e if you come across a bug or would like a feature to be added.\n\n## Notable Mentions\n\n* [Paul Hudson](https://www.hackingwithswift.com/)'s wonderful literature over `@dynamicMemberLookup`\n* Chris Lattner's Swift Evolution Propsal [SE-0195](https://github.com/apple/swift-evolution/blob/master/proposals/0195-dynamic-member-lookup.md)\n* [SwiftyJSON](https://github.com/SwiftyJSON/SwiftyJSON) for giving me a better idea of what the community wants out of a JSON parser\n* [Helge Heß](https://github.com/helje5) for helping [remove the need to use optional chaining](https://github.com/saoudrizwan/DynamicJSON/issues/4) (before v2.0.0)\n","funding_links":[],"categories":["HarmonyOS","Swift"],"sub_categories":["Windows Manager"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsaoudrizwan%2FDynamicJSON","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsaoudrizwan%2FDynamicJSON","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsaoudrizwan%2FDynamicJSON/lists"}