Skip to content

decoding a json with variable keys #195

Description

@pdegouville

I am trying to decode a JSON like this one with Himotoki in Swift 3

next: [
          {
            question: 'Fréquence',
            condition: { '==': ['answer', 'Actions'] },
          },
          {
            question: 'Fréquence',
            condition: { '>=': ['answer', 'Stop'] },
          },
          {
            question: 'Risque',
            condition: { '<=': ['answer', 'nul'] }
          }
        ]

In the condition, there is an object that looks like a dictionary, since the field name changes ('==', '<=', '>=, ...). The value is then an array of string

I have tried to decode like this

typealias ConditionType = [String]

extension ConditionType: Decodable {}

struct NextItem {
    let question: String?
    let condition: [String : ConditionType]?
}

extension NextItem: Decodable {
    static func decode(_ e: Extractor) throws -> NextItem {

        return try NextItem(
            question:  e <|? "question",
            condition: e <|-|? "condition"
        )
    }
}

I do not not how to decode the ConditionType since there is no field name for the array

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions