Type Method


Returns JSON data from a Foundation object.


+ (NSData *)dataWithJSONObject:(id)obj options:(NSJSONWritingOptions)opt error:(NSError * _Nullable *)error;



The object from which to generate JSON data. Must not be nil.


Options for creating the JSON data.

See NSJSONWritingOptions for possible values.


If an internal error occurs, upon return contains an NSError object with code NSPropertyListWriteInvalidError that describes the problem.

Return Value

JSON data for obj, or nil if an internal error occurs. The resulting data is encoded in UTF-8.


If obj will not produce valid JSON, an exception is thrown. This exception is thrown prior to parsing and represents a programming error, not an internal error. You should check whether the input will produce valid JSON before calling this method by using isValidJSONObject:.

Setting the NSJSONWritingPrettyPrinted option will generate JSON with whitespace designed to make the output more readable. If that option is not set, the most compact possible JSON will be generated.

See Also

Creating JSON Data

+ writeJSONObject:toStream:options:error:

Writes a given JSON object to a stream.

+ isValidJSONObject:

Returns a Boolean value that indicates whether a given object can be converted to JSON data.