# keyDecodingStrategy and keys with custom raw values

**URL:** <https://forums.swift.org/t/keydecodingstrategy-and-keys-with-custom-raw-values/12392>\
**Category:** Using Swift\
**Tags:** foundation, codable, json\
**Created:** [May 3, 2018, 10:42pm UTC](https://forums.swift.org/t/keydecodingstrategy-and-keys-with-custom-raw-values/12392 "2018-05-03T22:42:40Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![itaiferber](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/itaiferber/32/33840_2.png) [@itaiferber](https://forums.swift.org/u/itaiferber)\
**Post date:** [May 4, 2018, 4:28pm UTC](https://forums.swift.org/t/keydecodingstrategy-and-keys-with-custom-raw-values/12392/2 "2018-05-04T16:28:16Z")

</div>

Funny that this came up pretty much right after I posted [Handling edge cases in JSONEncoder/JSONDecoder key conversion strategies (SR-6629)](https://forums.swift.org/t/handling-edge-cases-in-jsonencoder-jsondecoder-key-conversion-strategies-sr-6629/12384). This is a good data point for discussing the topic, I think.

What you're seeing here is a combination of two things:

1. The raw value of the key is affected by the key strategy. Anything consuming a `CodingKey` can only operate on its `stringValue`/`intValue`; there's no way for it to know that these values are being provided directly vs. being synthesized. As far as `JSONEncoder`/`JSONDecoder` is concerned, there's no difference between `case last_name` and `case surname = "last_name"`, because there's no way for it to discern the difference
2. The behavior on decode with the key decoding strategy is described in the linked thread — when you `decode(String.self, forKey: .surname)`, the key in the JSON (`"last_name"`) is converted to camelCase based on the `.keyDecodingStrategy`, and the string value of `.surname` (`"last_name"`) is used to look up the value. Because the JSON payload has already been converted to camelCase `"last_name"` isn't found. Instead, you have to provide the camelCase name

(1) here happens by design (this is the idea behind the key strategy), but we're looking to improve on (2). Please take a look at the linked thread if you get a chance; I'd value your feedback there.

---

_[View the full topic](https://forums.swift.org/t/keydecodingstrategy-and-keys-with-custom-raw-values/12392)._
