---
title: "encode(_:)"
framework: foundation
role: symbol
role_heading: Instance Method
path: "foundation/jsonencoder/encode(_:)"
---

# encode(_:)

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

## Declaration

```swift
func encode<T>(_ value: T) throws -> Data where T : Encodable
```

## Parameters

- `value`: The value to encode as JSON.

## Return Value

Return Value The encoded JSON data.

## Discussion

Discussion If there’s a problem encoding the value you supply, this method throws an error based on the type of problem: The value fails to encode, or contains a nested value that fails to encode—this method throws the corresponding error. The value isn’t encodable as a JSON array or JSON object—this method throws the EncodingError.invalidValue(_:_:) error. The value contains an exceptional floating-point number (such as infinity or nan) and you’re using the default JSONEncoder.NonConformingFloatEncodingStrategy — this method throws the EncodingError.invalidValue(_:_:) error.

## See Also

### First Steps

- [init()](foundation/jsonencoder/init().md)
