<!--
{
  "availability" : [
    "iOS: 8.0.0 -",
    "iPadOS: 8.0.0 -",
    "macCatalyst: 8.0.0 -",
    "macOS: 10.10.0 -",
    "tvOS: 9.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 2.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Foundation",
  "identifier" : "/documentation/Foundation/JSONEncoder",
  "metadataVersion" : "0.1.0",
  "role" : "Class",
  "symbol" : {
    "kind" : "Class",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "s:10Foundation11JSONEncoderC"
  },
  "title" : "JSONEncoder"
}
-->

# JSONEncoder

An object that encodes instances of a data type as JSON objects.

```
class JSONEncoder
```

## Overview

The example below shows how to encode an instance of a simple `GroceryProduct` type from a JSON object. The type adopts <doc://com.apple.documentation/documentation/Swift/Codable> so that it’s encodable as JSON using a [`JSONEncoder`](/documentation/Foundation/JSONEncoder) instance.

```swift
struct GroceryProduct: Codable {
    var name: String
    var points: Int
    var description: String?
}

let pear = GroceryProduct(name: "Pear", points: 250, description: "A ripe pear.")

let encoder = JSONEncoder()
encoder.outputFormatting = .prettyPrinted

let data = try encoder.encode(pear)
print(String(data: data, encoding: .utf8)!)

/* Prints:
 {
   "name" : "Pear",
   "points" : 250,
   "description" : "A ripe pear."
 }
*/
```

## Topics

### First Steps

[`init()`](/documentation/Foundation/JSONEncoder/init())

Creates a new, reusable JSON encoder with the default formatting settings and encoding strategies.

[`encode(_:)`](/documentation/Foundation/JSONEncoder/encode(_:))

Returns a JSON-encoded representation of the value you supply.

### Customizing Encoding

[`outputFormatting`](/documentation/Foundation/JSONEncoder/outputFormatting-swift.property)

A value that determines the readability, size, and element order of the encoded JSON object.

[`JSONEncoder.OutputFormatting`](/documentation/Foundation/JSONEncoder/OutputFormatting-swift.struct)

The output formatting options that determine the readability, size, and element order of an encoded JSON object.

[`keyEncodingStrategy`](/documentation/Foundation/JSONEncoder/keyEncodingStrategy-swift.property)

A value that determines how to encode a  type’s coding keys as JSON keys.

[`JSONEncoder.KeyEncodingStrategy`](/documentation/Foundation/JSONEncoder/KeyEncodingStrategy-swift.enum)

The values that determine how to encode a type’s coding keys as JSON keys.

[`userInfo`](/documentation/Foundation/JSONEncoder/userInfo)

A dictionary you use to customize the encoding process by providing contextual information.

### Encoding Dates

[`dateEncodingStrategy`](/documentation/Foundation/JSONEncoder/dateEncodingStrategy-swift.property)

The strategy used when encoding dates as part of a JSON object.

[`JSONEncoder.DateEncodingStrategy`](/documentation/Foundation/JSONEncoder/DateEncodingStrategy-swift.enum)

The formatting strategies available for formatting dates when encoding a date as JSON.

### Encoding Raw Data

[`dataEncodingStrategy`](/documentation/Foundation/JSONEncoder/dataEncodingStrategy-swift.property)

The strategy that an encoder uses to encode raw data.

[`JSONEncoder.DataEncodingStrategy`](/documentation/Foundation/JSONEncoder/DataEncodingStrategy-swift.enum)

The strategies for encoding raw data.

### Encoding Exceptional Numbers

[`nonConformingFloatEncodingStrategy`](/documentation/Foundation/JSONEncoder/nonConformingFloatEncodingStrategy-swift.property)

The strategy used by an encoder when it encounters exceptional floating-point values.

[`JSONEncoder.NonConformingFloatEncodingStrategy`](/documentation/Foundation/JSONEncoder/NonConformingFloatEncodingStrategy-swift.enum)

The strategies for encoding nonconforming floating-point numbers, also known as IEEE 754 exceptional values.

### Supporting Types



---

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)