Questions tagged [jsondecoder]

This tag should be used for `JSONDecoder` (introduced in Swift 4) questions for decoding JSON decoder on Apple platforms.

JSONDecoder is a class, introduced in Swift 4, for decoding JSON on Apple operating systems that conform to Decodable protocol (or Codable protocol, which is type alias for Encodable & Codable). This offers a simple mechanism to easily parse JSON from Decodable Swift types. This applies to standard Swift collections, such as Array and Dictionary, but custom types can conform to Decodable to participate in this simplified JSON decoding process.

This replaces/supplements the JSONSerialiation class used in prior Swift versions.

See also:

528 questions
5
votes
1 answer

Swift: ERROR: keyNotFound(CodingKeys(stringValue: "adult", intValue: nil)

I do see a lot of similar questions in stack-overflow, but seems no one is similar with my case. I'm new to Combine frame work, and it took me this whole afternoon to figure out what is wrong, however still stuck at here... Xcode gives me below…
Zhou Haibo
  • 1,681
  • 1
  • 12
  • 32
5
votes
4 answers

swift parse json - The data couldn’t be read because it isn’t in the correct format

Here is struct codable code: import Foundation public struct TaskID: Codable { let embedded: Embedded } public struct Embedded: Codable { let task: [Task] } public struct Task : Codable { let embedded: EmbeddedVariable let id :…
PvUIDev
  • 95
  • 1
  • 9
  • 38
5
votes
2 answers

Using Generics / Codable w/ API response 204 NO CONTENT

I am using generics and codable with URLSession. When I receive a response from an API, I check the status is in the 200 - 299 range and decode the data like so guard let data = data, let value = try? JSONDecoder().decode(T.self, from: data)…
Tim J
  • 1,211
  • 1
  • 14
  • 31
5
votes
2 answers

How do I decode JSON in Swift when it's an array and the first item is a different type than the rest?

Say the JSON looks like this: [ { "name": "Spot", "breed": "dalmation" }, { "color": "green", "eats": "lettuce" }, { "color": "brown", "eats": "spinach" }, { …
Doug Smith
  • 29,668
  • 57
  • 204
  • 388
4
votes
2 answers

Date string does not match format expected by formatter

Was only able to reproduce this issue on a friend's device. The device is from Germany and is set to the German region in Settings. I cannot reproduce on any Canadian devices. Why is it failing when trying to create a Date property from the…
SwiftCODA
  • 172
  • 2
  • 16
4
votes
1 answer

Swift - Memory Leak in JSONDecoder

I discovered some memory leaks in my iOS app when it decodes server responses. These memory leaks don't happen all the time. I am using the following code to decode the response. The codes are inside a generic struct. T.self is also a struct not a…
thus
  • 1,326
  • 1
  • 14
  • 23
4
votes
0 answers

Swift - crash EXC_BAD_ACCESS JSONDecoder with large JSON

I'm stuck on decoding a JSON in swift. I've got the following code in a playground with a JSON that has 10 fields. When i try to decode the data I get the following Error error: Execution was interrupted, reason: EXC_BAD_ACCESS…
4
votes
1 answer

General strategy to decode type mismatch keys in JSON into nil when optional in Swift

Here is my problem, when I receive some JSON, it happens that some values do not match the required type. I don't really mind, I'm only interested by the value when its type is correct. For instance, the following structure: struct Foo : Decodable…
Zaphod
  • 6,758
  • 3
  • 40
  • 60
4
votes
4 answers

Why do I get ‘Cannot get keyed decoding container -- found null value instead.’ error when I use JSONDecoder

(unknown context at $10741e078).CodingKeys>, Swift.DecodingError.Context(codingPath: [], debugDescription: "Cannot get keyed decoding container -- found null value instead.", underlyingError: nil) my model is , struct Details : Decodable { var…
Faheem Rahman
  • 343
  • 2
  • 10
4
votes
3 answers

How should I decode a json object using JSONDecoder if I am unsure of the keys

I have an api response in the following shape - { "textEntries":{ "summary":{ "id":"101e9136-efd9-469e-9848-132023d51fb1", "text":"some text", "locale":"en_GB" }, "body":{ …
Teddy K
  • 820
  • 1
  • 6
  • 17
4
votes
1 answer

Reusing JSONDecoder or recreating it on every request?

In an asynchronous environment is it more efficient to use one JSONDecoder for all request (Might result in delays if multiple threads are waiting for the lock) or it's more efficient to create a new JSONDecoder for every new request? Apple…
l.vasilev
  • 926
  • 2
  • 10
  • 23
4
votes
1 answer

JSONDecoder always returns "No value associated with key CodingKeys"

I am using following decoding struct to decode the data from server but it always returns "No value associated with key CodingKeys". Please see the code below struct UserDecode: Codable { var user: User? var result: String? private enum…
Balaji Kondalrayal
  • 1,743
  • 3
  • 19
  • 38
4
votes
2 answers

How to decode a named array of json objects in Swift

I have a named array of json objects which I receive via an API call. { "Images": [{ "Width": 800, "Height": 590, "Url": "https://obfuscated.image.url/image1.jpg" }, { "Width": 800, "Height": 533, …
Damo
  • 12,840
  • 3
  • 51
  • 62
4
votes
1 answer

Adopting CustomNSError in DecodingError

I'm writing an error logger using Crashlytics and I've come up against an issue that is making me question my understanding of protocols and dynamic dispatch. When recording non fatal errors using Crashlytics the API expects an Error conforming…
mike
  • 2,073
  • 4
  • 20
  • 31
3
votes
3 answers

How to correctly parse JSON with root element as an array in Swift?

I've the following issue, where I don't get the structure right to decode a response from node-red for the status of my sonos…
1
2
3
35 36