Group

View as Markdown

Group

A Group holds the fields of one Group field of an entry. Use this class to

  • read the fields of a Group field as Java types
  • reach the assets, nested groups, and referenced entries inside the group

Group has no public constructor and sends no request. You get one from data that a request already returned:

Each Group wraps the JSON object of its field inside the entry JSON. It reads that object only, so a group holds data as soon as you have it.

get

get returns the value of one field of the group, in the type the SDK parsed it into.

NameTypeDescription
key (required)String

Field UID to read.

Default: Not applicable

Returns: Object. The field value.

Validation

  • Throws org.json.JSONException if the group has no field with this key, including a field whose value is JSON null. See the Known issue on the Group class page for what happens inside a fetch callback.
  • A null key returns null.

Behavior

  • Client-side only. get reads the key of the group JSON.
  • The value keeps its parsed type: String, Boolean, Integer, Long, Double, JSONObject, or JSONArray.
  • It does not resolve a dotted path such as dimensions.width. Read the nested group with the getGroup method, then read its field.
  • Check group.toJSON().has(key) before you read a field that can be absent.

Example

Basic usage: read a field and check its type

import com.contentstack.sdk.Contentstack;
import com.contentstack.sdk.Entry;
import com.contentstack.sdk.EntryResultCallBack;
import com.contentstack.sdk.Error;
import com.contentstack.sdk.Group;
import com.contentstack.sdk.ResponseType;
import com.contentstack.sdk.Stack;

public class ReadGroupField {
    public static void main(String[] args) throws IllegalAccessException {
        Stack stack = Contentstack.stack("<API_KEY>", "<DELIVERY_TOKEN>", "<ENVIRONMENT>");
        Entry entry = stack.contentType("<CONTENT_TYPE_UID>").entry("<ENTRY_UID>");

        entry.fetch(new EntryResultCallBack() {
            @Override
            public void onCompletion(ResponseType responseType, Error error) {
                if (error != null) {
                    // The API rejected the request, or it did not complete
                    System.out.println("Request failed: " + error.getErrorCode() + " " + error.getErrorMessage());
                    return;
                }
                Group details = entry.getGroup("details");
                if (details == null) {
                    System.out.println("No details group");
                    return;
                }
                if (!details.toJSON().has("weight")) {
                    System.out.println("No weight");
                    return;
                }
                Object weight = details.get("weight");
                System.out.println(weight instanceof Number ? "Weight: " + weight : "Weight is not a number");
            }
        });
    }
}

getAllEntries

getAllEntries returns the entries in a reference field of the group as a list of Entry objects.

NameTypeDescription
refKey (required)String

Reference field UID to read.

Default: Not applicable
refContentType (required)String

Content type UID to give each returned entry.

Default: Not applicable

Returns: List<Entry>. The entries in the reference field.

Validation

  • getAllEntries does not throw. It returns an empty list when refKey is missing or null, or when its value is not an array.
  • When an array item is not an object, it stops there and returns the entries that it built before that item.
  • The SDK does not check refContentType. It uses the value only as the content type UID of each returned entry.

Behavior

  • Known issue: Entries in the list return null from getTitle, getURL, and getLocale. Read those values with getString("title"), getString("url"), and getString("locale").
  • Client-side only. getAllEntries builds one Entry for each object in the reference array, and makes no request. Each entry holds the object as its JSON, and its UID.
  • Without the includeReference method on the entry request, the reference array holds only the UIDs of the referenced entries. Pass the dotted path, such as details.accessories, to includeReference to include them in full.
  • It behaves the same way as the getAllEntries method of Entry, one level down.

Example

Basic usage: read the referenced entries of a group

import com.contentstack.sdk.Contentstack;
import com.contentstack.sdk.Entry;
import com.contentstack.sdk.EntryResultCallBack;
import com.contentstack.sdk.Error;
import com.contentstack.sdk.Group;
import com.contentstack.sdk.ResponseType;
import com.contentstack.sdk.Stack;

import java.util.List;

public class ReadGroupReferences {
    public static void main(String[] args) throws IllegalAccessException {
        Stack stack = Contentstack.stack("<API_KEY>", "<DELIVERY_TOKEN>", "<ENVIRONMENT>");
        Entry entry = stack.contentType("<CONTENT_TYPE_UID>").entry("<ENTRY_UID>").includeReference("details.accessories");

        entry.fetch(new EntryResultCallBack() {
            @Override
            public void onCompletion(ResponseType responseType, Error error) {
                if (error != null) {
                    // The API rejected the request, or it did not complete
                    System.out.println("Request failed: " + error.getErrorCode() + " " + error.getErrorMessage());
                    return;
                }
                Group details = entry.getGroup("details");
                if (details == null) {
                    System.out.println("No details group");
                    return;
                }

                // accessories refers to entries of the same content type
                List<Entry> accessories = details.getAllEntries("accessories", "<CONTENT_TYPE_UID>");
                for (Entry accessory : accessories) {
                    // getTitle returns null on these entries, so read the field
                    System.out.println(accessory.getUid() + " " + accessory.getString("title"));
                }
            }
        });
    }
}

