I'm trying to encode data but struggling to deal with empty array with no type as the API's I am working with needs me to send [] if there is no entry.
[
{
"RequestId": "5B6E36D9-8759-41BB-A0C0-EDFB116DFBB7",
"DataSources": ["5B6E36D9-8759-41BB-A0C0-EDFB116DFBB7"],
"GroupBy": [],
"Filters": []
}
]
above is the object json which I have to send.
struct ResponseElement: Encodable {
let requestID: String
let dataSources: [String]
let groupBy: []
let filters: []
enum CodingKeys: String, CodingKey {
case requestID = "RequestId"
case dataSources = "DataSources"
case groupBy = "GroupBy"
case filters = "Filters"
}
}
let data = ResponseElement(requestID: "5B6E36D9-8759-41BB-A0C0-EDFB116DFBB7",
dataSources: ["5B6E36D9-8759-41BB-A0C0-EDFB116DFBB7", ["5B6E36D9-8759-41BB-A0C0-EDFB116DFBB7]"],
groupBy: [],
filters: [])
let jsonEncoder = JSONEncoder()
let data = try! jsonEncoder.encode(data)
please note while creating data variable I have to pass groupBy, filters as empty array [], I have tried with [nil] which goes as [null] after encoding but it doesn't work in my case, it has to be []
how do I solve this please?