Setting a JSON Document

For procedures for setting a JSON document in a KiiObject, see Creating a KiiObject or Updating a KiiObject.

The specification of a JSON document in a KiiObject is as below.

  • Any numeric value can be set in a JSON document. However, a large value can lead to a reference error in the Kii Cloud SDK and incorrect display of the value in the data browser in the developer portal.

  • The maximum size of a JSON document in a KiiObject is 65534 characters of key-value pairs in the JSON format, including internal fields used by Kii Cloud.

Setting a value of a basic data type

The following sample code shows how to set string, int, long, double, and bool values when a KiiObject is created.

This is based on the sample code in Creating a KiiObject. The code block for setting values is different.

curl -v -X POST \
  -H "Authorization: Bearer {ACCESS_TOKEN}" \
  -H "Content-Type: application/vnd.{APP_ID}.mydata+json" \
  "https://api.kii.com/api/apps/{APP_ID}/users/me/buckets/{BUCKET_ID}/objects" \
  -d '{
        "stringValue": "my value",
        "booleanValue": true,
        "longValue": 1495677489018,
        "doubleValue": 987.654,
        "intValue": 123
      }'

Setting a geolocation

This section explains how to set a geolocation (GeoPint) in a KiiObject.

To set a geolocation, set a GeoPoint object that has the key-value pairs described in the table below as a value for any key. A GeoPoint can be set only at the first level of the JSON hierarchy. It cannot be nested.

Key Value
_type Set a fixed value, "point".
lat Set a latitude. A value that is greater than or equal to -90 and less than 90 is accepted. The boundary value 90 cannot be set.
lon Set a longitude. A value that is greater than or equal to -180 and less than 180 is accepted. The boundary value 180 cannot be set.

The following sample code shows how to create a KiiObject in a bucket in the scope of the currently logged-in user and set two geolocations.

curl -v -X POST \
  -H "Authorization: Bearer {ACCESS_TOKEN}" \
  -H "Content-Type: application/vnd.{APP_ID}.mydata+json" \
  "https://api.kii.com/api/apps/{APP_ID}/users/me/buckets/{BUCKET_ID}/objects" \
  -d '{
        "location1":{
          "_type": "point",
          "lat": 35.658603,
          "lon": 139.745433
        },
        "location2":{
          "_type": "point",
          "lat": 35.658625,
          "lon": 139.745415
        }
      }'

Setting complex data

You can set a JSON object and an array of JSON objects in a JSON document.

See an example below.

curl -v -X POST \
  -H "Authorization: Bearer {ACCESS_TOKEN}" \
  -H "Content-Type: application/vnd.{APP_ID}.mydata+json" \
  "https://api.kii.com/api/apps/{APP_ID}/users/me/buckets/{BUCKET_ID}/objects" \
  -d '{
        "myArray": [
          {
            "Name": "Alice",
            "age": 30
          },
          {
            "Name": "Bob",
            "age": 28
          }
        ],
        "myObject": {
          "score": 987,
          "mode": "easy"
        }
      }'