-2

I have been away from Swift for the last 12 months but have just come back to an application that was previously working (Swift 4.0) but now seems to be failing and I am not sure why.

My problem relates to the JSONDecoder and trying to decode an array of arrays of strings.

var tryingToDecode: [[String]]

The JSON that I am using has the following format (See table below) for the actual data please use the following link.

JASON DATA

enter image description here

The code I am using is (See below) self.requestData is the JSON data I am using, which works when decoding all my other data, it just does not work with [[String]]

func TEST_decodeReceivedJSON() {
    let decoder = JSONDecoder()
    do {
        let array = try decoder.decode(DataStruct.self, from: self.requestData)
        print(array)
    } catch {
        print("Error")
    }
}

The struct that I am using for the decode is

struct DataStruct: Codable {
var data: [[String]]

}

This is just test code, but when I compile it I always get thrown to the catch error. I have tried searching online but can't find any relevent examples. The strange thing is that prior to Xcode 10 this worked, it was even accepted in the App Store. I have now been informed by a number of users that something is not working and this is indeed the case, it seems to be related to this particular section where the [[String]] is decoded using DataStruct.

Any help or pointers would be very much appreciated.

[EDIT 001] Added a link to the JSON data, the code below shows a minimal example, I am specifically interested in how I should be accessing the [[String]] -- the array of arrays of strings. I am trying to assertain as this was working before is there something wrong with the way I am trying to decode the JSON (maybe a Swift update/change)or is there maybe a problem with JSONDecoder.

[EDIT 002] The solution was [[String?]] and the problem was indeed in the JSON, you just can't see it in the text blizzard of the raw data, if you look at the table view below you can clearly see that Item 10 is "null" and as a consequence the code required an optional String.

enter image description here

fuzzygoat
  • 26,573
  • 48
  • 165
  • 294
  • 2
    Please show the real JSON data, not some table view – even better: a *minimal* example demonstrating the problem. – Martin R Dec 09 '18 at 17:33
  • 3
    The decoder tells you exactly what the problem is, but you are throwing that information away. Why? Replace `print("Error")` with `print(error)` and you will instantly know the issue. – matt Dec 09 '18 at 18:11
  • 1
    I have tested your code with JSON data `{ "data" : [["foo", "bar"], ["baz"]] }` and it works correctly. Your JSON behind the link looks quite different and does not match the `struct DataStruct`. – We need a [mcve]. – Martin R Dec 09 '18 at 18:12
  • Thank you all for the great comments, thank you matt for the "error" pointer, its much appreciated. – fuzzygoat Dec 09 '18 at 19:43

1 Answers1

1

The issue is not with the JSONDecoder, the issue is with your JSON data.

When I checked your code I'm getting the following error:

valueNotFound(Swift.String, Swift.DecodingError.Context(codingPath: [CodingKeys(stringValue: "data", intValue: nil), _JSONKey(stringValue: "Index 11", intValue: 11), _JSONKey(stringValue: "Index 10", intValue: 10)], debugDescription: "Expected String but found null value instead.", underlyingError: nil))

In your code you are expecting string, but when I checked your JSON data it contains null values and it will obviously break the written code. To fix this issue all you have to do is, change your model definition to accept null/nil values:

struct DataStruct: Codable {
    var data: [[String?]]
}
Midhun MP
  • 103,496
  • 31
  • 153
  • 200
  • Midhun, that missing "?" was exactly the problem, I had to do a bit of unwrapping later in my code but it now works as expected. I am not sure if this change has come from the way that Swift handles optionals (I know that has evolved over the years) or if the "null" fields are something that are new in the JSON data and as a consequence did not trip me up before. Anyways thank you very much, you hit the problem exactly, Much appreciated Sir. – fuzzygoat Dec 09 '18 at 19:30
  • 1
    @fuzzygoat You are welcome. I think the `null` items in the JSON data is new, there are no drastic change happened to the JSONDecoder class after it was introduced. Anyway, happy to hear that your issue is solved. Happy Coding !!! – Midhun MP Dec 09 '18 at 19:55