How to properly format an array inside a struct from JSON

Hi, I have a struct that looks like the following:

struct Event: Codable, Identifiable {

    var id:Int = 0
    var datetime:String = ""
    var location:[Int] = []
    
}

This is the data I am receiving from the API in JSON format.

{
    "id": 123,
    "datetime": "1970-01-01 12:00:00",
    "location": {
        "latitude": 1,
        "longitude": 1
    }
}

However, when running my project, I get the following error when I try and view this data.

Thread 5: Fatal error: 'try!' expression unexpectedly raised an error: Swift.DecodingError.typeMismatch(Swift.Array<Any>, Swift.DecodingError.Context(codingPath: [_JSONKey(stringValue: "Index 0", intValue: 0), CodingKeys(stringValue: "events", intValue: nil), _JSONKey(stringValue: "Index 0", intValue: 0), CodingKeys(stringValue: "location", intValue: nil)], debugDescription: "Expected to decode Array<Any> but found a dictionary instead.", underlyingError: nil))

If I comment out the var location:[String] = [] part within the struct the project works fine without any issues. So I'm assuming there's some kind of formatting issue I'm having it?

Could someone point me in the right direction?

It looks to me as if your struct has a location property that is an Array of Ints. That's not what the JSON is made of.

What if you change that property to the following (a Dictionary of String:Ints)

var location: [String: Int] = [:]

Indeed.

This is how to read the error you are getting to know exact location of the problem (it's the last line in the codingPath, along with the key path leading to it):

Swift.DecodingError.typeMismatch(
	Swift.Array<Any>,
	Swift.DecodingError.Context(
		codingPath: [
			🔴 _JSONKey(stringValue: "Index 0", intValue: 0),
			🟢 CodingKeys(stringValue: "events", intValue: nil),
			🔵 _JSONKey(stringValue: "Index 0", intValue: 0),
			🟠 CodingKeys(stringValue: "location", intValue: nil)
		],
		debugDescription: "Expected to decode Array<Any> but found a dictionary instead.",
		underlyingError: nil
	)
)

[
    {												// 🔴 _JSONKey(stringValue: "Index 0", intValue: 0),
        "events": [									// 🟢 CodingKeys(stringValue: "events", intValue: nil),
            {										// 🔵 _JSONKey(stringValue: "Index 0", intValue: 0),
                "id": 123,
                "datetime": "1970-01-01 12:00:00",
                "location": {						// 🟠 CodingKeys(stringValue: "location", intValue: nil)
                    "latitude": 1,
                    "longitude": 1
                }
            }
        ]
    }
]

typealias Records = [Record]						// 🔴 _JSONKey(stringValue: "Index 0", intValue: 0),

struct Record: Codable {
    var events: [Event]								// 🟢 CodingKeys(stringValue: "events", intValue: nil),
    												// 🔵 _JSONKey(stringValue: "Index 0", intValue: 0),
    struct Event: Codable, Identifiable {
        var id:Int = 0
        var datetime:String = ""
        var location:[Int] = []						// 🟠 CodingKeys(stringValue: "location", intValue: nil)
    }
}