# How to decode JSON object, which contains different keys?

**URL:** <https://forums.swift.org/t/how-to-decode-json-object-which-contains-different-keys/55440>\
**Category:** Using Swift\
**Tags:** json\
**Created:** [February 19, 2022, 3:18pm UTC](https://forums.swift.org/t/how-to-decode-json-object-which-contains-different-keys/55440 "2022-02-19T15:18:41Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Abrc](https://avatars.discourse-cdn.com/v4/letter/a/41988e/32.png) [@Abrc](https://forums.swift.org/u/Abrc)\
**Post date:** [February 19, 2022, 3:18pm UTC](https://forums.swift.org/t/how-to-decode-json-object-which-contains-different-keys/55440/1 "2022-02-19T15:18:41Z")

</div>

I have json response like this and server returns different keys for "prices" and in sometimes "prices" may be false

```swift
{ "id": 194,
  "name": "Apple iPhone 11 128GB Black",
  "prices": {
        "100": "23500",
        "200": "23300"
    }

```

```swift
{ "id": 195,
  "name": "Samsung Galaxy A12",
  "prices": {
        "500": "23500",
        "200": "23300"
    }

```

When keys for prices were only "100" and "200", I decoded like this but it now gives error:

```swift
struct ProductDetail: Decodable, Hashable {
    
    let name: String
    let prices: PricesResponse

```

```swift
struct PricesResponse: Decodable, Hashable {
  
    let firstPrice: String
    let secondPrice: String

    enum CodingKeys: String, CodingKey, Decodable {

        case firstPrice = "100"
        case secondPrice = "200"
    

```

Could you please say how can I decode json in this case?

---

<div class="post-metadata">

**Author:** ![Morten\_Bek\_Ditlevsen](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/morten_bek_ditlevsen/32/28133_2.png) [@Morten\_Bek\_Ditlevsen](https://forums.swift.org/u/Morten_Bek_Ditlevsen)\
**Post date:** [February 19, 2022, 8:02pm UTC](https://forums.swift.org/t/how-to-decode-json-object-which-contains-different-keys/55440/2 "2022-02-19T20:02:27Z")

</div>

One idea could be to decode a dictionary of the prices?

```swift
let priceResponse: [String: String]

```

That would be a good choice if the keys for the prices are completely dynamic.

---

<div class="post-metadata">

**Author:** ![taylorswift](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/taylorswift/32/43413_2.png) [@taylorswift](https://forums.swift.org/u/taylorswift)\
**Post date:** [February 19, 2022, 9:58pm UTC](https://forums.swift.org/t/how-to-decode-json-object-which-contains-different-keys/55440/3 "2022-02-19T21:58:58Z")

</div>

you should be able to use [`swift-json`](https://github.com/kelvin13/swift-json) for this:

```swift
guard case .object(let items) = 
        try Grammar.parse(utf8, as: JSON.Rule<String.Index>.Root.self), 
        case .object(let prices)? = items["prices"]
else 
{
    ...
}

```

which will give you a `[String: JSON]` dictionary. you will then have to check that each `JSON` value is a `JSON.string(_:)` case.

---

<div class="post-metadata">

**Author:** ![jack](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jack/32/20250_2.png) [@jack](https://forums.swift.org/u/jack)\
**Post date:** [February 20, 2022, 4:35am UTC](https://forums.swift.org/t/how-to-decode-json-object-which-contains-different-keys/55440/4 "2022-02-20T04:35:00Z")

</div>

`Codable` supports this, actually. `CodingKey` doesn't need to be an enum, it can be a struct. Then in your `init(from:)`, you can loop over `allKeys` to read out your keys. For known keys, you can define static properties.

```swift
struct WhateverObject: Codable {
    var myProperty: String // properties...

    struct CodingKeys: CodingKey, Hashable {
        var stringValue: String

        static let myProperty = CodingKeys(stringValue: "myProperty")
        
        init(stringValue: String) {
            self.stringValue = stringValue
        }
        
        // Unused, only for int-backed Codables.
        var intValue: Int? { return nil }
        init?(intValue: Int) { return nil }
    }

    init(from decoder: Decoder) throws {
        let container = try decoder.container(keyedBy: CodingKeys.self)
        
        myProperty = container.decode(String.self, forKey: .myProperty)

        let dynamicKeys = container.allKeys
        for key in dynamicKeys {
            // do whatever you want to do with the keys.
        }
    }
}

```
