I am trying to decode an API call and I keep getting an error stating:
keyNotFound(CodingKeys(stringValue: "instruction", intValue: nil),
Swift.DecodingError.Context(codingPath: [], debugDescription: "No
value associated with key CodingKeys(stringValue: \"instruction\",
intValue: nil) (\"instruction\").", underlyingError: nil))
This is the json data
"resourceSets": [
{
"resources": [
{
"routeLegs": [
{
"itineraryItems": [
{
"compassDirection": "south",
"details": [
{
"compassDegrees": 182,
"endPathIndices": [
1
],
"maneuverType":
"DepartStart",
"mode": "Driving",
"names": [
"Infinite Loop"
],
"roadType": "Street",
"startPathIndices": [
0
]
}
],
"iconType": "Auto",
"instruction": {
"formattedText": "<Action>Depart</Action> <RoadName>Infinite Loop</RoadName> toward <Toward>Mariani Ave</Toward>",
"maneuverType": "DepartStart",
"text": "Depart Infinite Loop toward Mariani Ave"
},
Here is my data structs and coding keys
struct TruckRoute: Codable {
let resourceSets: [ResourceSet]
enum CodingKeys: String, CodingKey {
case resourceSets
}
}
// MARK: - ResourceSet
struct ResourceSet: Codable {
let resources: [Resource]
}
// MARK: - Resource
struct Resource: Codable {
let routeLegs: [RouteLeg]
enum CodingKeys: String, CodingKey {
case routeLegs
}
}
// MARK: - RouteLeg
struct RouteLeg: Codable {
let itineraryItems: [ItineraryItem]
enum CodingKeys: String, CodingKey {
case itineraryItems
}
}
// MARK: - ItineraryItem
struct ItineraryItem: Codable {
let instruction: Instruction
private enum CodingKeys: String, CodingKey {
case instruction
}
}
// MARK: - Instruction
struct Instruction: Codable {
let formattedText, maneuverType, text: String
private enum CodingKeys: String, CodingKey {
case formattedText, maneuverType, text
}
}
https://dev.virtualearth.net/REST/v1/Routes/Truck?wp.0=37.33233141,%20-122.0312186&wp.1=33.1956369,%20-117.3784666&key=AhLvBEtcGrKMDAIHHOhIhDm-aMM_dvGqstttp3wLfs8-J2A0UVBgN5bkzvaiZhj8
Related
I want to take weather informations with JSON but there is an error : The data couldn’t be read because it isn’t in the correct format.
Error 'It looks like your post is mostly code; please add some more details.' in stackoverflow. Although I briefly describe my problem, it still expects an explanation from me :/
override func viewDidLoad() {
super.viewDidLoad()
let url = "https://api.openweathermap.org/data/2.5/weather?q=bursa,tr&appid=00f63a1cff271776651468c0204c422c"
getData(from: url)
}
private func getData (from url : String){
let task = URLSession.shared.dataTask(with: URL(string: url)!, completionHandler: { data , response , error in
guard let data = data , error == nil else {
print ("birşeyler ters gitti")
return
}
var main : Response?
do {
main = try JSONDecoder().decode(Response.self , from: data)
} catch{
print ("ERROR IS HERE!!! \(error.localizedDescription)")
}
guard let json = main else {
return
}
print (json.weather)
})
task.resume()
}}
struct Response : Codable {
let weather : myResult
let status : String
}
struct myResult : Codable {
let main : String
let description : String
let icon : String
}
API Response is like that :
{"coord": { "lon": 139,"lat": 35},
"weather": [
{
"id": 800,
"main": "Clear",
"description": "clear sky",
"icon": "01n"
}
],
"base": "stations",
"main": {
"temp": 281.52,
"feels_like": 278.99,
"temp_min": 280.15,
"temp_max": 283.71,
"pressure": 1016,
"humidity": 93
},
"wind": {
"speed": 0.47,
"deg": 107.538
},
"clouds": {
"all": 2
},
"dt": 1560350192,
"sys": {
"type": 3,
"id": 2019346,
"message": 0.0065,
"country": "JP",
"sunrise": 1560281377,
"sunset": 1560333478
},
"timezone": 32400,
"id": 1851632,
"name": "Shuzenji",
"cod": 200
}
First, error.localizedDescription is meant to display an information for the user. It's not useful for debugging. If you replace it with error:
} catch {
print ("ERROR IS HERE!!! \(error)") // <- remove .localizedDescription
}
you will get more details:
ERROR IS HERE!!! typeMismatch(Swift.Dictionary<Swift.String, Any>,
Swift.DecodingError.Context(codingPath: [CodingKeys(stringValue:
"weather", intValue: nil)], debugDescription: "Expected to decode
Dictionary<String, Any> but found an array instead.", underlyingError:
nil))
To solve this you need to declare weather as an array:
let weather: [myResult]
I'd also recommend replacing myResult with Weather (or at least capitalised MyResult) as it will be more readable:
struct Weather: Codable {
let main: String
let description: String
let icon: String
}
Also, in the JSON response you provided there is no status field so you may need to remove it from the Response class (or make it optional).
If you'd like to add more fields to the response, declare them according to your JSON structure. Eg. if you want to add humidity and temperature you can do:
struct Response: Codable {
...
let main: Main
}
struct Main: Codable {
let temp: Double
let humidity: Double
}
To have a more readable code you can use CodingKeys - then your variable names can be independent from JSON variables.
struct Main: Codable {
enum CodingKeys: String, CodingKey {
case temperature = "temp"
case humidity
}
let temperature: Double
let humidity: Double
}
Summing up, your Response may look like this:
struct Response: Codable {
let weather: [Weather]
let main: Main
// alternatively declare `status` optional
// let status: String?
}
struct Weather: Codable {
let main: String
let description: String
let icon: String
}
struct Main: Codable {
enum CodingKeys: String, CodingKey {
case temperature = "temp"
case humidity
}
let temperature: Double
let humidity: Double
}
I'm trying to access data in a json api like below
"Products": [
{
"ProductName": "GR",
"ShortDescription": "General service epoxy mortar system that utilizes recycled glass and rapidly renewable soy based components.",
"PDSOverride": [
{
"FileName": "EcoLab Netherlands",
"FileUrl": "http://test.stonhard.com/media/2264/eco-lab-netherlands-usa-version.pdf"
},
{
"FileName": "General Dynamics.pdf",
"FileUrl": "http://test.stonhard.com/media/2060/general-dynamics.pdf"
}
]
}
]
And I'm modeling this in a struct like this below
struct Solutions: Codable, Identifiable {
let id = UUID()
let SectionTitle: String
let SectionImage: String
let ProductLines: [ProductLine]
}
struct ProductLine: Codable {
let System: String
let Products: [Product]
}
struct Product: Codable {
let ProductName: String
let ShortDescription: String
let PDSOverride: [PDSOverride]
}
struct PDSOverride: Codable {
let FileName: String
let FileUrl: String
}
struct SdsPdf: Codable {
let FileName: String
let FileUrl: String
}
struct GuideSpecPdf: Codable {
let FileName: String
let FileUrl: String
}
When I try to access the data, I get an error that says The data couldn't be read because it isn't in the correct format. I know it's a problem with my model because when I comment out PDSOverride, SdsPdf, and GuideSpecPdf it works, but obviously I don't have access to that data. How do I model my struct so I can pull in that data?
The problem is not in your model, your JSON is bad formatted as it says by the compiler your JSON needs to look like this:
[
{
"Products": [
{
"ProductName": "GR",
"ShortDescription": "General service epoxy mortar system that utilizes recycled glass and rapidly renewable soy based components.",
"PDSOverride": [
{
"FileName": "EcoLab Netherlands",
"FileUrl": "http://test.stonhard.com/media/2264/eco-lab-netherlands-usa-version.pdf"
},
{
"FileName": "General Dynamics.pdf",
"FileUrl": "http://test.stonhard.com/media/2060/general-dynamics.pdf"
}
]
}
]
}
]
Also, the model could be done in this way, I recommend you to use Quicktype (https://app.quicktype.io) the online version is good and they have a desktop one:
// MARK: - PurpleProduct
struct PurpleProduct: Codable {
let products: [ProductProduct]
enum CodingKeys: String, CodingKey {
case products = "Products"
}
}
// MARK: - ProductProduct
struct ProductProduct: Codable {
let productName, shortDescription: String
let pdsOverride: [PDSOverride]
enum CodingKeys: String, CodingKey {
case productName = "ProductName"
case shortDescription = "ShortDescription"
case pdsOverride = "PDSOverride"
}
}
// MARK: - PDSOverride
struct PDSOverride: Codable {
let fileName: String
let fileURL: String
enum CodingKeys: String, CodingKey {
case fileName = "FileName"
case fileURL = "FileUrl"
}
}
typealias Products = [PurpleProduct]
And to decoded you can use the JSONDecoder, I also recommend you to check your json in this page: https://jsonformatter.curiousconcept.com
Assuming your JSON is only missing the closing braces, you can parse it like so:
struct Entry: Codable {
let products: [Product]
enum CodingKeys: String, CodingKey {
case products = "Products"
}
}
struct Product: Codable {
let productName, shortDescription: String
let pdsOverride: [PDSOverride]
enum CodingKeys: String, CodingKey {
case productName = "ProductName"
case shortDescription = "ShortDescription"
case pdsOverride = "PDSOverride"
}
}
struct PDSOverride: Codable {
let fileName: String
let fileURL: String
enum CodingKeys: String, CodingKey {
case fileName = "FileName"
case fileURL = "FileUrl"
}
}
do {
let entries = try JSONDecoder().decode([Entry].self, from: data)
} catch {
print(error)
}
I'm using Alamofire and Decodable for a Google Books API request for performing a search.
I've created a Decodable model which I will add below and it worked up until I try to get any other field below "title" in "volumeInfo" (I've been commenting out properties one by one to check where it fails). The error I get after uncommenting "authors", "publisher" or anything other than "title" inside "volumeInfo" is:
responseSerializationFailed(reason: Alamofire.AFError.ResponseSerializationFailureReason.decodingFailed(error: Swift.DecodingError.keyNotFound(CodingKeys(stringValue: "publisher", intValue: nil)
An example response is:
{
"kind": "books#volume",
"id": "Ett09eLWE5oC",
"etag": "WIwTdsmpnhs",
"selfLink": "https://www.googleapis.com/books/v1/volumes/Ett09eLWE5oC",
"volumeInfo": {
"title": "The Picture of Dorian Gray",
"authors": [
"Oscar Wilde"
],
"publisher": "Wordsworth Editions",
"publishedDate": "1992",
"description": "The handsome appearance of dissolute young Dorian Gray remains unchanged while the features in his portrait become distorted as his degeneration progresses",
"industryIdentifiers": [
{
"type": "ISBN_10",
"identifier": "1853260150"
},
{
"type": "ISBN_13",
"identifier": "9781853260155"
}
],
"readingModes": {
"text": false,
"image": true
},
"pageCount": 312,
"printType": "BOOK",
"categories": [
"Fiction"
],
"averageRating": 4.0,
"ratingsCount": 6,
"maturityRating": "NOT_MATURE",
"allowAnonLogging": false,
"contentVersion": "1.1.2.0.preview.1",
"panelizationSummary": {
"containsEpubBubbles": false,
"containsImageBubbles": false
},
"imageLinks": {
"smallThumbnail": "http://books.google.com/books/content?id=Ett09eLWE5oC&printsec=frontcover&img=1&zoom=5&edge=curl&source=gbs_api",
"thumbnail": "http://books.google.com/books/content?id=Ett09eLWE5oC&printsec=frontcover&img=1&zoom=1&edge=curl&source=gbs_api"
},
"language": "en",
"previewLink": "http://books.google.ro/books?id=Ett09eLWE5oC&pg=PA130&dq=9781853260155&hl=&cd=1&source=gbs_api",
"infoLink": "http://books.google.ro/books?id=Ett09eLWE5oC&dq=9781853260155&hl=&source=gbs_api",
"canonicalVolumeLink": "https://books.google.com/books/about/The_Picture_of_Dorian_Gray.html?hl=&id=Ett09eLWE5oC"
}
}
My model:
struct Response: Decodable {
struct VolumeInfo: Decodable {
var title: String
//var authors: [String]
var publisher: String
//var identifiers: [Identifier]
//var image: ImageLink
enum CodingKeys: String, CodingKey {
case title
//case authors
case publisher
//case identifiers = "industryIdentifiers"
//case image = "imageLinks"
}
}
struct Identifier: Decodable {
var type: String
var identifier: String
enum CodingKeys: String, CodingKey {
case type
case identifier
}
}
struct ImageLink: Decodable {
var url: URL
enum CodingKeys: String, CodingKey {
case url = "thumbnail"
}
}
var id: String
var volumeInfo: VolumeInfo
}
I have a below JSON response. And I want to fetch the allkeys of “data” which is [“rules”, “preference”, “goals”] using .keys method. But I couldn’t get the array of allkeys using .keys feature. I have attached my code snippet also. if you had faced this one, please suggest me to rid of this concern.
Although, I can get these allKeys using ObjectMapper and native Dictionary objects. I just need to know why I couldn't achieve this using Codable.
My json response
{
"statusCode": 200,
"status": "success",
"message": null,
"data": {
"rules": {
"goals": {
"min": "1",
"max": "3"
}
},
"preference": [
1,
2,
3
],
"goals": {
"total": 4,
"data": []
}
}
}
My code Snippet:
struct MeetingsDataModal: Codable {
let statusCode: Int?
let status: String?
let message: String?
let data: Results?
enum CodingKeys: String, CodingKey {
case statusCode = "statusCode"
case status = "status"
case message = "message"
case data = "data"
}
func allkeys() {
}
}
struct Results : Codable {
let rules: Rules?
let preference: [Preference]?
let goals: Goals?
enum CodingKeys: String, CodingKey {
case rules = "rules"
case preference = "preference"
case goals = "goals"
}
}
struct Rules : Codable {
}
struct Preference : Codable {
}
struct Goals : Codable {
}
My expectation
let content = try JSONDecoder().decode(MeetingsDataModal.self, from: (response as? Data)!)
print(content.data.keys)
But I am getting,
Value of type 'Results?' has no member 'keys'
Perhaps I am not understanding the question well but your "keys" are defined by your Codable protocol - so they are known. If you are using Swift 4.2+ you can take advantage of the CaseIterable protocol
struct Results: Codable {
let testOne: Int
let testTwo: String
enum CodingKeys: String, CodingKey, CaseIterable {
case testOne
case testTwo
}
}
Results.CodingKeys.allCases.map { $0.rawValue }
If you do need .keys. could add two-line code :
struct Results : Codable {
let rules: Rules?
let preference: [Preference]?
let goals: Goals?
enum CodingKeys: String, CodingKey {
case rules = "rules"
case preference = "preference"
case goals = "goals"
}
var keys : [String]{
return["rules", "preference","goals"]
}
}
Actually, if you don't like encoding/decoding way , we have another traditional JSON object can help you to handle unstructured JSON.
let obj = try JSONSerialization.jsonObject(with: response!, options: JSONSerialization.ReadingOptions.allowFragments)
print((((obj as! [String: Any?])["data"]) as! [String: Any?]).keys)
Here is one way to decode you data structure.
let d = """
{
"statusCode": 200,
"status": "success",
"message": null,
"data": {
"rules": {
"goals": {
"min": "1",
"max": "3"
}
},
"preference": [
1,
2,
3
],
"goals": {
"total": 4,
"data": []
}
}
}
""".data(using: .utf8)!
struct MeetingsDataModal: Decodable {
let statusCode: Int
let status: String
let message: String?
let data: Results
}
struct Results : Decodable {
let rules: Rules
let preference: [Int]
let goals: Goals
}
struct Rules : Decodable {
let goals : DirectData
}
struct DirectData : Decodable{
let min : String
let max : String
}
struct Goals : Decodable {
let total : Int
let data : [String]
}
let data0 = try JSONDecoder().decode(MeetingsDataModal.self, from: d)
print(data0)
I have a JSON response that I am parsing with the help of codable models.
Even though my models look good, I am getting the following error:
[Result]: FAILURE: keyNotFound(CodingKeys(stringValue: “user”, intValue: nil), Swift.DecodingError.Context(codingPath: [], debugDescription: “No value associated with key CodingKeys(stringValue: \“user\“, intValue: nil) (\“user\“).“, underlyingError: nil))
The JSON response is:
{
“status”: “success”,
“message”: “successfully.“,
“user”: {
“username”: “admin”,
“profileImage”: “/storage/default.png”
},
“data”: {
“cash”: {
“withdrawableCash”: “$999540”,
“outstandingOrders”: “$0”
},
“offering”: [
{
“company”: “ABCD”,
“location”: “San Francisco, CA”,
“amount”: 10
}
],
“history”: [
{
“amount”: 330,
“order_id”: 3,
“order_type”: “preBid”,
“status”: 2,
“message”: “Placed bid with 33 notes, on Auction,
“transaction_time”: “31-07-2018 05:31"
}
]
}
}
The models are:
public struct WalletResponseModel: Codable {
public let status: String
public let message: String
public let user: UserData
public let data: WalletData
}
public struct UserData: Codable {
public let username: String
public let profileImage: URL
enum CodingKeys: String, CodingKey {
case username
case profileImage = "profileImage"
}
}
public struct WalletData: Codable {
public let cash: Cash
public let history: [HistoryItem]
public let offerings: [Offering]
enum CodingKeys: String, CodingKey {
case cash
case history
case offerings = "offering"
}
}
public struct Cash: Codable {
public let withdrawableCash: String
public let outstandingOrders: String
}
public struct HistoryItem: Codable {
public let amount: Int
public let status: Int
public let orderId: Int
public let orderType: String
public let message: String
public let transactionTime: String
enum CodingKeys: String, CodingKey {
case amount, status, message
case transactionTime = "transaction_time"
case orderId = "order_id"
case orderType = "order_type"
}
}
public struct Offering: Codable {
public let company: String
public let amount: Int
public let location: String
}
Your json format is incorrect it contains “ ” instead of " " , the valid one is
{
"status": "success",
"message": "successfully.",
"user": {
"username": "admin",
"profileImage": "/storage/default.png"
},
"data": {
"cash": {
"withdrawableCash": "$999540",
"outstandingOrders": "$0"
},
"offering": [{
"company": "ABCD",
"location": "San Francisco, CA",
"amount": 10
}],
"history": [{
"amount": 330,
"order_id": 3,
"order_type": "preBid",
"status": 2,
"message": "Placed bid with 33 notes, on Auction",
"transaction_time": "31-07-2018 05:31"
}]
}
}