I want to have the following JSON structure (fake example):
{
"Admin" :
{
"name" : "John",
"age" : "42"
},
"Sales" :
{
"name" : "John",
"age" : "42"
},
"CEO" :
{
"name" : "Peter",
"age" : "52",
"salary" : "100000"
},
"Janitor" :
{
"name" : "Matthew",
"age" : "22"
}
}
As you can see, the structure if determined, but the name of the structure is dynamic.
How can I convert this to a Swift Codable struct? Current try:
struct Positions: Codable
{
var posDicts: [String: Position] = [:]
}
struct Position: Codable
{
let name: String
let age: Int
let salary: Int?
}
However, this would give the following:
"posDicts" : {
"Admin" :
{
"name" : "John",
"age" : "42"
},
"Sales" :
{
"name" : "John",
"age" : "42"
},
"CEO" :
{
"name" : "Peter",
"age" : "52",
"salary" : "100000"
},
"Janitor" :
{
"name" : "Matthew",
"age" : "22"
}
}
I don't want the "posDicts" in the JSON. What would be the best/simplest solution?
P.S.: related question regarding decodable Swift Codable with dynamic keys