Sample code for 30+ languages & platforms
Swift

Insert JSON Array into another JSON Object

See more JSON Examples

Demonstrates how to insert a JSON array into a JSON object.

Chilkat Swift Downloads

Swift

func chilkatTest() {
    // Imagine we have two separate JSON objects.
    let jsonA = CkoJsonObject()!
    jsonA.updateString(jsonPath: "ciphertext", value: "encryptedData")
    jsonA.updateInt(jsonPath: "status", value: 200)
    jsonA.updateString(jsonPath: "error", value: "errorMsg")

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

    // jsonA contains:

    // {
    //   "ciphertext": "encryptedData",
    //   "status": 200,
    //   "error": "errorMsg"
    // }

    let jsonB = CkoJsonObject()!
    jsonB.updateString(jsonPath: "users[0].role", value: "Surgeon")
    jsonB.updateNewArray(jsonPath: "users[0].sub_roles")
    jsonB.updateBool(jsonPath: "users[0].viewable_for_sharing", value: true)
    jsonB.updateInt(jsonPath: "users[0].eula_create_date", value: 123)
    jsonB.updateString(jsonPath: "users[1].role", value: "Support")
    jsonB.updateString(jsonPath: "users[1].sub_roles[0]", value: "Tech")
    jsonB.updateString(jsonPath: "users[1].sub_roles[1]", value: "Service")
    jsonB.updateBool(jsonPath: "users[1].viewable_for_sharing", value: true)
    jsonB.updateInt(jsonPath: "users[1].eula_create_date", value: 123)

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

    // jsonB contains:

    // {
    //   "users": [
    //     {
    //       "role": "Surgeon",
    //       "sub_roles": [],
    //       "viewable_for_sharing": true,
    //       "eula_create_date": 1649108922482
    //     },
    //     {
    //       "role": "Support",
    //       "sub_roles": [
    //         "Tech",
    //         "Service"
    //       ],
    //       "viewable_for_sharing": true,
    //       "eula_create_date": 1649108951523
    //     }
    //   ]
    // }

    // Let's say we want to insert jsonB into jsonA to get this:

    // {
    //   "ciphertext": "encryptedData",
    //   "status": 200,
    //   "error": "errorMsg",
    //   "users": [
    //     {
    //       "role": "Surgeon",
    //       "sub_roles": [],
    //       "viewable_for_sharing": true,
    //       "eula_create_date": 1649108922482
    //     },
    //     {
    //       "role": "Support",
    //       "sub_roles": [
    //         "Tech",
    //         "Service"
    //       ],
    //       "viewable_for_sharing": true,
    //       "eula_create_date": 1649108951523
    //     }
    //   ]
    // }

    // The destination is the empty "users" array, the source is the populated "users" array in jsonB.
    let jsonUsersDest = CkoJsonArray()!
    jsonA.appendArray2(name: "users", jarr: jsonUsersDest)

    let jsonUsersSrc = CkoJsonArray()!
    jsonB.arrayOf2(jsonPath: "users", jarr: jsonUsersSrc)

    // Copy the array items from source to dest
    jsonUsersDest.appendArrayItems(jarr: jsonUsersSrc)

    print("\(jsonA.emit()!)")

    // The end result is this:

    // {
    //   "ciphertext": "encryptedData",
    //   "status": 200,
    //   "error": "errorMsg",
    //   "users": [
    //     {
    //       "role": "Surgeon",
    //       "sub_roles": [
    //       ],
    //       "viewable_for_sharing": true,
    //       "eula_create_date": 123
    //     },
    //     {
    //       "role": "Support",
    //       "sub_roles": [
    //         "Tech",
    //         "Service"
    //       ],
    //       "viewable_for_sharing": true,
    //       "eula_create_date": 123
    //     }
    //   ]
    // }

}