Swift - the Data Couldn't Be Read Because It Isn't in the Correct Format

Swift - The data couldn’t be read because it isn’t in the correct format

Try this

let gitData = try decoder.decode(Root.self, from: data!)

Traverse through your data

for singleData in gitData.data where (singleData.name ?? "") == "Cafe" {
print(singleData.image)
}

Swiftui The data couldn’t be read because it isn’t in the correct format

Your design is wrong.

Replace

let countrys = try JSONDecoder().decode(APIResult.self, from: APIData)

DispatchQueue.main.async {

if self.fetchCountries == nil {
self.fetchCountries = countrys.data.results
}
}

With

let countries = try JSONDecoder().decode([Countries].self, from: APIData)

DispatchQueue.main.async {
self.fetchCountries = countries
}

the other two structs make no sense.

And declare fetchCountries as non-optional empty array and name the struct in singular form Country.

Problem while converting JSON to Swift The data couldn’t be read because it isn’t in the correct format.

First of all

never print(error.localizedDescription).

in a JSONDecoder catch block. You get a generic but quite meaningless error message.

Always print the entire error, DecodingErrors are very descriptive

print(error)

Your code contains three major errors, one of them (error #3) occurs multiple times

Error #1

typeMismatch(Swift.Dictionary<Swift.String, Any>, Swift.DecodingError.Context(codingPath: [], debugDescription: "Expected to decode Dictionary<String, Any> but found an array instead.", underlyingError: nil))

indicates that the root object is an array, the JSON starts clearly with [

Solution: Decode [CryptListStruct].self


Error #2

dataCorrupted(Swift.DecodingError.Context(codingPath: [_JSONKey(stringValue: "Index 0", intValue: 0), CodingKeys(stringValue: "volume_1mth_usd", intValue: nil)], debugDescription: "Parsed JSON number <3699822674922524.74> does not fit in Int.", underlyingError: nil))

indicates that the received value 3699822674922524.74 is actually a Double.

Solution: Declare

let volume1MthUsd: Double

Error #3

keyNotFound(CodingKeys(stringValue: "id_icon", intValue: nil), Swift.DecodingError.Context(codingPath: [_JSONKey(stringValue: "Index 4", intValue: 4)], debugDescription: "No value associated with key CodingKeys(stringValue: "id_icon", intValue: nil) ("id_icon").", underlyingError: nil))

indicates that the key id_icon is missing (at least) in the 5th item of the array.

Solution: Declare the type as optional

let idIcon : String?

The same error occurs for dataTradeStart, dataTradeEnd, dataQuoteStart, dataQuoteEnd, dataOrderbookStart, dataOrderbookEnd, dataStart, dataEnd

let dataQuoteStart, dataQuoteEnd, dataOrderbookStart, dataOrderbookEnd: String?
let dataTradeStart, dataTradeEnd : String?
let dataStart, dataEnd: String?

Side note:

You can delete the entire CodingKeys enum if you replace assetID with assetId and add the convertFromSnakeCase key decoding strategy

let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase
let crpytList = try decoder.decode([CryptListStruct].self, from: data)

The data couldn’t be read because it isn’t in the correct format [swift 3]

You're right, problem occurred because of "\n". I tried your code without "\n" and it's work perfectly.

I replaced "\n" by "\\n", and iOS seems to convert the string to dictionary :

let value =  "{\"state\":\"NY\",\"city\":\"NY\",\"postalCode\":\"22002\",\"value\":\"Fifth Avenue1\nNY NY 22002\nUSA\",\"iosIdentifier\":\"71395A78-604F-47BE-BC3C-7F932263D397\",\"street\":\"Fifth Avenue1\",\"country\":\"USA\"}"

if let data = value.replacingOccurrences(of: "\n", with: "\\n").data(using: String.Encoding.utf8) {
do {
let a = try JSONSerialization.jsonObject(with: data, options: .mutableLeaves) as? [String: Any]
NSLog("check \(a)")
} catch {
NSLog("ERROR \(error.localizedDescription)")
}
}

I obtained this in my log :

check Optional(["value": Fifth Avenue1
NY NY 22002
USA, "country": USA, "city": NY, "iosIdentifier": 71395A78-604F-47BE-BC3C-7F932263D397, "street": Fifth Avenue1, "postalCode": 22002, "state": NY])

The data couldn't be read because it isn't in the correct format

yes ,there is an issue in your model you don't need to use the (Response) only use the Model (ResultItem) the JSON isn't complex JSON like that it just array of (ResultItem)

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("something went wrong")
return
}
do {
let result = try JSONDecoder().decode([ResultItem].self, from: data)
print(result)
}
catch {
print("failed to convert\(error.localizedDescription)")
}

})
task.resume()
}

struct ResultItem: Codable {
let categorie: String
}

Swift The data couldn’t be read because it isn’t in the correct format

Your JSON format doesn't quite match what you're trying to decode. You need a wrapper for the latest_photos array at the root of your JSON object.

For example:

