---
title: "writeJSONObject(_:to:options:error:)"
framework: foundation
role: symbol
role_heading: Type Method
path: "foundation/jsonserialization/writejsonobject(_:to:options:error:)"
---

# writeJSONObject(_:to:options:error:)

Writes a given JSON object to a stream.

## Declaration

```swift
class func writeJSONObject(_ obj: Any, to stream: OutputStream, options opt: JSONSerialization.WritingOptions = [], error: NSErrorPointer) -> Int
```

## Parameters

- `obj`: The object to write to stream.
- `stream`: The stream to which to write. The stream should be open and configured.
- `opt`: Options for writing the JSON data. See doc://com.apple.foundation/documentation/Foundation/JSONSerialization/WritingOptions for possible values.
- `error`: If an error occurs, upon return contains an NSError object with code doc://com.apple.foundation/documentation/Foundation/NSPropertyListWriteInvalidError-swift.var that describes the problem.

## Return Value

Return Value The number of bytes written to the stream, or 0 if an error occurs.

## See Also

### Creating JSON Data

- [data(withJSONObject:options:)](foundation/jsonserialization/data(withjsonobject:options:).md)
- [JSONSerialization.WritingOptions](foundation/jsonserialization/writingoptions.md)
- [isValidJSONObject(_:)](foundation/jsonserialization/isvalidjsonobject(_:).md)