getAsset

getAsset returns the asset in a single file field of the group as an Asset.

NameTypeDescription
key (required)String

Field UID to read.

Default: Not applicable

Returns: Asset. The asset in the file field.

Validation

  • Throws org.json.JSONException if the group has no field with this key, including a field whose value is JSON null. See the Known issue on the Group class page for what happens inside a fetch callback.
  • Throws ClassCastException if the value is not an object.
  • A null key returns an Asset whose getters all return null.

Behavior

  • Client-side only. getAsset builds an Asset from the JSON object under the key, and makes no request.
  • Read the asset with the Asset getters, such as getUrl, getFileName, and getAssetUid.
  • For a file field marked multiple, use the getAssets method.
  • Check group.toJSON().has(key) before you read a field that can be absent.

Example

Basic usage: read a file field of the group

import com.contentstack.sdk.Asset;
import com.contentstack.sdk.Contentstack;
import com.contentstack.sdk.Entry;
import com.contentstack.sdk.EntryResultCallBack;
import com.contentstack.sdk.Error;
import com.contentstack.sdk.Group;
import com.contentstack.sdk.ResponseType;
import com.contentstack.sdk.Stack;

public class ReadGroupAsset {
    public static void main(String[] args) throws IllegalAccessException {
        Stack stack = Contentstack.stack("<API_KEY>", "<DELIVERY_TOKEN>", "<ENVIRONMENT>");
        Entry entry = stack.contentType("<CONTENT_TYPE_UID>").entry("<ENTRY_UID>");

        entry.fetch(new EntryResultCallBack() {
            @Override
            public void onCompletion(ResponseType responseType, Error error) {
                if (error != null) {
                    // The API rejected the request, or it did not complete
                    System.out.println("Request failed: " + error.getErrorCode() + " " + error.getErrorMessage());
                    return;
                }
                Group details = entry.getGroup("details");
                if (details == null) {
                    System.out.println("No details group");
                    return;
                }
                if (details.toJSON().has("swatch")) {
                    Asset swatch = details.getAsset("swatch");
                    System.out.println(swatch.getFileName() + " " + swatch.getUrl());
                }
            }
        });
    }
}

getAssets

getAssets returns the assets in a file field of the group marked multiple as a list of Asset objects.

NameTypeDescription
key (required)String

Field UID to read.

Default: Not applicable

Returns: List<Asset>. The assets in the file field.

Validation

  • Throws org.json.JSONException if the group has no field with this key, including a field whose value is JSON null. See the Known issue on the Group class page for what happens inside a fetch callback.
  • Throws ClassCastException if the value is not an array.
  • Throws NullPointerException if key is null.

Behavior

  • Client-side only. getAssets builds one Asset for each JSON object in the array, and makes no request.
  • It skips array items that are not objects. An empty array returns an empty list.
  • For a single file field, use the getAsset method.
  • Check group.toJSON().has(key) before you read a field that can be absent.

Example

Basic usage: read a multiple file field of the group

import com.contentstack.sdk.Asset;
import com.contentstack.sdk.Contentstack;
import com.contentstack.sdk.Entry;
import com.contentstack.sdk.EntryResultCallBack;
import com.contentstack.sdk.Error;
import com.contentstack.sdk.Group;
import com.contentstack.sdk.ResponseType;
import com.contentstack.sdk.Stack;

import java.util.List;

public class ReadGroupAssets {
    public static void main(String[] args) throws IllegalAccessException {
        Stack stack = Contentstack.stack("<API_KEY>", "<DELIVERY_TOKEN>", "<ENVIRONMENT>");
        Entry entry = stack.contentType("<CONTENT_TYPE_UID>").entry("<ENTRY_UID>");

        entry.fetch(new EntryResultCallBack() {
            @Override
            public void onCompletion(ResponseType responseType, Error error) {
                if (error != null) {
                    // The API rejected the request, or it did not complete
                    System.out.println("Request failed: " + error.getErrorCode() + " " + error.getErrorMessage());
                    return;
                }
                Group details = entry.getGroup("details");
                if (details == null) {
                    System.out.println("No details group");
                    return;
                }
                if (!details.toJSON().has("swatches")) {
                    System.out.println("No swatches");
                    return;
                }
                List<Asset> swatches = details.getAssets("swatches");
                for (Asset swatch : swatches) {
                    System.out.println(swatch.getUrl());
                }
            }
        });
    }
}

getBoolean

getBoolean returns the value of a boolean field of the group.

NameTypeDescription
key (required)String

Field UID to read.

Default: Not applicable

Returns: Boolean. The field value as a Boolean.

Validation

  • Throws org.json.JSONException if the group has no field with this key, including a field whose value is JSON null. See the Known issue on the Group class page for what happens inside a fetch callback.
  • Throws ClassCastException if the value is not a Boolean.
  • A null key returns false.

