---
title: OptimizelyJSON for the Swift SDK
slug: feature-experimentation/docs/optimizelyjson-swift
docTags: 
createdAt: 2026-08-05T20:39:51.000Z
---

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

## Version

SDK v3.4.0 and higher

## Methods

You can access JSON representations with the following methods:

| Method       | Parameters            | Description                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              |
| ------------ | --------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| \`toString\` | none                  | Returns a string representation of the JSON object                                                                                                                                                                                                                                                                                                                                                                                                                                                       |
| \`toMap\`    | none                  | Returns a map representation of the JSON object: \`\[String: Any]\`                                                                                                                                                                                                                                                                                                                                                                                                                                      |
| \`getValue\` | \`jsonPath: String?\` | Returns a specified schema object (\`T\`) with the key/value pair of the JSON key you pass to this method. If JSON key is nil 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 \`nestedField2\` in \`\{field1: \{nestedField2: "blah"}}\`, you can call \`getValue\` with the parameter \`"field1.nestedField2"\`. |

## Object definition

The OptimizelyJSON object is defined as follows:

```swift
public class OptimizelyJSON: NSObject {
    public func toString() -> String?
  
    public func toMap() -> [String: Any]
  
    public func getValue<T: Decodable>(jsonPath: String?) -> T?
  
  	public func getValue<T>(jsonPath: String?) -> T?
}
```

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

```swift
//declare a schema object into which you want to unmarshal OptimizelyJson content:
struct SSub: Decodable {
    var field: String = ""
}
        
struct SObj: Decodable {
    var field1: Int = 0
    var field2: Double = 0.0
    var field3: String = ""
    var field4: SSub = SSub()
}
        
//parse all json key/value pairs into your schema, sObj
let robj: SObj? = self.optimizelyJSON.getValue(jsonPath: nil)
        
//or, parse the specified key/value pair with an integer value
let rint: Int? = self.optimizelyJSON.getValue(jsonPath: "field1")
        
//or, parse the specified key/value pair with a string value
let strValue: String? = self.optimizelyJSON.getValue(jsonPath: "field4.field")
```

## Source

The language/platform source files containing the implementation for Swift is [OptimizelyClient.swift](https://github.com/optimizely/swift-sdk/blob/master/Sources/Optimizely/OptimizelyClient.swift).
