---
title: OptimizelyJSON for the Java SDK
slug: feature-experimentation/docs/optimizelyjson-java
docTags: 
createdAt: 2026-08-05T20:39:54.000Z
---

Since statically typed languages lack native support for JSON, the Java SDK uses the OptimizelyJSON object to retrieve JSON in a flexible way.

## Version

SDK v3.5 and higher

## Methods

You can access JSON representations with the following methods:

| Method |
| ------ |

```javascript
  <th>
    Parameters
  </th>

  <th>
    Description
  </th>
</tr>
```

\| \`toString\` |

````javascript
  <td>
    none
  </td>

  <td>
    Returns a string representation of the JSON object
  </td>
</tr>

<tr>
  <td>
    `toMap`
  </td>

  <td>
    none
  </td>

  <td>
    Returns a map representation of the JSON object: `(Map<String,Object>)`
  </td>
</tr>

<tr>
  <td>
    `getValue`
  </td>

  <td>
    ```
    String jsonKey, 
    Class<T> clazz
    ```
  </td>

  <td>
    Returns a specified schema object  (`T`) with the key/value pair of the JSON key you pass to this method.  

    If JSON key is null or empty, it populates your schema object with all JSON key/value pairs.  

    You can retrieve information for a nested member of the JSON data structure by using flattened JSON dot notation.\
    For example, if you want to access the key `nestedField` in  `{field1: {nestedField: "blah"}}`, you can call `getValue` with the parameter `"field1.nestedField"`.
  </td>
</tr>
````

The OptimizelyJSON object is defined as follows:

```java
public class OptimizelyJSON {
    public String toString();
  
    public Map<String,Object> toMap();
  
    public <T> T getValue(@Nullable String jsonKey, Class<T> clazz) throws JsonParseException
}
```

## Examples

You can use `OptimizelyJSON` object, for example to:

- Get a JSON string by calling  the `toString` method, or
- Retrieve a specified schema from the `OptimizelyJSON` object by calling the `getValue` method.

The following example shows how to use an OptimizelyJSON object to populate a schema object you declare.

```java
//declare a schema object into which you want to unmarshal OptimizelyJson content:
public static class SSub {
	public String field;
}
public static class SObj {
	public int field1;
	public double field2;
	public String field3;
	public SSub field4;
}

//parse all json key/value pairs into your schema, sObj
SObj robj = optlyJSON.getValue(null, SObj.class);

//or, parse the specified key/value pair with an integer value 
Integer rint = optlyJSON.getValue("field1", Integer.class);

//or, parse the specified key/value pair with a string value
String rstr = optlyJSON.getValue("field4.field", String.class);
```