Behavior

  • Client-side only. getBoolean reads the key of the group JSON and casts the value to Boolean.
  • Unlike the getBoolean method of Entry, it does not return false for a missing field or a value of another type.
  • Check group.toJSON().has(key) before you read a field that can be absent.

Example

Basic usage: read a boolean field of the group

import com.contentstack.sdk.Contentstack;
import com.contentstack.sdk.Entry;
import com.contentstack.sdk.EntryResultCallBack;
import com.contentstack.sdk.Error;
import com.contentstack.sdk.Group;
import com.contentstack.sdk.ResponseType;
import com.contentstack.sdk.Stack;

public class ReadGroupBoolean {
    public static void main(String[] args) throws IllegalAccessException {
        Stack stack = Contentstack.stack("<API_KEY>", "<DELIVERY_TOKEN>", "<ENVIRONMENT>");
        Entry entry = stack.contentType("<CONTENT_TYPE_UID>").entry("<ENTRY_UID>");

        entry.fetch(new EntryResultCallBack() {
            @Override
            public void onCompletion(ResponseType responseType, Error error) {
                if (error != null) {
                    // The API rejected the request, or it did not complete
                    System.out.println("Request failed: " + error.getErrorCode() + " " + error.getErrorMessage());
                    return;
                }
                Group details = entry.getGroup("details");
                if (details == null) {
                    System.out.println("No details group");
                    return;
                }
                if (details.toJSON().has("is_handmade") && details.getBoolean("is_handmade")) {
                    System.out.println("Handmade");
                }
            }
        });
    }
}

getDate

getDate returns the value of a date field of the group as a Calendar.

NameTypeDescription
key (required)String

Field UID to read.

Default: Not applicable

Returns: Calendar. The field value as a Calendar.

Validation

  • getDate does not throw. It returns null for a missing key, a value that is not a string, and a string that is not an ISO 8601 date-time.
  • Each failed parse writes a SEVERE record to the java.util.logging logger of the SDK.

Behavior

  • Known issue: The returned Calendar is one month ahead, and it holds the UTC clock time as local time in the default time zone of the JVM. For example, 2024-12-15T10:30:00.000Z becomes January 15, 2025, 10:30 local time. Parse the string from getString with java.time.OffsetDateTime.parse instead.
  • Client-side only. getDate reads the string value of the key and parses it the same way as the getDate method of Entry.
  • A date with no time part, such as 2024-05-01, returns null.
  • The Calendar uses the default time zone of the JVM, and its milliseconds come from the current time, not from the string.

Example

Basic usage: read a date field of the group

import com.contentstack.sdk.Contentstack;
import com.contentstack.sdk.Entry;
import com.contentstack.sdk.EntryResultCallBack;
import com.contentstack.sdk.Error;
import com.contentstack.sdk.Group;
import com.contentstack.sdk.ResponseType;
import com.contentstack.sdk.Stack;

import java.util.Calendar;

public class ReadGroupDate {
    public static void main(String[] args) throws IllegalAccessException {
        Stack stack = Contentstack.stack("<API_KEY>", "<DELIVERY_TOKEN>", "<ENVIRONMENT>");
        Entry entry = stack.contentType("<CONTENT_TYPE_UID>").entry("<ENTRY_UID>");

        entry.fetch(new EntryResultCallBack() {
            @Override
            public void onCompletion(ResponseType responseType, Error error) {
                if (error != null) {
                    // The API rejected the request, or it did not complete
                    System.out.println("Request failed: " + error.getErrorCode() + " " + error.getErrorMessage());
                    return;
                }
                Group details = entry.getGroup("details");
                if (details == null) {
                    System.out.println("No details group");
                    return;
                }
                Calendar availableFrom = details.getDate("available_from");
                if (availableFrom != null) {
                    System.out.println("Available from: " + availableFrom.getTime());
                }
            }
        });
    }
}

Workaround: parse the date with java.time

import com.contentstack.sdk.Contentstack;
import com.contentstack.sdk.Entry;
import com.contentstack.sdk.EntryResultCallBack;
import com.contentstack.sdk.Error;
import com.contentstack.sdk.Group;
import com.contentstack.sdk.ResponseType;
import com.contentstack.sdk.Stack;

import java.time.OffsetDateTime;

public class ParseGroupDate {
    public static void main(String[] args) throws IllegalAccessException {
        Stack stack = Contentstack.stack("<API_KEY>", "<DELIVERY_TOKEN>", "<ENVIRONMENT>");
        Entry entry = stack.contentType("<CONTENT_TYPE_UID>").entry("<ENTRY_UID>");

        entry.fetch(new EntryResultCallBack() {
            @Override
            public void onCompletion(ResponseType responseType, Error error) {
                if (error != null) { System.out.println(error.getErrorMessage()); return; }
                Group details = entry.getGroup("details");
                if (details == null) {
                    System.out.println("No details group");
                    return;
                }
                if (!details.toJSON().has("available_from")) {
                    System.out.println("No date");
                    return;
                }
                OffsetDateTime availableFrom = OffsetDateTime.parse(details.getString("available_from"));
                System.out.println(availableFrom.toLocalDate());
            }
        });
    }
}

