---
title: "Group"
description: "Group"
url: "https://www.contentstack.com/docs/developers/sdks/content-delivery-sdk/java/reference/group"
product: "Contentstack"
doc_type: "guide"
audience:
  - developers
  - admins
version: "current"
last_updated: "2026-11-13"
---

# Group

## 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:

*   [getGroup](/docs/developers/sdks/content-delivery-sdk/java/reference/entry#entry-getgroup) and [getGroups](/docs/developers/sdks/content-delivery-sdk/java/reference/entry#entry-getgroups) on an [Entry](/docs/developers/sdks/content-delivery-sdk/java/reference/entry) return the Group fields of the entry.
*   [getGroup](/docs/developers/sdks/content-delivery-sdk/java/reference/group#group-getgroup) and [getGroups](/docs/developers/sdks/content-delivery-sdk/java/reference/group#group-getgroups) on a Group return the Group fields nested inside it.

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.

```
Returns: Object. The field value.
ValidationThrows 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.BehaviorClient-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.ExampleBasic usage: read a field and check its typeimport 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");
            }
        });
    }
}
```

Field UID to read.

## getAllEntries

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

```
Returns: List<Entry>. The entries in the reference field.
ValidationgetAllEntries 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.BehaviorKnown 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.ExampleBasic usage: read the referenced entries of a groupimport 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"));
                }
            }
        });
    }
}
```

Reference field UID to read.

Content type UID to give each returned entry.

## getAsset

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

```
Returns: Asset. The asset in the file field.
ValidationThrows 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.BehaviorClient-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.ExampleBasic usage: read a file field of the groupimport 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());
                }
            }
        });
    }
}
```

Field UID to read.

## getAssets

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

```
Returns: List<Asset>. The assets in the file field.
ValidationThrows 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.BehaviorClient-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.ExampleBasic usage: read a multiple file field of the groupimport 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());
                }
            }
        });
    }
}
```

Field UID to read.

## getBoolean

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

```
Returns: Boolean. The field value as a Boolean.
ValidationThrows 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.BehaviorClient-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.ExampleBasic usage: read a boolean field of the groupimport 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");
                }
            }
        });
    }
}
```

Field UID to read.

## getDate

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

```
Returns: Calendar. The field value as a Calendar.
ValidationgetDate 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.BehaviorKnown 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.ExampleBasic usage: read a date field of the groupimport 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.timeimport 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());
            }
        });
    }
}
```

Field UID to read.

## getDouble

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

```
Returns: double. The field value as a double.
ValidationThrows 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.BehaviorClient-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.ExampleBasic usage: read a decimal field of the groupimport 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);
                }
            }
        });
    }
}
```

Field UID to read.

## getFloat

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

```
Returns: float. The field value as a float.
ValidationThrows 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.BehaviorClient-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.ExampleBasic usage: read a decimal field of the groupimport 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);
                }
            }
        });
    }
}
```

Field UID to read.

## getGroup

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

```
Returns: Group. The nested group in the field.
ValidationgetGroup returns null when the key is missing or empty, or when its value is not an object.Throws NullPointerException if key is null.BehaviorClient-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.ExampleBasic usage: read a field of a nested groupimport 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"));
                }
            }
        });
    }
}
```

Field UID to read.

## getGroups

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

```
Returns: List<Group>. The nested groups in the field.
ValidationgetGroups 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.BehaviorClient-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.ExampleBasic usage: read each item of a nested multiple Group fieldimport 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"));
                    }
                }
            }
        });
    }
}
```

Field UID to read.

## getInt

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

```
Returns: int. The field value as an int.
ValidationThrows 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.BehaviorClient-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.ExampleBasic usage: read a whole-number field of the groupimport 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);
                }
            }
        });
    }
}
```

Field UID to read.

## getJSONArray

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

```
Returns: JSONArray. The field value as a JSONArray.
ValidationThrows 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.BehaviorClient-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.ExampleBasic usage: read a multiple text field of the groupimport 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));
                }
            }
        });
    }
}
```

Field UID to read.

## getJSONObject

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

```
Returns: JSONObject. The field value as a JSONObject.
ValidationThrows 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.BehaviorClient-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.ExampleBasic usage: read a nested Group field as JSONimport 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"));
                }
            }
        });
    }
}
```

Field UID to read.

## getLong

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

```
Returns: long. The field value as a long.
ValidationThrows 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.BehaviorClient-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.ExampleBasic usage: read a whole-number field of the groupimport 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);
                }
            }
        });
    }
}
```

Field UID to read.

## getNumber

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

```
Returns: Number. The field value as a Number.
ValidationThrows 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.BehaviorClient-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.ExampleBasic usage: read a number field of the groupimport 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());
                }
            }
        });
    }
}
```

Field UID to read.

## getShort

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

```
Returns: short. The field value as a short.
ValidationThrows 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.BehaviorClient-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.ExampleBasic usage: read a small whole number of the groupimport 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);
                }
            }
        });
    }
}
```

Field UID to read.

## getString

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

```
Returns: String. The field value as a String.
ValidationThrows 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.BehaviorClient-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.ExampleBasic usage: read a text field of the groupimport 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 typeimport 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"));
                }
            }
        });
    }
}
```

Field UID to read.

## toJSON

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

```
Returns: JSONObject. The group's fields as JSON.
ValidationThere is no client-side validation. toJSON cannot fail.BehaviorClient-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.ExampleBasic usage: print the group JSONimport 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));
            }
        });
    }
}
```

## Group | Java Delivery SDK | Contentstack

The Group class in the Java Delivery SDK gives typed access to the fields of a Group field inside a fetched entry, including nested groups, assets, and references.