Hi there,
I am trying to decode JSON response that's being sent by ServiceNow but I am getting an error -
"keyNotFound(CodingKeys(stringValue: "short_description", intValue: nil), Swift.DecodingError.Context(codingPath: [], debugDescription: "No value associated with key CodingKeys(stringValue: "short_description", intValue: nil) ("short_description").", underlyingError: nil))"
JSON response sent by ServiceNow:
{
"result": {
"short_description": "broken door",
"due_date": "2022-04-01",
"sys_mod_count": "0",
"inspector": "",
"sys_updated_on": "2022-03-06 05:17:59",
"priority": "low",
"sys_tags": "",
"number": "SAFT0001034",
"sys_id": "e9823a3d07024119898908080ed0ce",
"sys_updated_by": "admin",
"opened_by": {
"link": "https://dev******.service-now.com/api/now/table/sys_user/TOP2",
"value": "TOP2"
},
"sys_created_on": "2022-03-06 05:17:59",
"location": {
"link": "https://dev******.service-now.com/api/now/table/cmn_location/2e3c49b037d0200044e0829849df9",
"value": "2e3c49b037d0200044e0b8493849e5df9"
},
"state": "new",
"category": "big",
"sys_created_by": "admin",
"assigned_to": ""
}
}
Here is the code:
struct Opened_By: Codable {
var link: String = ""
var value: String = ""
}
struct Location: Codable {
var link: String = ""
var value: String = ""
}
struct Response: Codable {
var short_description: String = ""
var due_date: String = ""
var sys_mod_count: String = ""
var inspector: String = ""
var sys_updated_on: String = ""
var priority: String = ""
var sys_tags: String = ""
var number: String = ""
var sys_id: String = ""
var sys_updated_by: String = ""
var opened_by: Opened_By
var sys_created_on: String = ""
var location: Location
var state: String = ""
var category: String = ""
var sys_created_by: String = ""
var assigned_to: String = ""
}
Any help in decoding the response will be greatly appreciated.
Many thanks :)
There a few things I don't understand in your code.
What doc you do with this dictionary ?
let dictionary = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as! [String:Any]
You have a result keyword in your JSON file, but I do not see it in the servicenowResponse
You should either declare it in servicenowResponse
struct SomeResponse: Codable {
var short_description: String = ""
// etc …
}
struct ServiceNowResponse: Codable {
var someResponse : SomeResponse()
}
or isolate what is inside result for decoder.
Note: struct name should start with upperCase and be CamelCase : ServiceNowResponse