getDouble

getDouble returns the value of a number field of the group as a double.

NameTypeDescription
key (required)String

Field UID to read.

Default: Not applicable

Returns: double. The field value as a double.

Validation

  • Throws org.json.JSONException if the group has no field with this key, including a field whose value is JSON null. See the Known issue on the Group class page for what happens inside a fetch callback.
  • Throws ClassCastException if the value is not a Number.
  • A null key returns 0.

Behavior

  • Client-side only. getDouble reads the key of the group JSON and converts the Number with doubleValue().
  • It converts any number, whole or decimal.
  • Check group.toJSON().has(key) before you read a field that can be absent.

Example

Basic usage: read a decimal field of the group

import com.contentstack.sdk.Contentstack;
import com.contentstack.sdk.Entry;
import com.contentstack.sdk.EntryResultCallBack;
import com.contentstack.sdk.Error;
import com.contentstack.sdk.Group;
import com.contentstack.sdk.ResponseType;
import com.contentstack.sdk.Stack;

public class ReadGroupDouble {
    public static void main(String[] args) throws IllegalAccessException {
        Stack stack = Contentstack.stack("<API_KEY>", "<DELIVERY_TOKEN>", "<ENVIRONMENT>");
        Entry entry = stack.contentType("<CONTENT_TYPE_UID>").entry("<ENTRY_UID>");

        entry.fetch(new EntryResultCallBack() {
            @Override
            public void onCompletion(ResponseType responseType, Error error) {
                if (error != null) {
                    // The API rejected the request, or it did not complete
                    System.out.println("Request failed: " + error.getErrorCode() + " " + error.getErrorMessage());
                    return;
                }
                Group details = entry.getGroup("details");
                if (details == null) {
                    System.out.println("No details group");
                    return;
                }
                if (details.toJSON().has("weight")) {
                    double value = details.getDouble("weight");
                    System.out.println("weight: " + value);
                }
            }
        });
    }
}

getFloat

getFloat returns the value of a number field of the group as a float.

NameTypeDescription
key (required)String

Field UID to read.

Default: Not applicable

Returns: float. The field value as a float.

Validation

  • Throws org.json.JSONException if the group has no field with this key, including a field whose value is JSON null. See the Known issue on the Group class page for what happens inside a fetch callback.
  • Throws ClassCastException if the value is not a Number.
  • A null key returns 0.

Behavior

  • Client-side only. getFloat reads the key of the group JSON and converts the Number with floatValue().
  • It converts any number, whole or decimal. A double value can lose precision. Unlike the getFloat method of Entry, it returns the decimal values of fetched data.
  • Check group.toJSON().has(key) before you read a field that can be absent.

Example

Basic usage: read a decimal field of the group

import com.contentstack.sdk.Contentstack;
import com.contentstack.sdk.Entry;
import com.contentstack.sdk.EntryResultCallBack;
import com.contentstack.sdk.Error;
import com.contentstack.sdk.Group;
import com.contentstack.sdk.ResponseType;
import com.contentstack.sdk.Stack;

public class ReadGroupFloat {
    public static void main(String[] args) throws IllegalAccessException {
        Stack stack = Contentstack.stack("<API_KEY>", "<DELIVERY_TOKEN>", "<ENVIRONMENT>");
        Entry entry = stack.contentType("<CONTENT_TYPE_UID>").entry("<ENTRY_UID>");

        entry.fetch(new EntryResultCallBack() {
            @Override
            public void onCompletion(ResponseType responseType, Error error) {
                if (error != null) {
                    // The API rejected the request, or it did not complete
                    System.out.println("Request failed: " + error.getErrorCode() + " " + error.getErrorMessage());
                    return;
                }
                Group details = entry.getGroup("details");
                if (details == null) {
                    System.out.println("No details group");
                    return;
                }
                if (details.toJSON().has("weight")) {
                    float value = details.getFloat("weight");
                    System.out.println("weight: " + value);
                }
            }
        });
    }
}

getGroup

getGroup returns a Group field nested inside the group as a Group.

NameTypeDescription
key (required)String

Field UID to read.

Default: Not applicable

Returns: Group. The nested group in the field.

Validation

  • getGroup returns null when the key is missing or empty, or when its value is not an object.
  • Throws NullPointerException if key is null.

Behavior

  • Client-side only. getGroup wraps the JSON object under the key in a new Group, and makes no request.
  • It behaves the same way as the getGroup method of Entry, one level down.
  • For a nested Group field marked multiple, use the getGroups method.

Example

Basic usage: read a field of a nested group

import com.contentstack.sdk.Contentstack;
import com.contentstack.sdk.Entry;
import com.contentstack.sdk.EntryResultCallBack;
import com.contentstack.sdk.Error;
import com.contentstack.sdk.Group;
import com.contentstack.sdk.ResponseType;
import com.contentstack.sdk.Stack;