struct LatestPhotosWrapper: Codable {
let latestPhotos: [LatestPhoto]

enum CodingKeys: String, CodingKey {
case latestPhotos = "latest_photos"
}
}
let apod = try JSONDecoder().decode(LatestPhotosWrapper.self, from: data)

(Rather than providing a CodingKey, you can also look into the built-in systems for converting from snake case: https://developer.apple.com/documentation/foundation/jsondecoder/keydecodingstrategy/convertfromsnakecase)

Also, you may want to print the error and not just the error.localizedDescription -- you can get a better picture of what's going on. For example, with your original code, you get:

Expected to decode Array but found a dictionary instead.

Finally, you might check out app.quicktype.io -- you can paste in your JSON and get correct Swift structs pre-built for you.

Swift : The data couldn’t be read because it isn’t in the correct format

Just your Album model is incorrect.

struct Album: Codable {
var source : Source
var id : String

enum CodingKeys: String, CodingKey {
case source = "_source"
case id = "_id"
}
}

struct Source: Codable {
var nome : String
var endereco : String?
var uf : String?
var cidade : String?
var bairro : String?
}

If you don't want _id altogether then simply remove the related parts.

As for your Alamofire related code, that part is good.


Notable improvements:

  • Have avoided underscored variable name in model by customizing CodingKeys for key mapping purpose
  • Typenames should always start with a Capital letter (so _source is Source)

    • Similarly, variable names should always start with a lowercase letter
  • Made some variables optional (based on your updated response)

    • Keeping a variable non-optional means it must be present in the response for the model to be created
    • Making a variable optional means that key may or may not be present in the response and it not being there won't prevent the model from being created

Swift, The data couldn’t be read because it isn’t in the correct format

The way you making your model is wrong, your're not assigning default value in case of nil or error. So decoding always failed.

try to create model this way or use online tools:

struct ResultRootModel : Codable {
let results : [ResultsModel]?
let page : Int?
let total_results : Int?
let total_pages : Int?

enum CodingKeys: String, CodingKey {

case results = "results"
case page = "page"
case total_results = "total_results"
case total_pages = "total_pages"
}

init(from decoder: Decoder) throws {
let values = try decoder.container(keyedBy: CodingKeys.self)
results = try values.decodeIfPresent([ResultsModel].self, forKey: .results)
page = try values.decodeIfPresent(Int.self, forKey: .page)
total_results = try values.decodeIfPresent(Int.self, forKey: .total_results)
total_pages = try values.decodeIfPresent(Int.self, forKey: .total_pages)
}

}

struct ResultsModel : Codable {
let video : Bool?
let vote_average : Double?
let popularity : Double?
let vote_count : Int?
let release_date : String?
let adult : Bool?
let backdrop_path : String?
let overview : String?
let genre_ids : [Int]?
let title : String?
let original_language : String?
let original_title : String?
let poster_path : String?
let id : Int?

enum CodingKeys: String, CodingKey {

case video = "video"
case vote_average = "vote_average"
case popularity = "popularity"
case vote_count = "vote_count"
case release_date = "release_date"
case adult = "adult"
case backdrop_path = "backdrop_path"
case overview = "overview"
case genre_ids = "genre_ids"
case title = "title"
case original_language = "original_language"
case original_title = "original_title"
case poster_path = "poster_path"
case id = "id"
}

init(from decoder: Decoder) throws {
let values = try decoder.container(keyedBy: CodingKeys.self)
video = try values.decodeIfPresent(Bool.self, forKey: .video)
vote_average = try values.decodeIfPresent(Double.self, forKey: .vote_average)
popularity = try values.decodeIfPresent(Double.self, forKey: .popularity)
vote_count = try values.decodeIfPresent(Int.self, forKey: .vote_count)
release_date = try values.decodeIfPresent(String.self, forKey: .release_date)
adult = try values.decodeIfPresent(Bool.self, forKey: .adult)
backdrop_path = try values.decodeIfPresent(String.self, forKey: .backdrop_path)
overview = try values.decodeIfPresent(String.self, forKey: .overview)
genre_ids = try values.decodeIfPresent([Int].self, forKey: .genre_ids)
title = try values.decodeIfPresent(String.self, forKey: .title)
original_language = try values.decodeIfPresent(String.self, forKey: .original_language)
original_title = try values.decodeIfPresent(String.self, forKey: .original_title)
poster_path = try values.decodeIfPresent(String.self, forKey: .poster_path)
id = try values.decodeIfPresent(Int.self, forKey: .id)
}

}

change this line

let json = try JSONDecoder().decode(result.self, from: datos) 

to

let json = try JSONDecoder().decode(ResultRootModel.self, from: datos)

Some suggestion:

start Struct/Class name with Uppercase

print print(error) instead debugPrint(error.localizedDescription) it will give more explanation on error like below:

keyNotFound(CodingKeys(stringValue: "budget", intValue: nil), Swift.DecodingError.Context(codingPath: [CodingKeys(stringValue: "results", intValue: nil), _JSONKey(stringValue: "Index 0", intValue: 0)], debugDescription: "No value associated with key CodingKeys(stringValue: \"budget\", intValue: nil) (\"budget\").", underlyingError: nil))


Related Topics



Leave a reply



Submit