<!--
{
  "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/jsonObject(with:options:)-3afap",
  "metadataVersion" : "0.1.0",
  "role" : "Type Method",
  "symbol" : {
    "kind" : "Type Method",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "c:objc(cs)NSJSONSerialization(cm)JSONObjectWithStream:options:error:"
  },
  "title" : "jsonObject(with:options:)"
}
-->

# jsonObject(with:options:)

Returns a Foundation object from JSON data in a given stream.

```
class func jsonObject(with stream: InputStream, options opt: JSONSerialization.ReadingOptions = []) throws -> Any
```

## Parameters

`stream`

A stream from which to read JSON data.

    The stream should be open and configured.

`opt`

Options for reading the JSON data and creating the Foundation objects.

    For possible values, see [`JSONSerialization.ReadingOptions`](/documentation/Foundation/JSONSerialization/ReadingOptions)    .

## Return Value

A Foundation object from the JSON data in `stream`.

## Discussion

The data in the stream must be in one of the 5 supported encodings listed in the JSON specification: UTF-8, UTF-16LE, UTF-16BE, UTF-32LE, UTF-32BE. The data may or may not have a BOM. The most efficient encoding to use for parsing is UTF-8, so if you have a choice in encoding the data passed to this method, use UTF-8.

> Handling Errors in Swift:
> In Swift, this method returns a nonoptional result and is marked with the `throws` keyword to indicate that it throws an error in cases of failure.
> 
> You call this method in a `try` expression and handle any errors in the `catch` clauses of a `do` statement, as described in [Error Handling](https://docs.swift.org/swift-book/LanguageGuide/ErrorHandling.html) in [The Swift Programming Language](https://docs.swift.org/swift-book/) and `About Imported Cocoa Error Parameters`.

## See Also

[`writeJSONObject(_:to:options:error:)`](/documentation/Foundation/JSONSerialization/writeJSONObject(_:to:options:error:))

Writes a given JSON object to a stream.



---

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)