public class ReadNestedGroup {
    public static void main(String[] args) throws IllegalAccessException {
        Stack stack = Contentstack.stack("<API_KEY>", "<DELIVERY_TOKEN>", "<ENVIRONMENT>");
        Entry entry = stack.contentType("<CONTENT_TYPE_UID>").entry("<ENTRY_UID>");

        entry.fetch(new EntryResultCallBack() {
            @Override
            public void onCompletion(ResponseType responseType, Error error) {
                if (error != null) {
                    // The API rejected the request, or it did not complete
                    System.out.println("Request failed: " + error.getErrorCode() + " " + error.getErrorMessage());
                    return;
                }
                Group details = entry.getGroup("details");
                if (details == null) {
                    System.out.println("No details group");
                    return;
                }
                Group dimensions = details.getGroup("dimensions");
                if (dimensions != null && dimensions.toJSON().has("width")) {
                    System.out.println("Width: " + dimensions.getDouble("width"));
                }
            }
        });
    }
}

getGroups

getGroups returns a nested Group field marked multiple as a list of Group objects.

NameTypeDescription
key (required)String

Field UID to read.

Default: Not applicable

Returns: List<Group>. The nested groups in the field.

Validation

  • getGroups returns an empty list when the key is missing or empty, or when its value is not an array.
  • Throws NullPointerException if key is null.
  • Throws ClassCastException if an array item is not an object, as in a text field marked multiple.

Behavior

  • Client-side only. getGroups wraps each JSON object of the array in a new Group, and makes no request.
  • The list keeps the order of the array.
  • For a single nested Group field, use the getGroup method.

Example

Basic usage: read each item of a nested multiple Group field

import com.contentstack.sdk.Contentstack;
import com.contentstack.sdk.Entry;
import com.contentstack.sdk.EntryResultCallBack;
import com.contentstack.sdk.Error;
import com.contentstack.sdk.Group;
import com.contentstack.sdk.ResponseType;
import com.contentstack.sdk.Stack;

import java.util.List;

public class ReadNestedGroups {
    public static void main(String[] args) throws IllegalAccessException {
        Stack stack = Contentstack.stack("<API_KEY>", "<DELIVERY_TOKEN>", "<ENVIRONMENT>");
        Entry entry = stack.contentType("<CONTENT_TYPE_UID>").entry("<ENTRY_UID>");

        entry.fetch(new EntryResultCallBack() {
            @Override
            public void onCompletion(ResponseType responseType, Error error) {
                if (error != null) {
                    // The API rejected the request, or it did not complete
                    System.out.println("Request failed: " + error.getErrorCode() + " " + error.getErrorMessage());
                    return;
                }
                Group details = entry.getGroup("details");
                if (details == null) {
                    System.out.println("No details group");
                    return;
                }
                List<Group> careSteps = details.getGroups("care_steps");
                for (Group step : careSteps) {
                    if (step.toJSON().has("instruction")) {
                        System.out.println(step.getString("instruction"));
                    }
                }
            }
        });
    }
}

getInt

getInt returns the value of a number field of the group as an int.

NameTypeDescription
key (required)String

Field UID to read.

Default: Not applicable

Returns: int. The field value as an int.

Validation

  • Throws org.json.JSONException if the group has no field with this key, including a field whose value is JSON null. See the Known issue on the Group class page for what happens inside a fetch callback.
  • Throws ClassCastException if the value is not a Number.
  • A null key returns 0.

Behavior

  • Client-side only. getInt reads the key of the group JSON and converts the Number with intValue().
  • A decimal loses its fraction, so 4.9 returns 4. A whole number outside the int range wraps around, as Java narrowing conversion does. The getInt method of Entry returns 0 for both instead.
  • Check group.toJSON().has(key) before you read a field that can be absent.

Example

Basic usage: read a whole-number field of the group

import com.contentstack.sdk.Contentstack;
import com.contentstack.sdk.Entry;
import com.contentstack.sdk.EntryResultCallBack;
import com.contentstack.sdk.Error;
import com.contentstack.sdk.Group;
import com.contentstack.sdk.ResponseType;
import com.contentstack.sdk.Stack;

public class ReadGroupInt {
    public static void main(String[] args) throws IllegalAccessException {
        Stack stack = Contentstack.stack("<API_KEY>", "<DELIVERY_TOKEN>", "<ENVIRONMENT>");
        Entry entry = stack.contentType("<CONTENT_TYPE_UID>").entry("<ENTRY_UID>");

        entry.fetch(new EntryResultCallBack() {
            @Override
            public void onCompletion(ResponseType responseType, Error error) {
                if (error != null) {
                    // The API rejected the request, or it did not complete
                    System.out.println("Request failed: " + error.getErrorCode() + " " + error.getErrorMessage());
                    return;
                }
                Group details = entry.getGroup("details");
                if (details == null) {
                    System.out.println("No details group");
                    return;
                }
                if (details.toJSON().has("warranty_years")) {
                    int value = details.getInt("warranty_years");
                    System.out.println("warranty_years: " + value);
                }
            }
        });
    }
}

getJSONArray

getJSONArray returns the value of a multiple field or a reference field of the group as an array.

