I have an object from 20 fields. When I get the json from the server I get an error about the decode of the json.
There is a way to quickly find out which field is problematic instead of deleting all the fields and putting it back one after the other to find out which one is problematic.
If you're using Codable
to parse your JSON, you can simply print the error
in catch
block and it will print the complete details of where the issue exist.
do {
let response = try JSONDecoder().decode(Root.self, from: data)
print(response)
} catch {
print(error) //here.....
}
You can also add additional catch blocks to understand exactly what is the nature of the error.
do {
let decoder = JSONDecoder()
let messages = try decoder.decode(Response.self, from: data)
print(messages as Any)
} catch DecodingError.dataCorrupted(let context) {
print(context)
} catch DecodingError.keyNotFound(let key, let context) {
print("Key '\(key)' not found:", context.debugDescription)
print("codingPath:", context.codingPath)
} catch DecodingError.valueNotFound(let value, let context) {
print("Value '\(value)' not found:", context.debugDescription)
print("codingPath:", context.codingPath)
} catch DecodingError.typeMismatch(let type, let context) {
print("Type '\(type)' mismatch:", context.debugDescription)
print("codingPath:", context.codingPath)
} catch {
print("error: ", error)
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With