Hard time trying to decode local JSON File.

Hi! I am having a hard time with this JSON decode and optionals thing. I have this struct :

    var id: Int64?
     var title : String?
     var artist : String?
     var isOut : String?
     var label : String?
     var vinylAudoID : String?
     var vinylCountry : String?
     var vinylFormat : String?
     var vinylID : String?
     var vinylLocation : String?
     var year : String?
}

and here is my class to decode a local JSON File that is inside my project.

    @Published var vinyls = [Vinyl]()
    
    init() {
        loadData()
    }
    
    func loadData() {
        
        guard let url = Bundle.main.url(forResource: "vinyls", withExtension: "json") else {
            print("deu erro")
            return
            
        }
        let data = try? Data(contentsOf: url)
        let vinyls = try? JSONDecoder().decode([Vinyl].self, from: data!)

        self.vinyls = vinyls!
    }
}

here is my JSON file:

    "vinyls" : [
        {
            "id" : 1,
            "title" : "I’m Missing You",
            "artist" : "Fabrica",
            "isOut" : "False",
            "label" : "Dance Pool",
            "vinylAudoID" : "00mwDSOTStNtIMRWIZPs",
            "vinylCountry" : "Italy",
            "vinylFormat" : "12",
            "vinylID" : "DAN6652126",
            "vinylLocation" : "LR25",
            "year" : "1997"
        },
        {
            "id" : 2,
            "title" : "No More, Baby",
            "artist" : "Disco Blu",
            "isOut" : "False",
            "label" : "DJ Approved",
            "vinylAudoID" : "02Q6i5tJfUBNEXxanfMM",
            "vinylCountry" : "Italy",
            "vinylFormat" : "12",
            "vinylID" : "APP 9706",
            "vinylLocation" : "LR11",
            "year" : "1997"
        },
        {
            "id" : 3,
            "title" : "Carramba",
            "artist" : "Arena Blanca",
            "isOut" : "False",
            "label" : "Outta Records",
            "vinylAudoID" : "02loclZIMqGHzIXI9Xo5",
            "vinylCountry" : "Italy",
            "vinylFormat" : "12",
            "vinylID" : "OTA696004",
            "vinylLocation" : "LR19",
            "year" : "1996"
        }]
    }

and here is the error message: JSON_Parse__vinyl_/ReadData.swift:27: Fatal error: Unexpectedly found nil while unwrapping an Optional value

what is wrong with my code?

thank you very much!

I would change try? to try to see if any of those throw

Is the error on this line ?

        let data = try? Data(contentsOf: url)

or here:

        self.vinyls = vinyls!

If the first, that means data is nil:

Check with this:

if data != nil {
        let vinyls = try? JSONDecoder().decode([Vinyl].self, from: data!)
        self.vinyls = vinyls!
} else {
  print("data nil")
}

What do you get ?

If data is nil, check directly the url in Safari. What do you get ?

You have to create a struct ith the array.

I tested this that works (don't forget starting curly brace in the JSON):

struct VinylLibrary: Codable {
    var vinyls: [Vinyl]
}

struct Vinyl: Codable {
    var id: Int64?
    var title : String?
    var artist : String?
    var isOut : String?
    var label : String?
    var vinylAudoID : String?
    var vinylCountry : String?
    var vinylFormat : String?
    var vinylID : String?
    var vinylLocation : String?
    var year : String?
}

let jsonVinylData =
"""
  {  "vinyls": [
        {
            "id" : 1,
            "title" : "I’m Missing You",
            "artist" : "Fabrica",
            "isOut" : "False",
            "label" : "Dance Pool",
            "vinylAudoID" : "00mwDSOTStNtIMRWIZPs",
            "vinylCountry" : "Italy",
            "vinylFormat" : "12",
            "vinylID" : "DAN6652126",
            "vinylLocation" : "LR25",
            "year" : "1997"
        },
        {
            "id" : 2,
            "title" : "No More, Baby",
            "artist" : "Disco Blu",
            "isOut" : "False",
            "label" : "DJ Approved",
            "vinylAudoID" : "02Q6i5tJfUBNEXxanfMM",
            "vinylCountry" : "Italy",
            "vinylFormat" : "12",
            "vinylID" : "APP 9706",
            "vinylLocation" : "LR11",
            "year" : "1997"
        },
        {
            "id" : 3,
            "title" : "Carramba",
            "artist" : "Arena Blanca",
            "isOut" : "False",
            "label" : "Outta Records",
            "vinylAudoID" : "02loclZIMqGHzIXI9Xo5",
            "vinylCountry" : "Italy",
            "vinylFormat" : "12",
            "vinylID" : "OTA696004",
            "vinylLocation" : "LR19",
            "year" : "1996"
        }]
    }
""".data(using: .utf8)

let vinylLib = try? JSONDecoder().decode(VinylLibrary.self, from: jsonVinylData!)

print("first title", vinylLib!.vinyls[0].title!)

And get

first title I’m Missing You
Hard time trying to decode local JSON File.
 
 
Q