<!--
{
  "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/KeyEncodingStrategy-swift.enum/convertToSnakeCase",
  "metadataVersion" : "0.1.0",
  "role" : "Case",
  "symbol" : {
    "kind" : "Case",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "s:10Foundation11JSONEncoderC19KeyEncodingStrategyO18convertToSnakeCaseyA2EmF"
  },
  "title" : "JSONEncoder.KeyEncodingStrategy.convertToSnakeCase"
}
-->

# JSONEncoder.KeyEncodingStrategy.convertToSnakeCase

A key encoding strategy that converts camel-case keys to snake-case keys.

```
case convertToSnakeCase
```

## Discussion

Camel-case and snake-case are two common approaches for combining words when naming parts of an API. The Swift [API Design Guidelines](https://swift.org/documentation/api-design-guidelines/#general-conventions) recommend using camel-case names. Some JSON APIs adopt snake-case; use this strategy when you encounter such an API.

This strategy uses [`uppercaseLetters`](/documentation/Foundation/CharacterSet/uppercaseLetters) and [`lowercaseLetters`](/documentation/Foundation/CharacterSet/lowercaseLetters) to determine the boundaries between words, and the [`system`](/documentation/Foundation/NSLocale/system) locale when converting uppercase letters to lowercase letters.

This strategy follows these steps to convert key names to snake-case:

1. Split the name into words, preserving leading or trailing underscores.
2. Insert an underscore between each word.
3. Convert the resulting string to lowercase.

The following examples show the result of applying this strategy:

- `feeFiFoFum`: Converts to: `fee_fi_fo_fum`
- `fee_fi_fo_fum`: Converts to: `fee_fi_fo_fum`
- `xmlContents`: Converts to: `xml_contents`

The example below shows how properties on the `OlympicEventResult` structure convert to snake-case when encoded as keys in a JSON object.

```swift
struct OlympicEventResult: Codable {
    var goldWinner: String
    var silverWinner: String
    var bronzeWinner: String
}

let marathonResult = OlympicEventResult(goldWinner: "Light", silverWinner: "Sound", bronzeWinner: "Unladen Swallow")

let encoder = JSONEncoder()
encoder.outputFormatting = [.prettyPrinted, .sortedKeys]
let encodeAndPrint = { print(String(data: try! encoder.encode(marathonResult), encoding: .utf8)!) }

encoder.keyEncodingStrategy = .convertToSnakeCase
encodeAndPrint()

/* Prints:
{
  "bronze_winner" : "Unladen Swallow",
  "gold_winner" : "Light",
  "silver_winner" : "Sound"
}
*/
```

## See Also

[`JSONDecoder.KeyDecodingStrategy.convertFromSnakeCase`](/documentation/Foundation/JSONDecoder/KeyDecodingStrategy-swift.enum/convertFromSnakeCase)

A key decoding strategy that converts snake-case keys to camel-case keys.



---

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)