Faild to decode JSON, SwiftUI - json

I can’t seem to decode my JSON. It locates, and loads, but fails to decode.
Here is the JSON example:
[{
"name": "Hartsfield Jackson Atlanta Intl",
"city": "Atlanta",
"country": "United States",
"iata_code": "ATL",
"_geoloc": {
"lat": 33.636719,
"lng": -84.428067
},
"links_count": 1826,
"objectID": "3682"}]
Here is my Struct:
struct Airports: Codable, Identifiable {
struct GeoLoc: Codable {
let lat: Double
let lng: Double
}
var id = UUID()
let name: String
let city: String
let country: String
let iata_code: String
let _geoloc: GeoLoc
let links_count: Int
let objectID: String }
Bundle etension:
extension Bundle {
func decode<T: Codable>(_ file: String) -> T {
guard let url = self.url(forResource: file, withExtension: nil) else {
fatalError("Failed to locate \(file) in bundle.")
}
guard let data = try? Data(contentsOf: url) else {
fatalError("Failed to load \(file) from bundle.")
}
let decoder = JSONDecoder()
let formatter = DateFormatter()
formatter.dateFormat = "y-MM-dd"
decoder.dateDecodingStrategy = .formatted(formatter)
guard let loaded = try? decoder.decode(T.self, from: data) else {
fatalError("Failed to decode \(file) from bundle.")
}
return loaded
}}
And I call it in my content View like this:
let airports: [Airports] = Bundle.main.decode("airports.json")
I get the fatalError("Failed to decode (file) from bundle.")
So my question is, what is wrong here? Is the mistake in my Airport Struct?
I know the bundle extension is working, I have used the same with many other JSONS.

It is beacuse of property of id in your structure your json has no key id and id in structure is not optional. You have to change it with
let id : UUID?
instead of
var id = UUID()

Related

Can't Decode JSON successfully from file (Swift)

O.
I have a JSON file in my XCode project. Lets use this as an example:
{ "shifts": [
{"name": "Mary",
"role": "Waiter"},
{"name": "Larry",
"role": "Cook"}
]
}
In my JSONManager.swift, I have something along the lines of this:
struct Shift: Codable {
let role, name: String
static let allShifts: [Shift] = Bundle.main.decode(file: "shifts.json")
}
extension Bundle {
func decode<T: Decodable>(file: String) -> T {
guard let url = self.url(forResource: file, withExtension: nil) else {
fatalError("Could not find \(file) in the project")
}
guard let data = try? Data(contentsOf: url) else {
fatalError("Could not load \(file) in the project")
}
let decoder = JSONDecoder()
guard let loadedData = try? decoder.decode(T.self, from: data) else {
fatalError("Could not decode \(file) in the project")
}
return loadedData
}
}
I keep hitting the fatalError("Could not decode \(file) in the project") and I'm wondering if it's because the JSON is not formatted correctly or why it can't decode the JSON from the JSON file.
Take a look at you JSON structure for a momement.
You have a element called shifts, which is any array of other elements, but your code seems to trying to load a structure which only contains the child element.
Instead, you need a outer struct which contains the child struct, for example...
struct Shift: Codable {
let role, name: String
}
struct Shifts: Codable {
let shifts: [Shift]
}
Then you'd use the parent struct as the initial source type when decoding the content...
extension Bundle {
func decode<T: Decodable>(file: String) -> T {
guard let url = self.url(forResource: file, withExtension: nil) else {
fatalError("Could not find \(file) in the project")
}
guard let data = try? Data(contentsOf: url) else {
fatalError("Could not load \(file) in the project")
}
let decoder = JSONDecoder()
guard let loadedData = try? decoder.decode(T.self, from: data) else {
fatalError("Could not decode \(file) in the project")
}
return loadedData
}
}
let shifts: Shifts = Bundle.main.decode(file: "shifts.json")
I tested this in playgrounds and it worked fine.
There might be away to decode the structure without needing the "parent" struct, but this works for me.
I fixed my own mistake. I don't need the
{ "shifts": }
in my JSON