NameTypeDescription
key (required)String

Field UID to read.

Default: Not applicable

Returns: JSONArray. The field value as a JSONArray.

Validation

  • Throws org.json.JSONException if the group has no field with this key, including a field whose value is JSON null. See the Known issue on the Group class page for what happens inside a fetch callback.
  • Throws ClassCastException if the value is not a JSONArray.
  • A null key returns null.

Behavior

  • Client-side only. getJSONArray reads the key of the group JSON and casts the value to JSONArray.
  • Use getAssets, getGroups, or getAllEntries to read the array as SDK objects.
  • Check group.toJSON().has(key) before you read a field that can be absent.

Example

Basic usage: read a multiple text field of the group

import com.contentstack.sdk.Contentstack;
import com.contentstack.sdk.Entry;
import com.contentstack.sdk.EntryResultCallBack;
import com.contentstack.sdk.Error;
import com.contentstack.sdk.Group;
import com.contentstack.sdk.ResponseType;
import com.contentstack.sdk.Stack;

import org.json.JSONArray;

public class ReadGroupArray {
    public static void main(String[] args) throws IllegalAccessException {
        Stack stack = Contentstack.stack("<API_KEY>", "<DELIVERY_TOKEN>", "<ENVIRONMENT>");
        Entry entry = stack.contentType("<CONTENT_TYPE_UID>").entry("<ENTRY_UID>");

        entry.fetch(new EntryResultCallBack() {
            @Override
            public void onCompletion(ResponseType responseType, Error error) {
                if (error != null) {
                    // The API rejected the request, or it did not complete
                    System.out.println("Request failed: " + error.getErrorCode() + " " + error.getErrorMessage());
                    return;
                }
                Group details = entry.getGroup("details");
                if (details == null) {
                    System.out.println("No details group");
                    return;
                }
                if (!details.toJSON().has("colors")) {
                    System.out.println("No colors");
                    return;
                }
                JSONArray colors = details.getJSONArray("colors");
                for (int i = 0; i < colors.length(); i++) {
                    System.out.println(colors.optString(i));
                }
            }
        });
    }
}

getJSONObject

getJSONObject returns the value of a nested Group, file, or JSON Rich Text Editor field of the group as an object.

NameTypeDescription
key (required)String

Field UID to read.

Default: Not applicable

Returns: JSONObject. The field value as a JSONObject.

Validation

  • Throws org.json.JSONException if the group has no field with this key, including a field whose value is JSON null. See the Known issue on the Group class page for what happens inside a fetch callback.
  • Throws ClassCastException if the value is not a JSONObject.
  • A null key returns null.

Behavior

  • Client-side only. getJSONObject reads the key of the group JSON and casts the value to JSONObject.
  • Use getGroup or getAsset to read the object as an SDK object with typed getters.
  • Check group.toJSON().has(key) before you read a field that can be absent.

Example

Basic usage: read a nested Group field as JSON

import com.contentstack.sdk.Contentstack;
import com.contentstack.sdk.Entry;
import com.contentstack.sdk.EntryResultCallBack;
import com.contentstack.sdk.Error;
import com.contentstack.sdk.Group;
import com.contentstack.sdk.ResponseType;
import com.contentstack.sdk.Stack;

import org.json.JSONObject;

public class ReadGroupObject {
    public static void main(String[] args) throws IllegalAccessException {
        Stack stack = Contentstack.stack("<API_KEY>", "<DELIVERY_TOKEN>", "<ENVIRONMENT>");
        Entry entry = stack.contentType("<CONTENT_TYPE_UID>").entry("<ENTRY_UID>");

        entry.fetch(new EntryResultCallBack() {
            @Override
            public void onCompletion(ResponseType responseType, Error error) {
                if (error != null) {
                    // The API rejected the request, or it did not complete
                    System.out.println("Request failed: " + error.getErrorCode() + " " + error.getErrorMessage());
                    return;
                }
                Group details = entry.getGroup("details");
                if (details == null) {
                    System.out.println("No details group");
                    return;
                }
                if (details.toJSON().has("dimensions")) {
                    JSONObject dimensions = details.getJSONObject("dimensions");
                    System.out.println(dimensions.optDouble("width") + " x " + dimensions.optDouble("height"));
                }
            }
        });
    }
}

getLong

getLong returns the value of a number field of the group as a long.

NameTypeDescription
key (required)String

Field UID to read.

Default: Not applicable

Returns: long. The field value as a long.

Validation

  • Throws org.json.JSONException if the group has no field with this key, including a field whose value is JSON null. See the Known issue on the Group class page for what happens inside a fetch callback.
  • Throws ClassCastException if the value is not a Number.
  • A null key returns 0.

Behavior

  • Client-side only. getLong reads the key of the group JSON and converts the Number with longValue().
  • A decimal loses its fraction, so 4.9 returns 4.
  • Check group.toJSON().has(key) before you read a field that can be absent.

Example

Basic usage: read a whole-number field of the group

