Installation • Usage • License
DynamicJSON is a dynamically typed JSON parser built upon the new @dynamicMemberLookup 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.
iflet jsonObject =try?JSONSerialization.jsonObject(with: data, options:[])as?[String:Any],let user =jsonObject["user"]as?[String:Any],let username =user["username"]as?String{
// ...
}letusername=JSON(data).user.username.stringpod'DynamicJSON','~> 2.0.2'(if you run into problems, pod repo update and try again)
github"saoudrizwan/DynamicJSON"- Or drag and drop
DynamicJSON.swiftinto your project.
And import DynamicJSON in the files you'd like to use it.
Throw Anything into a JSON object to get started
letjson=JSON(Data())JSON(123)JSON(["key":"value"])JSON(["element",1])JSON("Hello world")JSON(false)...or cast a literal as JSON
letjson="Hello world"asJSON123asJSON[1,2,3]asJSONletuser:JSON=["username":"Saoud","age":21,"address":["zip":"12345","city":"San Diego"]]Treat JSON objects like you're in JavaScript Land
letdictionary= json.dictionary
letarray=json[0].cars.array
letstring= json.users[1].username.string
letnsnumber= json.creditCard.pin.number
letdouble=json[3][1].height.double
letint=json[0].age.int
letbool= json.biography.isHuman.boolNote 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.
In 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:
letnumber= json.account.contact["number"].number
letdescription= json.user.biography["description"].stringJSON conforms to Comparable
letjson1=JSON(jsonData1)letjson2=JSON(jsonData2)
// Equality applies to all types (Dictionary, Array, String, NSNumber, Bool, NSNull)
letisEqual= json1 == json2
// Less/greater than only applies to NSNumbers (Double, Int) and Strings
letisLessThan= json1 < json2
letisLessThanOrEqual= json1 <= json2
letisGreaterThan= json1 > json2
letisGreaterThanOrEqual= json1 >= json2Pretty print for debug purposes
print(json)Convert to raw object
letanyObject= json.objectConvert to Data
letdata= json.data() // optionally specify options...DynamicJSON 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.
DynamicJSON 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.
Contact me by email hello@saoudmr.com, or by twitter @sdrzn. Please create an issue if you come across a bug or would like a feature to be added.
- Paul Hudson's wonderful literature over
@dynamicMemberLookup - Chris Lattner's Swift Evolution Propsal SE-0195
- SwiftyJSON for giving me a better idea of what the community wants out of a JSON parser
- Helge Heß for helping remove the need to use optional chaining (before v2.0.0)



