Enumeration Case


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


case convertFromSnakeCase


Snake-case and camel-case are two common approaches for combining words when naming parts of an API. The Swift API Design Guidelines recommend using camel-case names. Some JSON APIs adopt snake-case; use this strategy when you encounter such an API.

This strategy uses uppercaseLetters and lowercaseLetters to determine the boundaries between words, and the system locale when capitalizing letters.

This strategy follows these steps to convert JSON keys to camel-case:

  1. Capitalize each word that follows an underscore.

  2. Remove all underscores that aren't at the very start or end of the string.

  3. Combine the words into a single string.

The following examples show the result of applying this strategy:


Converts to: feeFiFoFum


Converts to: feeFiFoFum


Converts to: baseUri

The example below shows how properties on the OlympicEventResult structure are converted from snake-case when decoded as keys in a JSON object.

struct OlympicEventResult: Codable {
    var goldWinner: String
    var silverWinner: String
    var bronzeWinner: String

let json = """
    "silver_winner": "Sound",
    "gold_winner": "Light",
    "bronze_winner": "Unladen Swallow"
""".data(using: .utf8)!

let decoder = JSONDecoder()
let decodeAndPrint = { print(try! decoder.decode(OlympicEventResult.self, from: json)) }

decoder.keyDecodingStrategy = .convertFromSnakeCase

// Prints: "OlympicEventResult(goldWinner: "Light", silverWinner: "Sound", bronzeWinner: "Unladen Swallow")"

See Also

Built-in Decoding

case useDefaultKeys

A key decoding strategy that doesn't change key names during decoding.