API will sometimes return an empty dictionary in JSON Data instead of the usual float: how do I handle it? (Swift)

BlakePat

I am using an API to retrieve JSON Data and on occasion the API will give me an empty dictionary when it usually gives me a float or nil If the float is 0.

It seems it is doing this when the value should be nil

How do I handle this? Is there a way so if I am given this empty dictionary value I can just ignore it or return nil or even 0?

Thank you for any and all help!

New Dev

If you can't change the API (preferred approach), then you can use an enum with associated values representing each type of result.

So, for the sake of example, let's assume that you get the following two types of results:

Success case:

{ "value": 5.2 }

Failure case:

{ "value": {} }

Then you can decode this with an enum, like so:

enum Result: Decodable {
   case success(Float)
   case error
    
   enum CodingKeys: CodingKey { case value }
    
   init(from decoder: Decoder) throws {
      let container = try decoder.container(keyedBy: CodingKeys.self)
        
      if let value = try? container.decodeIfPresent(Float.self, forKey: .value) {
         self = .success(value)
      } else {
         self = .error
      }
   }
}

You can then decode and extract the number:

let result = try JSONDecoder().decode(Result.self, from: json)

if case let .success(value) = result {
    print(value) // 5.2
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I return the output in dictionary or JSON format in FAST API?

How to deserialize json which is sometimes a dictionary and sometimes an empty array?

How do i return data where that contain empty string instead of NULL in laravel

How do I handle a Flat JSON Dictionary with Retrofit

How do I get ASP.NET Web API to return JSON instead of XML using Chrome?

How do I make JSON data appear on a Label instead of in the console? (Swift)

How to handle JSON data in Swift

How do I return these JSON objects to my browser instead of the console

How do I access a specific value in this dictionary of JSON using Swift?

How do I fix a list (sometimes incorrect) using a dictionary?

How can I bundle my JSON API data into one dictionary?

How do I change Serde's default implementation to return an empty object instead of null?

How do I return an empty JSON object for methods of return type void?

How to handle exception in empty return data with IEnumerable<decimal> C#

How to check if json data return is empty with jquery

How do I return JSON-data from python to javascript?

[del]eting an item from a JSON dictionary leaves an empty dict: `{}`. How do I remove it completely?

How to handle JSON is null or empty with creating extension string swift?

How can I parse json with a dictionary in swift?

How do I get a plist as a Dictionary in Swift?

How do I convert swift Dictionary to NSDictionary

Swift: How to return a dictionary in swift

How to handle if API response is a string instead of JSON in Ionic + Angular

How do I handle newlines in JSON?

How to handle empty objects with Swift?

How do I handle a KeyError exception in python without exiting the dictionary?

How can I return an array of elements, instead of a single one, in a Rails JSON API?

How do I handle this data type

How do I get php `json_encode` to return a Javascript array instead of a Javascript object?