# JSONValue > Reference for JSONValue, the type the iOS SDK uses for tool input and output Tool input and output can be any JSON, so the SDK carries them as `JSONValue` instead of `Any`. You can read it without casting and pass it between tasks safely. ## Overview `enum JSONValue: Sendable, Equatable, Codable` ```swift public enum JSONValue: Sendable, Equatable, Codable { case null case bool(Bool) case number(Double) case string(String) case array([JSONValue]) case object([String: JSONValue]) } ``` You'll meet it in four places: | Where | What you do | | - | - | | A tool handler's input | Read it | | A tool handler's return value | Write it | | `ToolCall.input` | Read it | | `ToolResult.output` and message parts | Read it | ## Reading values ### Looking up a key ```swift public subscript(key: String) -> JSONValue? { get } ``` Looks up a key in an object. Returns `nil` if the key is missing or the value isn't an object, so you can chain safely: ```swift let city = input["address"]?["city"]?.stringValue ``` ### Getting a Swift type ```swift public var stringValue: String? { get } public var numberValue: Double? { get } public var boolValue: Bool? { get } ``` Each returns `nil` if the value is a different type: ```swift await client.tool("book_table") { input in let name = input["name"]?.stringValue ?? "Guest" let party = Int(input["party_size"]?.numberValue ?? 2) let vip = input["vip"]?.boolValue ?? false // ... } ``` > [!NOTE] > > Every JSON number is a `.number(Double)`; there's no separate integer case. Convert with `Int(value)` when you need a whole number. ### Arrays and every case There's no array or object accessor: switch on the value to reach them, or to cover every possibility: ```swift func describe(_ value: JSONValue) -> String { switch value { case .null: return "-" case .bool(let b): return b ? "yes" : "no" case .number(let n): return n.formatted() case .string(let s): return s case .array(let a): return a.map(describe).joined(separator: ", ") case .object(let o): return o.map { "\($0): \(describe($1))" }.joined(separator: "; ") } } let tags: [String] = { guard case .array(let items)? = input["tags"] else { return [] } return items.compactMap(\.stringValue) }() ``` ## Writing values Build results from the cases: ```swift return .object([ "status": .string("shipped"), "eta_days": .number(2), "cost": .number(14.99), "expedited": .bool(true), "items": .array([.string("SKU-1"), .string("SKU-2")]), "note": .null ]) ``` `JSONValue` also accepts Swift literals, so the same result can be written more briefly: ```swift return [ "status": "shipped", "eta_days": 2, "cost": 14.99, "expedited": true, "items": ["SKU-1", "SKU-2"], "note": nil ] ``` String, integer, decimal, Boolean, `nil`, array and dictionary literals all work. Values that aren't literals need their case, such as `.string(order.status)`. ### Reporting a failure An **object with an `error` key** is how a tool reports a problem. The agent sees the tool as failed: ```swift return ["error": "No order found with that number"] ``` See [When a tool fails](/docs/ios-sdk/client-side-tools). ## Using your own types `JSONValue` is `Codable`, so you can convert to and from your own types with `JSONEncoder` and `JSONDecoder`. ### Reading input into a struct ```swift struct BookingInput: Decodable { let name: String let partySize: Int enum CodingKeys: String, CodingKey { case name case partySize = "party_size" } } await client.tool("book_table") { input in let booking = try JSONDecoder().decode( BookingInput.self, from: JSONEncoder().encode(input) ) // booking.name, booking.partySize, ... } ``` ### Returning a struct ```swift struct BookingResult: Encodable { let confirmationCode: String let time: String } await client.tool("book_table") { input in let result = BookingResult(confirmationCode: "AB12", time: "19:30") return try JSONDecoder().decode( JSONValue.self, from: JSONEncoder().encode(result) ) } ``` > [!TIP] > > A small extension in your app makes both directions easier to read: > > ```swift > extension JSONValue { > init(encoding value: T) throws { > self = try JSONDecoder().decode(JSONValue.self, from: JSONEncoder().encode(value)) > } > > func decoded(as type: T.Type = T.self) throws -> T { > try JSONDecoder().decode(T.self, from: JSONEncoder().encode(self)) > } > } > ``` > > ```swift > await client.tool("book_table") { input in > let booking: BookingInput = try input.decoded() > return try JSONValue(encoding: BookingResult(confirmationCode: "AB12", time: "19:30")) > } > ``` > > Throwing here is fine. The SDK turns it into an `error` result for the agent instead of failing the whole answer. Just keep the message safe for a customer to read. ## How values are read When the SDK decodes JSON, it tries the cases in this order: null, `Bool`, `Double`, `String`, array, object. So `true` is always `.bool`, and `9` and `9.0` are both `.number(9)`. ## Size limit A tool result can be at most **20 KB** of JSON. A bigger one is refused with `400` `VALIDATION_INVALID_BODY`. > [!WARNING] > > The agent reads tool results and they're saved with the conversation. Never put tokens, signed URLs, file paths, internal IDs or stack traces in a `JSONValue` you return. Return only what the agent needs to answer. ## Related > [card] Client-Side Tools | /docs/ios-sdk/client-side-tools | Where you use JSONValue most. > [card] Streaming Types | /docs/ios-sdk/streaming-events | ToolCall and ToolResult. > [card] ChatResponse | /docs/ios-sdk/chat-response | Message parts. > [card] SwiftUI | /docs/ios-sdk/swiftui | Showing tool input and output.