<!--
{
  "availability" : [
    "iOS: 5.0.0 -",
    "iPadOS: 5.0.0 -",
    "macCatalyst: 13.1.0 -",
    "macOS: 10.7.0 -",
    "tvOS: 9.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 2.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Foundation",
  "identifier" : "/documentation/Foundation/JSONSerialization/writeJSONObject(_:to:options:error:)",
  "metadataVersion" : "0.1.0",
  "role" : "Type Method",
  "symbol" : {
    "kind" : "Type Method",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "c:objc(cs)NSJSONSerialization(cm)writeJSONObject:toStream:options:error:"
  },
  "title" : "writeJSONObject(_:to:options:error:)"
}
-->

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

Writes a given JSON object to a stream.

```
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 [`JSONSerialization.WritingOptions`](/documentation/Foundation/JSONSerialization/WritingOptions)     for possible values.

`error`

If an error occurs, upon return contains an `NSError` object with code [`NSPropertyListWriteInvalidError`](/documentation/Foundation/NSPropertyListWriteInvalidError-swift.var) that describes the problem.

## Return Value

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

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)