BackspaceJSON 1.0.0

BackspaceJSON 1.0.0

Maintained by Mark Smith.


Codable has made consuming JSON on iOS much easier, but there are still cases where it's not suitable, such as a configuration file with arbitrary keys and values. In those cases, you just want type-safe access to values without a lot of fuss.

BackspaceJSON is a tiny (single enum, ~100 lines) framework written in Swift.


init(data: Data, options: JSONSerialization.ReadingOptions = .allowFragments) throws

Create a JSON object from data.

let json = JSON(data: data)


subscript(index: Int) -> JSON
subscript(key: String) -> JSON

Use string and integer subscripts to traverse any path through the JSON object. Subscripts always return another JSON object, so optional chaining isn't required.



var dictionary: [String: JSON]?
var array: [JSON]?
var string: String?
var number: NSNumber?
var double: Double?
var int: Int?
var bool: Bool?

Extract the value from a JSON object using the optional properties.



var exists: Bool
var existsNull: Bool
var existsNotNull: Bool

BackspaceJSON distinguishes between null and missing values.

json["one"]["two"].exists        // If true, a value exists at this path (possibly null).
json["one"]["two"].existsNull    // If true, a null value exists at this path.
json["one"]["two"].existsNotNull // If true, a non-null value exists at this path.


This code fetches the current version of an app from from the App Store.

let url = URL(string: "<YOUR_APP_ID>")!
let task = URLSession.shared.dataTask(with: url) { data, response, error in
  guard let data = data,
        let json = try? JSON(data: data),
        let currentVersion = json["results"][0]["version"].string else {
    print("Lookup failed.")

  print("Current version is \(currentVersion).")



Install via CocoaPods:

pod "BackspaceJSON"

Alternatively, just copy the single file BackspaceJSON/JSON.swift into your project.


MIT license.