Swift Parse Json - 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)
}

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)

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

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.

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])

How to exclude properties from Swift Codable?

The list of keys to encode/decode is controlled by a type called CodingKeys (note the s at the end). The compiler can synthesize this for you but can always override that.

Let's say you want to exclude the property nickname from both encoding and decoding:

struct Person: Codable {
var firstName: String
var lastName: String
var nickname: String?

private enum CodingKeys: String, CodingKey {
case firstName, lastName
}
}

If you want it to be asymmetric (i.e. encode but not decode or vice versa), you have to provide your own implementations of encode(with encoder: ) and init(from decoder: ):

struct Person: Codable {
var firstName: String
var lastName: String

// Since fullName is a computed property, it's excluded by default
var fullName: String {
return firstName + " " + lastName
}

private enum CodingKeys: String, CodingKey {
case firstName, lastName, fullName
}

// We don't want to decode `fullName` from the JSON
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
firstName = try container.decode(String.self, forKey: .firstName)
lastName = try container.decode(String.self, forKey: .lastName)
}

// But we want to store `fullName` in the JSON anyhow
func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(firstName, forKey: .firstName)
try container.encode(lastName, forKey: .lastName)
try container.encode(fullName, forKey: .fullName)
}
}


Related Topics



Leave a reply



Submit