Swift
Swift
Insert JSON Object into another JSON Object
See more JSON Examples
Demonstrates how to insert one JSON object into another. Effectively, the JSON object must be copied into the other..Chilkat Swift Downloads
func chilkatTest() {
// Imagine we have two separate JSON objects.
let jsonA = CkoJsonObject()!
jsonA.updateString(jsonPath: "animal", value: "zebra")
jsonA.updateString(jsonPath: "colors[0]", value: "white")
jsonA.updateString(jsonPath: "colors[1]", value: "black")
jsonA.emitCompact = false
print("\(jsonA.emit()!)")
// jsonA contains:
// {
// "animal": "zebra",
// "colors": [
// "white",
// "black"
// ]
// }
let jsonB = CkoJsonObject()!
jsonB.updateString(jsonPath: "type", value: "mammal")
jsonB.updateBool(jsonPath: "carnivore", value: false)
jsonB.emitCompact = false
print("\(jsonB.emit()!)")
// jsonB contains:
// {
// "type": "mammal",
// "carnivore": false
// }
// Let's say we want to insert jsonB into jsonA to get this:
// {
// "animal": "zebra",
// "info" " {
// "type": "mammal",
// "carnivore": false
// },
// "colors": [
// "white",
// "black"
// ]
// }
jsonA.addObjectCopy(at: 1, name: "info", jsonObj: jsonB)
print("\(jsonA.emit()!)")
// The result is this:
// {
// "animal": "zebra",
// "info": {
// "type": "mammal",
// "carnivore": false
// },
// "colors": [
// "white",
// "black"
// ]
// }
}