import com.contentstack.sdk.Contentstack;
import com.contentstack.sdk.Entry;
import com.contentstack.sdk.EntryResultCallBack;
import com.contentstack.sdk.Error;
import com.contentstack.sdk.Group;
import com.contentstack.sdk.ResponseType;
import com.contentstack.sdk.Stack;

public class ReadGroupLong {
    public static void main(String[] args) throws IllegalAccessException {
        Stack stack = Contentstack.stack("<API_KEY>", "<DELIVERY_TOKEN>", "<ENVIRONMENT>");
        Entry entry = stack.contentType("<CONTENT_TYPE_UID>").entry("<ENTRY_UID>");

        entry.fetch(new EntryResultCallBack() {
            @Override
            public void onCompletion(ResponseType responseType, Error error) {
                if (error != null) {
                    // The API rejected the request, or it did not complete
                    System.out.println("Request failed: " + error.getErrorCode() + " " + error.getErrorMessage());
                    return;
                }
                Group details = entry.getGroup("details");
                if (details == null) {
                    System.out.println("No details group");
                    return;
                }
                if (details.toJSON().has("warranty_years")) {
                    long value = details.getLong("warranty_years");
                    System.out.println("warranty_years: " + value);
                }
            }
        });
    }
}

getNumber

getNumber returns the value of a number field of the group.

NameTypeDescription
key (required)String

Field UID to read.

Default: Not applicable

Returns: Number. The field value as a Number.

Validation

  • Throws org.json.JSONException if the group has no field with this key, including a field whose value is JSON null. See the Known issue on the Group class page for what happens inside a fetch callback.
  • Throws ClassCastException if the value is not a Number, such as a number stored as text.
  • A null key returns null.

Behavior

  • Client-side only. getNumber reads the key of the group JSON and casts the value to Number.
  • A whole number arrives as an Integer, or as a Long when it exceeds the int range. A decimal arrives as a Double.
  • Check group.toJSON().has(key) before you read a field that can be absent.

Example

Basic usage: read a number field of the group

import com.contentstack.sdk.Contentstack;
import com.contentstack.sdk.Entry;
import com.contentstack.sdk.EntryResultCallBack;
import com.contentstack.sdk.Error;
import com.contentstack.sdk.Group;
import com.contentstack.sdk.ResponseType;
import com.contentstack.sdk.Stack;

public class ReadGroupNumber {
    public static void main(String[] args) throws IllegalAccessException {
        Stack stack = Contentstack.stack("<API_KEY>", "<DELIVERY_TOKEN>", "<ENVIRONMENT>");
        Entry entry = stack.contentType("<CONTENT_TYPE_UID>").entry("<ENTRY_UID>");

        entry.fetch(new EntryResultCallBack() {
            @Override
            public void onCompletion(ResponseType responseType, Error error) {
                if (error != null) {
                    // The API rejected the request, or it did not complete
                    System.out.println("Request failed: " + error.getErrorCode() + " " + error.getErrorMessage());
                    return;
                }
                Group details = entry.getGroup("details");
                if (details == null) {
                    System.out.println("No details group");
                    return;
                }
                if (details.toJSON().has("weight")) {
                    Number weight = details.getNumber("weight");
                    System.out.println("Weight: " + weight.doubleValue());
                }
            }
        });
    }
}

getShort

getShort returns the value of a number field of the group as a short.

NameTypeDescription
key (required)String

Field UID to read.

Default: Not applicable

Returns: short. The field value as a short.

Validation

  • Throws org.json.JSONException if the group has no field with this key, including a field whose value is JSON null. See the Known issue on the Group class page for what happens inside a fetch callback.
  • Throws ClassCastException if the value is not a Number.
  • A null key returns 0.

Behavior

  • Client-side only. getShort reads the key of the group JSON and converts the Number with shortValue().
  • A decimal loses its fraction, and a value outside the short range of -32,768 to 32,767 wraps around, as Java narrowing conversion does.
  • Check group.toJSON().has(key) before you read a field that can be absent.

Example

Basic usage: read a small whole number of the group

import com.contentstack.sdk.Contentstack;
import com.contentstack.sdk.Entry;
import com.contentstack.sdk.EntryResultCallBack;
import com.contentstack.sdk.Error;
import com.contentstack.sdk.Group;
import com.contentstack.sdk.ResponseType;
import com.contentstack.sdk.Stack;

public class ReadGroupShort {
    public static void main(String[] args) throws IllegalAccessException {
        Stack stack = Contentstack.stack("<API_KEY>", "<DELIVERY_TOKEN>", "<ENVIRONMENT>");
        Entry entry = stack.contentType("<CONTENT_TYPE_UID>").entry("<ENTRY_UID>");

        entry.fetch(new EntryResultCallBack() {
            @Override
            public void onCompletion(ResponseType responseType, Error error) {
                if (error != null) {
                    // The API rejected the request, or it did not complete
                    System.out.println("Request failed: " + error.getErrorCode() + " " + error.getErrorMessage());
                    return;
                }
                Group details = entry.getGroup("details");
                if (details == null) {
                    System.out.println("No details group");
                    return;
                }
                if (details.toJSON().has("warranty_years")) {
                    short value = details.getShort("warranty_years");
                    System.out.println("warranty_years: " + value);
                }
            }
        });
    }
}

