Sample code for 30+ languages & platforms
Swift

Firebase JSON Put and Patch

See more JSON Examples

Demonstrates how to apply Firebase put and patch events to a JSON database.

Chilkat Swift Downloads

Swift

func chilkatTest() {
    var json1: String? = "{\"a\": 1, \"b\": 2}"

    let json = CkoJsonObject()!

    // Use Firebase delimiters for JSON paths.
    json.delimiterChar = "/"

    json.load(json: json1)
    json.firebasePut(jsonPath: "/c", value: "{\"foo\": true, \"bar\": false}")
    // Output should be: {"a":1,"b":2,"c":{"foo":true,"bar":false}}
    print("1) \(json.emit()!)")

    json.firebasePut(jsonPath: "/c", value: "\"hello world\"")
    // Output should be: {"a":1,"b":2,"c":"hello world"}
    print("2) \(json.emit()!)")

    json.firebasePut(jsonPath: "/c", value: "{\"foo\": \"abc\", \"bar\": 123}")
    // Output should be: {"a":1,"b":2,"c":{"foo":"abc","bar":123}}
    print("3) \(json.emit()!)")

    // Back to the original..
    json.firebasePut(jsonPath: "/", value: "{\"a\": 1, \"b\": 2}")
    print("4) \(json.emit()!)")

    json.firebasePut(jsonPath: "/c", value: "{\"foo\": true, \"bar\": false}")
    json.firebasePatch(jsonPath: "/c", jsonData: "{\"foo\": 3, \"baz\": 4}")
    // Output should be: {"a":1,"b":2,"c":{"foo":3,"bar":false,"baz":4}}
    print("5) \(json.emit()!)")

    json.firebasePatch(jsonPath: "/c", jsonData: "{\"foo\": \"abc123\", \"baz\": {\"foo\": true, \"bar\": false}, \"bax\": {\"foo\": 200, \"bar\": 400} }")
    // Output should be: {"a":1,"b":2,"c":{"foo":"abc123","bar":false,"baz":{"foo":true,"bar":false},"bax":{"foo":200,"bar":400}}}
    print("6) \(json.emit()!)")

}