JSON conversion problem appears

I’m at the parsing the JSON into SWIFT Instances and I ran onto small problem,
and this is my code which is the same :

but on mine I have this error and I googled a bit and came up to this solution(solution below), so I wanted to know from the pros is this good way or not. Everything is fine with json file imported properly but maybe something changed so far.

if let path = Bundle.main.path(forResource: "DemoData", ofType: "json"){
        let url = URL(filePath: path) (I added. this so problem goes away)
        do {
            // Read the file and turn it into data
            let data = try Data(contentsOf: url)
        }
        catch {
            print("Error occurred: \(error)")
        }
        
        
        // Parse JSON into Swift instances
    }

In the screenshot where you have this code:

if let url = Bundle.main.path(forResource: "DemoData", ofType: "json") {

what you should have done is this:

if let url = Bundle.main.url(forResource: "DemoData", withExtension: "json") {

and the code you had in the screenshot would have worked fine.

1 Like

Great, thank you!