getString

getString returns the value of a text field of the group.

NameTypeDescription
key (required)String

Field UID to read.

Default: Not applicable

Returns: String. The field value as a String.

Validation

  • Throws org.json.JSONException if the group has no field with this key, including a field whose value is JSON null. See the Known issue on the Group class page for what happens inside a fetch callback.
  • Throws ClassCastException if the value is not a String, such as a number or a boolean.
  • A null key returns null.

Behavior

  • Client-side only. getString reads the key of the group JSON and casts the value to String.
  • Unlike the getString method of Entry, it does not return null for a missing field or a value of another type.
  • Check group.toJSON().has(key) before you read a field that can be absent.

Example

Basic usage: read a text field of the group

import com.contentstack.sdk.Contentstack;
import com.contentstack.sdk.Entry;
import com.contentstack.sdk.EntryResultCallBack;
import com.contentstack.sdk.Error;
import com.contentstack.sdk.Group;
import com.contentstack.sdk.ResponseType;
import com.contentstack.sdk.Stack;

public class ReadGroupString {
    public static void main(String[] args) throws IllegalAccessException {
        Stack stack = Contentstack.stack("<API_KEY>", "<DELIVERY_TOKEN>", "<ENVIRONMENT>");
        Entry entry = stack.contentType("<CONTENT_TYPE_UID>").entry("<ENTRY_UID>");

        entry.fetch(new EntryResultCallBack() {
            @Override
            public void onCompletion(ResponseType responseType, Error error) {
                if (error != null) {
                    // The API rejected the request, or it did not complete
                    System.out.println("Request failed: " + error.getErrorCode() + " " + error.getErrorMessage());
                    return;
                }
                Group details = entry.getGroup("details");
                if (details == null) {
                    System.out.println("No details group");
                    return;
                }
                if (details.toJSON().has("material")) {
                    System.out.println("Material: " + details.getString("material"));
                }
            }
        });
    }
}

Edge case: read a field that can hold another type

import com.contentstack.sdk.Contentstack;
import com.contentstack.sdk.Entry;
import com.contentstack.sdk.EntryResultCallBack;
import com.contentstack.sdk.Error;
import com.contentstack.sdk.Group;
import com.contentstack.sdk.ResponseType;
import com.contentstack.sdk.Stack;

public class ReadGroupStringSafely {
    public static void main(String[] args) throws IllegalAccessException {
        Stack stack = Contentstack.stack("<API_KEY>", "<DELIVERY_TOKEN>", "<ENVIRONMENT>");
        Entry entry = stack.contentType("<CONTENT_TYPE_UID>").entry("<ENTRY_UID>");

        entry.fetch(new EntryResultCallBack() {
            @Override
            public void onCompletion(ResponseType responseType, Error error) {
                if (error != null) { System.out.println(error.getErrorMessage()); return; }
                Group details = entry.getGroup("details");
                if (details == null) {
                    System.out.println("No details group");
                    return;
                }
                // getString throws ClassCastException for a number, so check the type first
                Object value = details.toJSON().opt("material");
                if (value instanceof String) {
                    System.out.println(details.getString("material"));
                }
            }
        });
    }
}

toJSON

toJSON returns the JSON of the group that the getters read.

Returns: JSONObject. The group's fields as JSON.

Validation

  • There is no client-side validation. toJSON cannot fail.

Behavior

  • Client-side only. toJSON returns the JSON object that the group wraps.
  • It returns that object, not a copy. The object is also part of the entry JSON. A change you make to it shows in the group getters and in the toJSON method of the entry.
  • Call toJSON().has(key) to check for a field before you read it with a getter that throws on a missing key.

Example

Basic usage: print the group JSON

import com.contentstack.sdk.Contentstack;
import com.contentstack.sdk.Entry;
import com.contentstack.sdk.EntryResultCallBack;
import com.contentstack.sdk.Error;
import com.contentstack.sdk.Group;
import com.contentstack.sdk.ResponseType;
import com.contentstack.sdk.Stack;

public class PrintGroupJson {
    public static void main(String[] args) throws IllegalAccessException {
        Stack stack = Contentstack.stack("<API_KEY>", "<DELIVERY_TOKEN>", "<ENVIRONMENT>");
        Entry entry = stack.contentType("<CONTENT_TYPE_UID>").entry("<ENTRY_UID>");

        entry.fetch(new EntryResultCallBack() {
            @Override
            public void onCompletion(ResponseType responseType, Error error) {
                if (error != null) {
                    // The API rejected the request, or it did not complete
                    System.out.println("Request failed: " + error.getErrorCode() + " " + error.getErrorMessage());
                    return;
                }
                Group details = entry.getGroup("details");
                if (details == null) {
                    System.out.println("No details group");
                    return;
                }
                System.out.println(details.toJSON().toString(2));
            }
        });
    }
}