Sample code for 30+ languages & platforms
Swift

Modify Parts of JSON Document

See more JSON Examples

Demonstrates how to modify parts of a JSON document. This example uses the following JSON document:
{
   "fruit": [
      	{
         "kind": "apple",
	 "count": 24,
	 "fresh": true,
	 "extraInfo": null,
	 "listA": [ "abc", 1, null, false ],
	 "objectB": { "animal" : "monkey" }
      	},
	{
         "kind": "pear",
	 "count": 18,
	 "fresh": false,
	 "extraInfo": null
	 "listA": [ "xyz", 24, null, true ],
	 "objectB": { "animal" : "lemur" }
	}
    ],
    "list" : [ "banana", 12, true, null, "orange", 12.5, { "ticker": "AAPL" }, [ 1, 2, 3, 4, 5 ] ],
    "alien" : true
}

Chilkat Swift Downloads

Swift

func chilkatTest() {
    var success: Bool = false

    let json = CkoJsonObject()!

    // Load the JSON from a file.
    success = json.loadFile(path: "qa_data/json/modifySample.json")
    if success == false {
        print("\(json.lastErrorText!)")
        return
    }

    // This example will not check for errors (i.e. null / false / 0 return values)...

    // Get the "list" array:

    let listA = CkoJsonArray()!

    json.arrayOf2(jsonPath: "list", jarr: listA)

    // Modify values in the list.

    // Change banana to plantain
    success = listA.setString(at: 0, value: "plantain")

    // Change 12 to 24
    success = listA.setInt(at: 1, value: 24)

    // Change true to false
    success = listA.setBool(at: 2, value: false)

    // Is the 3rd item null?
    var bNull: Bool = listA.isNull(at: 3)

    // Change "orange" to 32.
    success = listA.setInt(at: 4, value: 32)

    // Change 12.5 to 31.2
    success = listA.setNumber(at: 5, value: "31.2")

    // Replace the { "ticker" : "AAPL" } object with { "ticker" : "GOOG" }
    // Do this by deleting, then inserting a new object at the same location.
    success = listA.delete(at: 6)
    let tickerObj = CkoJsonObject()!
    listA.addObjectAt2(index: 6, json: tickerObj)

    success = tickerObj.appendString(name: "ticker", value: "GOOG")

    // Replace "[ 1, 2, 3, 4, 5 ]" with "[ "apple", 22, true, null, 1080.25 ]"
    success = listA.delete(at: 7)
    let aa = CkoJsonArray()!
    listA.addArrayAt2(index: 7, jarr: aa)

    success = aa.addString(at: -1, value: "apple")
    success = aa.addInt(at: -1, value: 22)
    success = aa.addBool(at: -1, value: true)
    success = aa.addNull(at: -1)
    success = aa.addNumber(at: -1, numericStr: "1080.25")

    // Get the "fruit" array
    let aFruit = CkoJsonArray()!
    json.arrayAt2(index: 0, jarr: aFruit)

    // Get the 1st element:
    let appleObj = CkoJsonObject()!
    aFruit.objectAt2(index: 0, jsonObj: appleObj)

    // Modify values by member name:
    success = appleObj.setString(of: "fruit", value: "fuji_apple")
    success = appleObj.setInt(of: "count", value: 46)
    success = appleObj.setBool(of: "fresh", value: false)
    success = appleObj.setString(of: "extraInfo", value: "developed by growers at the Tohoku Research Station in Fujisaki")

    // Modify values by index:

    let pearObj = CkoJsonObject()!
    aFruit.objectAt2(index: 1, jsonObj: pearObj)

    success = pearObj.setString(at: 0, value: "bartlett_pear")
    success = pearObj.setInt(at: 1, value: 12)
    success = pearObj.setBool(at: 2, value: false)
    success = pearObj.setString(at: 3, value: "harvested in late August to early September")

    json.emitCompact = false
    print("\(json.emit()!)")

}