Home > database >  Parse JSON from file using Codable swift
Parse JSON from file using Codable swift

Time:02-26

I have JSON file with cities

[
{"country":"UA","name":"Hurzuf","_id":707860,"coord":{"lon":34.283333,"lat":44.549999}},
{"country":"RU","name":"Novinki","_id":519188,"coord":{"lon":37.666668,"lat":55.683334}},
{"country":"NP","name":"Gorkhā","_id":1283378,"coord":{"lon":84.633331,"lat":28}},
{"country":"IN","name":"State of Haryāna","_id":1270260,"coord":{"lon":76,"lat":29}},
{"country":"UA","name":"Holubynka","_id":708546,"coord":{"lon":33.900002,"lat":44.599998}},
{"country":"NP","name":"Bāgmatī Zone","_id":1283710,"coord":{"lon":85.416664,"lat":28}},
{"country":"RU","name":"Mar’ina Roshcha","_id":529334,"coord":{"lon":37.611111,"lat":55.796391}},
{"country":"IN","name":"Republic of India","_id":1269750,"coord":{"lon":77,"lat":20}},
{"country":"NP","name":"Kathmandu","_id":1283240,"coord":{"lon":85.316666,"lat":27.716667}},
{"country":"UA","name":"Laspi","_id":703363,"coord":{"lon":33.733334,"lat":44.416668}},
{"country":"VE","name":"Merida","_id":3632308,"coord":{"lon":-71.144997,"lat":8.598333}},
{"country":"RU","name":"Vinogradovo","_id":473537,"coord":{"lon":38.545555,"lat":55.423332}},
{"country":"IQ","name":"Qarah Gawl al ‘Ulyā","_id":384848,"coord":{"lon":45.6325,"lat":35.353889}},
{"country":"RU","name":"Cherkizovo","_id":569143,"coord":{"lon":37.728889,"lat":55.800835}},
{"country":"UA","name":"Alupka","_id":713514,"coord":{"lon":34.049999,"lat":44.416668}},
{"country":"DE","name":"Lichtenrade","_id":2878044,"coord":{"lon":13.40637,"lat":52.398441}},
{"country":"RU","name":"Zavety Il’icha","_id":464176,"coord":{"lon":37.849998,"lat":56.049999}},
{"country":"IL","name":"‘Azriqam","_id":295582,"coord":{"lon":34.700001,"lat":31.75}},
{"country":"IN","name":"Ghūra","_id":1271231,"coord":{"lon":79.883331,"lat":24.766666}}
]

These are only few entries, I have almost 1000 entries into file. I need to apply pagination to load 100 entries.

I created codable as follows

struct Cities: Codable {
    let city: [City]?
    let pagination: Pagination?
}

struct City: Codable{
    let country : String
    let name : String
    let _id : Int
    let coord: [Coordinates]?
}

struct Coordinates: Codable {
    let lat : Double?
    let lon : Double?
}

struct Pagination: Codable {
    let limit, offset: Int?
}

and my parsing method is like this,

func getDataFrom(completion: @escaping (Cities?, Error?) -> Void) {
        let url = Bundle.main.url(forResource: "cities", withExtension: "json")!
        let data = try! Data(contentsOf: url)
        do {
            let jsonDescription = try JSONDecoder().decode(Cities.self, from: data)
            print(jsonDescription)
            completion(jsonDescription,nil)
        }
        catch let jsonError {
            print("Json Error:", jsonError)
        }
    }

When I parse the data it goes into catch block and gives this error

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

Can anyone tell me what I am doing wrong. I need to parse this data and show on tableview with pagination.

Thanks in advance.

CodePudding user response:

You are trying to decode a type of Cities.self, but your JSON is an array - it starts with "[" and ends with "]". You might want to try declaring the type [Cities].self in your decoder call.

let jsonDescription = try JSONDecoder().decode([City].self, from: data)

Edit:

@Joakim_Danielson caught an issue, you need to decode the array [City] and not [Cities], because that's what your JSON is (I edited the above line of code accordingly).

But there is another issue: the property coord in your struct City is declared as an array [Coordinates]?, but your JSON does not have an array in the "coord" key - just a single instance.

Try changing your coord property, make it a type of Coordinate?.

  • Related