How to decode JSON response from API in SwiftUI to use in a view

I'm pretty new to developing apps and working with Xcode and Swift and trying to get a handle on how to decode JSON responses from an API. I'm also trying to follow MVVM practices.
I'm calling to an API that returns data structured like this:
"location": {
"latitude": -27.4748,
"longitude": 153.017 },
"date": "2020-12-21",
"current_time": "21:55:42.198",
"sunrise": "04:49",
"sunset": "18:42",
"...": "..."
}
My understanding is I need a struct to decode this information into. This is what my struct looks like (it may be incorrect):
struct Example: Decodable {
let location: Coordinates
let date: String
let current_time: String
let sunset: String
let sunset: String
let ... :String
struct Coordinates: Decodable{
let latitude: Double
let longitude: Double
}
}
So I want to be able to call this api. I have the correct address to call it because the dashboard on the website I'm using is showing I've hit it. Could I get some guidance on how to call it and decode the response? Currently I'm doing something like this:
if let url = URL(string: "this is the api web address"){
URLSession.shared.dataTask(with: url){ (with: data, options [] ) as?
[String:String]{
print("(\json)" + "for debugging purposes")
}}
else{
print("error")
}
.resume()
Thanks for any and all help. Again, my goal is to call this function, hit the API, decode the JSON response and store it into a variable I can use in my views elsewhere. Thanks!
You are mixing JSONSerialization with Codable and URLSession. Forget about JSONSerialization. Try to focus on Codable protocol.
struct Example: Decodable {
let location: Coordinates
let date: String
let currentTime: String
let sunrise: String
let sunset: String
}
struct Coordinates: Decodable {
let latitude: Double
let longitude: Double
}
This is how you can decode your json data synchronously
extension JSONDecoder {
static let shared: JSONDecoder = {
let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase
decoder.dateDecodingStrategy = .iso8601
return decoder
}()
}
extension Data {
func decodedObject<T: Decodable>() throws -> T {
try JSONDecoder.shared.decode(T.self, from: self)
}
}
Playground testing
let json = """
{
"location": {
"latitude": -27.4748,
"longitude": 153.017 },
"date": "2020-12-21",
"current_time": "21:55:42.198",
"sunrise": "04:49",
"sunset": "18:42",
}
"""
let data = Data(json.utf8)
do {
let example: Example = try data.decodedObject()
print(example)
} catch {
print(error)
}
And fetching your data asynchronously
extension URL {
func getResult<T: Decodable>(completion: #escaping (Result<T, Error>) -> Void) {
URLSession.shared.dataTask(with: self) { data, response, error in
guard let data = data, error == nil else {
completion(.failure(error!))
return
}
do {
completion(.success(try data.decodedObject()))
} catch {
completion(.failure(error))
}
}.resume()
}
}
let url = URL(string: "https://www.example.com/whatever")!
url.getResult { (result: Result<Example, Error>) in
switch result {
case let .success(example):
print(example)
case let .failure(error):
print(error)
}
}
If you need help with your SwiftUI project implementing MVVM feel free to open a new question.
Correct Method to decode a local JSON file in SwiftUI
do {
let path = Bundle.main.path(forResource: "filename", ofType: "json")
let url = URL(fileURLWithPath: path!)
let data = try Data(contentsOf: url)
let decodedresults = try JSONDecoder().decode(struct_name.self, from: data)
print(decodedresults)
} catch {
print(error)
}

Accessing JSON Array from API response in Swift

I am brand new to writing swift so any tips on improvement/best practices are welcome but my main issue is that I am having trouble accessing a nested JSON array list. I am using this free API and trying to show a list of characters https://swapi.dev/api/people/
Please see the code snippet below.
When I print the type its : Optional<Any> and when i print json["results"] it prints the array like:
Optional(<__NSArrayI 0x600000fe31e0>(
{
"birth_year" = 19BBY;
created = "2014-12-09T13:50:51.644000Z";
....
I have tried several different things but have been unsuccessful. Could someone please give some advice on how I might iterate the list under json["results"?
func onLoad() -> Void {
let url = URL(string: "https://swapi.dev/api/people")
guard let requestUrl = url else { fatalError() }
// Create URL Request
var request = URLRequest(url: requestUrl)
// Specify HTTP Method to use
request.httpMethod = "GET"
// Send HTTP Request
let task = URLSession.shared.dataTask(with: request) { (data, response, error) in
// Check if Error took place
if let error = error {
print("Error took place \(error)")
return
}
// Convert HTTP Response Data to a simple String
if let data = data {
// let json = try? JSONSerialization.jsonObject(with: data, options: [])
do {
if let json = try JSONSerialization.jsonObject(with: data, options: []) as? [String: Any] {
// try to read out a string array
print(type(of: json["results"]))
print(json["results"])
}
} catch let error as Error {
print("Failed to load: \(error.localizedDescription)")
}
}
}
task.resume()
}
Thanks for any help!
You should really be using Decodable rather than trying to parse it with JSON as that can easily lead to errors as you are accessing values by strings and it doesn't allow the IDE to help you.
You need to create some objects that describe what you are getting in your response.
Your main json response is made up of the following
{
"count": 82,
"next": "http://swapi.dev/api/people/?page=2",
"previous": null,
"results": [...]
}
This allows you to create a People struct that conforms to Decodable.
struct People: Decodable {
let count: Int
let next: URL?
let previous: URL?
let results: [Person]
}
The results array is really what you are after as that contains all the information about a person.
{
"name": "Luke Skywalker",
"height": "172",
"mass": "77",
"hair_color": "blond",
"skin_color": "fair",
"eye_color": "blue",
"birth_year": "19BBY",
"gender": "male",
"homeworld": "http://swapi.dev/api/planets/1/",
"films": [
"http://swapi.dev/api/films/1/",
"http://swapi.dev/api/films/2/",
"http://swapi.dev/api/films/3/",
"http://swapi.dev/api/films/6/"
],
"species": [],
"vehicles": [
"http://swapi.dev/api/vehicles/14/",
"http://swapi.dev/api/vehicles/30/"
],
"starships": [
"http://swapi.dev/api/starships/12/",
"http://swapi.dev/api/starships/22/"
],
"created": "2014-12-09T13:50:51.644000Z",
"edited": "2014-12-20T21:17:56.891000Z",
"url": "http://swapi.dev/api/people/1/"
}
We can represent this with the following struct called Person that also conforms to Decodable
struct Person: Decodable {
let name: String
let height: String
let mass: String
let hairColor: String
let skinColor: String
let birthYear: String
let gender: Gender
let homeworld: String
let films: [URL]
let species: [URL]
let vehicles: [URL]
let starships: [URL]
let created: Date
let edited: Date
let url: URL
}
enum Gender: String, Decodable {
case male
case female
case unknown = "n/a"
}
Note a couple of differences between the names in the struct and the names in the object that you are getting back. eg hair_color (snakecase) and hairColor (camelCase) In Swift it is common to write it the latter way and when we use decodable we can tell our decoder to use a custom key decoding strategy. Also note that I have used an enum for Gender. This isn't required and we could have just used a String. Also note that created and edited are Dates, however they are not iso8601 compliant but we can also specify a custom date decoding strategy.
Here is how we can decode the data that you have received.
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSSSS'Z'"
let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase
decoder.dateDecodingStrategy = .formatted(dateFormatter)
let people = try decoder.decode(People.self, from: data)
Now we can put this all together in your network request to get the following:
func onLoad() {
let url = URL(string: "https://swapi.dev/api/people")
guard let requestUrl = url else { fatalError() }
// Create URL Request
var request = URLRequest(url: requestUrl)
// Specify HTTP Method to use
request.httpMethod = "GET"
// Send HTTP Request
let task = URLSession.shared.dataTask(with: request) { (data, response, error) in
// Check if Error took place
if let error = error {
print("Error took place \(error)")
return
}
// Convert HTTP Response Data to a simple String
if let data = data {
do {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSSSS'Z'"
let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase
decoder.dateDecodingStrategy = .formatted(dateFormatter)
let people = try decoder.decode(People.self, from: data)
people.results.forEach { person in print(person) }
} catch {
print("Failed to load: \(error)")
}
}
}
task.resume()
}
Cast results as an Array of Dictionary. Here's how
if let data = data {
do {
if let json = try JSONSerialization.jsonObject(with: data) as? [String: Any],
let results = json["results"] as? [[String: Any]] {
for result in results {
print(result)
}
}
} catch {
print("Failed to load: \(error.localizedDescription)")
}
}
Better Approach: Use Codable, JSONSerialization feels bit outdated.
Related Links:
https://developer.apple.com/documentation/swift/codable
https://www.swiftbysundell.com/basics/codable/

Deserialization JSON swift 4.2

I try to deserialize my JSON by using Decodable protocol, also i use enum with CodingKey, but it doesn't work. I need only nested array (start with "indicator"), and only few fields (all of them in struct). I tried a lot of different options, but unfortunately..
P.S. Also i tried to do it without CodingKey. Anyway response was: "Swift.DecodingError.keyNotFound(CodingKeys(stringValue: "country", intValue: nil)" Ofc i read this, maybe array is a reason(i mean this strange intValue)?
JSON
[
{
"page":1,
"pages":2,
"per_page":50,
"total":59,
"sourceid":"2",
"lastupdated":"2019-03-21"
},
[
{
"indicator":{
"id":"IP.PAT.RESD",
"value":"Patent applications, residents"
},
"country":{
"id":"SS",
"value":"South Sudan"
},
"countryiso3code":"SSD",
"date":"2018",
"value":null,
"unit":"",
"obs_status":"",
"decimal":0
},
{
"indicator":{
"id":"IP.PAT.RESD",
"value":"Patent applications, residents"
},
"country":{
"id":"SS",
"value":"South Sudan"
},
"countryiso3code":"SSD",
"date":"2017",
"value":null,
"unit":"",
"obs_status":"",
"decimal":0
},
...
]
]
My code
struct CountryObject: Decodable{
var country: CountryInfo
var date: Int
var value: Int?
private enum RawValues: String, Decodable{
case date = "date"
case vallue = "value"
}
}
struct CountryInfo: Decodable{//Country names
var id: String?
var value: String?
private enum RawValues: String, Decodable{
case id = "id"
case value = "value"
}
}//
let urlString = "https://api.worldbank.org/v2/country/SS/indicator/IP.PAT.RESD?format=json"
guard let url = URL(string: urlString) else {return}
URLSession.shared.dataTask(with: url) {(data,response,error) in
guard let data = data else {return}
guard error == nil else {return}
do{
let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase
let countryObject = try! decoder.decode([CountryObject].self, from: data)
print(countryObject)
}catch let error{
print(error)
}
}.resume()
Create a root struct and decode the array with unkeyedContainer
struct Root : Decodable {
let info : Info
let countryObjects : [CountryObject]
init(from decoder: Decoder) throws {
var arrayContrainer = try decoder.unkeyedContainer()
info = try arrayContrainer.decode(Info.self)
countryObject = try arrayContrainer.decode([CountryObject].self)
}
}
struct Info : Decodable {
let page, pages, perPage: Int
let lastupdated: String
}
struct CountryObject : Decodable {
let country: CountryInfo
let date: String
let value: Int?
}
struct CountryInfo : Decodable { //Country names
let id: String
let value: String
}
...
let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase
do {
let root = try decoder.decode(Root.self, from: data)
let countryObjects = root.countryObjects
print(countryObjects)
} catch { print(error) }
(De)serializing the JSON twice is unnecessarily expensive.

Store values from a JSON file into variables

I've been trying for hours to read values from a JSON file in the app bundle and print those values. I've been scouring several forums and SO questions trying to piece together a way to read from a .json file, but have had no luck. What I currently have is a bunch of (probably useless) code that looks like this:
//json parsing
if let path = Bundle.main.path(forResource: "POIs", ofType: "json") {
do {
let data = try Data(contentsOf: URL(fileURLWithPath: path), options: .mappedIfSafe)
let jsonResult = try JSONSerialization.jsonObject(with: data, options: .mutableLeaves)
if let jsonResult = jsonResult as? Dictionary<String, AnyObject>, let pois = jsonResult["POI"] {
print("test ", pois)
for poi in pois as! [AnyObject] {
guard let name = poi["name"], //String
let coordinatesJSON = poi["coordinates"] as? [String: Double],
let latitudeVar = coordinatesJSON["lat"],
let longitudeVar = coordinatesJSON["lng"],
let altitude = poi["altitude"], //Int
let facebookurl = poi["facebookurl"], //String
let poiImage = poi["pinImage"] //String
else {
return
}
and my POIs.json file looks like this:
{
"POI":[
{
"name": "Boston Paintball",
"coordinates": {
"lat": 42.401848,
"lng": -71.023843
},
"altitude": 7,
"facebookurl": "https://www.facebook.com/PlayBostonPaintball/",
"pinImage": "bp"
},
{
"name": "Chilis",
"coordinates": {
"lat": 42.402314,
"lng": -71.021655
},
"altitude": 7,
"facebookurl": "https://www.facebook.com/chilis",
"pinImage": "chilis"
}
]
}
Can anyone show me the best way to read values from a json, store them into the variables I specified, and then print them? Thank you!
UPDATED CODE:
//json parsing
let file = Bundle.main.url(forResource: "POIs", withExtension: "json")
let data = try? Data(contentsOf: file!)
let root = try? JSONDecoder().decode(Root.self, from: data!)
do {
let root = try JSONDecoder().decode(Root.self, from: data!)
print(root)
} catch {
print(error)
}
let decoder = JSONDecoder()
let pois = try? decoder.decode([Root].self, from: data!)
let pinUIImage = UIImage(named: Location.pinImage)
But still giving me a Instance member 'pinImage' cannot be used on type 'ViewController.Location' error.
UPDATE 2:
for poi in (root?.pointsOfInterest)! {
if let location = root?.pointsOfInterest.??? { //I dont know how to grab the poi from the current iteration. Any tips?
You should structure your data to conform to Codable protocol:
struct Root: Codable {
let pointsOfInterest: [Location]
private enum CodingKeys: String, CodingKey {
case pointsOfInterest = "POI"
}
}
struct Location: Codable {
let name: String
let coordinates: Coordinate
let altitude: Int
let facebookurl: String
let pinImage: String
}
struct Coordinate: Codable {
let latitude: Double
let longitude: Double
private enum CodingKeys: String, CodingKey {
case latitude = "lat", longitude = "lng"
}
}
do {
let root = try JSONDecoder().decode(Root.self, from: jsonData)
print(root)
} catch {
print(error)
}
This will print
Root(pointsOfInterest: [__lldb_expr_39.Location(name: "Boston
Paintball", coordinates: __lldb_expr_39.Coordinate(latitude:
42.401848000000001, longitude: -71.023842999999999), altitude: 7, facebookurl: "https://www.facebook.com/PlayBostonPaintball/",
pinImage: "bp"), __lldb_expr_39.Location(name: "Chilis", coordinates:
__lldb_expr_39.Coordinate(latitude: 42.402313999999997, longitude: -71.021654999999996), altitude: 7, facebookurl: "https://www.facebook.com/chilis", pinImage: "chilis")])