Sample code for 30+ languages & platforms
Swift

Build JSON with Mixture of Arrays and Objects

See more JSON Examples

Another example showing how to build JSON containing a mixture of arrays and objects.

Chilkat Swift Downloads

Swift

func chilkatTest() {
    // We want to build the following JSON:

    // { 
    //   "accountEnabled": true,
    //   "assignedLicenses": [
    //     { 
    //       "disabledPlans": [ "bea13e0c-3828-4daa-a392-28af7ff61a0f" ],
    //       "skuId": "skuId-value"
    //     }
    //   ],
    //   "assignedPlans": [
    //     { 
    //       "assignedDateTime": "datetime-value",
    //       "capabilityStatus": "capabilityStatus-value",
    //       "service": "service-value",
    //       "servicePlanId": "bea13e0c-3828-4daa-a392-28af7ff61a0f"
    //     }
    //   ],
    //   "businessPhones": [
    //     "businessPhones-value"
    //   ],
    //   "city": "city-value",
    //   "companyName": "companyName-value"
    // }

    let json = CkoJsonObject()!
    json.updateBool(jsonPath: "accountEnabled", value: true)
    json.i = 0
    json.updateString(jsonPath: "assignedLicenses[i].disabledPlans[0]", value: "bea13e0c-3828-4daa-a392-28af7ff61a0f")
    json.updateString(jsonPath: "assignedLicenses[i].skuId", value: "skuId-value")
    json.updateString(jsonPath: "assignedPlans[i].assignedDateTime", value: "datetime-value")
    json.updateString(jsonPath: "assignedPlans[i].capabilityStatus", value: "capabilityStatus-value")
    json.updateString(jsonPath: "assignedPlans[i].service", value: "service-value")
    json.updateString(jsonPath: "assignedPlans[i].servicePlanId", value: "bea13e0c-3828-4daa-a392-28af7ff61a0f")
    json.updateString(jsonPath: "businessPhones[i]", value: "businessPhones-value")
    json.updateString(jsonPath: "city", value: "city-value")
    json.updateString(jsonPath: "companyName", value: "companyName-value")

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

    // Output:

    // { 
    //   "accountEnabled": true,
    //   "assignedLicenses": [
    //     { 
    //       "disabledPlans": [
    //         "bea13e0c-3828-4daa-a392-28af7ff61a0f"
    //       ],
    //       "skuId": "skuId-value"
    //     }
    //   ],
    //   "assignedPlans": [
    //     { 
    //       "assignedDateTime": "datetime-value",
    //       "capabilityStatus": "capabilityStatus-value",
    //       "service": "service-value",
    //       "servicePlanId": "bea13e0c-3828-4daa-a392-28af7ff61a0f"
    //     }
    //   ],
    //   "businessPhones": [
    //     "businessPhones-value"
    //   ],
    //   "city": "city-value",
    //   "companyName": "companyName-value"
    // }

}