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
1
vote
1 answer

issue with parsing dateUTC swift from spaceX api v5?

I have an issue with decoding dateUTC in swift. It shows nil every time I run the app. the API has a date, but the JSONDecoder is not working. I have posted an image JSON response and Service.swift JSON from spaceX API…
NinjaDeveloper
  • 1,620
  • 3
  • 19
  • 51
1
vote
1 answer

How to extract and access data from JSON with PHP when some data changes?

I am trying to bring in some API data to a directory in wordpress. The data I am trying to get is just crypto coin price, none of the other information but because its format is sort of nested (?) it doesnt seem to work. { "bitcoin": { "usd":…
Arctic
  • 13
  • 2
1
vote
1 answer

Can't seem to decode JSON

I know this type of question seems to be answered a lot but I really can't seem to make this work. I'm trying to decode some JSON data into my data structs. I think the problem is there. I may have my data model wrong, but can't quite work it out.…
1
vote
1 answer

How do I (simply) "do something else" to the resulting struct after the JSONDecoder().decode does it's job?

Very simply, I have this: public struct SomeAPI: Codable { public let a: String public let b: String public let c: String public let d: String } and I of course do this ... let s = try? JSONDecoder().decode(SomeAPI.self, from:…
Fattie
  • 27,874
  • 70
  • 431
  • 719
1
vote
1 answer

Dictionary with Coding keys in Swift, Codale to String

I have valid, working code, but I want to find out if there is a way to make it simpler and smaller. I have a custom class Response which can be initialised from Json or text (depends on response from server) public class Response: Codable { let…
busido
  • 13
  • 5
1
vote
1 answer

API Decoding Struct Swift

Hello I'm trying to decode this API, https://gorest.co.in/public-api/posts. What I'm doing wrong with structs? Thank you. struct Root: Decodable { let first: [Code] let second: [Meta] let third: [Post] } struct Code: Decodable { let code:…
georgekak
  • 73
  • 11
1
vote
1 answer

Decode JSON response from API

I get some JSON objects from my api that look like this: { "from": "1970-01-01", "until": null, "employeeId": "13", "project": { "id": "05c6adce-20cd-4ca3-9eff-8fd430f63a20", "version": 0, "name": "AGFA", …
Pjaks
  • 251
  • 1
  • 11
1
vote
0 answers

No value associated with key CodingKeys - JSONDecoder() Error

Here I have 3 files loginView(SwiftUI file) for UI purpose, LoginViewModel for handling the logic, ServiceManager for handling the Network call Below code is in loginView(SwiftUI file) Button("Login") { loginVM.loginWebserviceCall() } Below code…
Bhanuteja
  • 771
  • 2
  • 8
  • 18
1
vote
1 answer

Swift - How to change JSON Struct Element Values

I have converted JSON data file to a deviceParameters instance of Struct using JSONDocode and it works fine. However I am struggling to change the values of properties on the Struct programmatically afterwards. Any Suggestings? struct…
Jukka
  • 55
  • 3
1
vote
1 answer

Decode List[String] to List[JSONObject(key,value)] in circe scala

Given incoming json like below, how can i decode it the given case class based on condition. Incoming JSON { "config": { "files": ["welcome"], "channel": "media" } } Case Classes case class File(`type`: String, value:…
1
vote
3 answers

just one object nested in an array, how to decode the object?

I have this json { "status": [ { "state": "checked", "errorCode": "123", "userId": "123456" } ] } this is an array of statuses but is implemented badly because can be just one so I would…
AlexNica
  • 58
  • 8
1
vote
2 answers

No value associated with key CodingKeys(stringValue: \"data\", intValue: nil) (\"data\")

If you wanna test Postman.. You can test on Postman. I couldn't decode data. How can I decode ? Error: keyNotFound(CodingKeys(stringValue: "data", intValue: nil), Swift.DecodingError.Context(codingPath: [], debugDescription: "No value associated…
Ufuk Köşker
  • 1,288
  • 8
  • 29
1
vote
1 answer

Trouble Decoding JSON Data with Swift

Trying to get a little practice in decoding JSON data, and I am having a problem. I know the URL is valid, but for some reason my decoder keeps throwing an error. Below is my model struct, the JSON object I'm trying to decode, and my decoder. Model…
1
vote
0 answers

Swift floating point precision issue when decoding json value

When working with decimals encoding a value of e.g 68.32 does not result in the same value when decoding it afterwards. I'm using the suggestion from https://forums.swift.org/t/decimal-has-no-rounded/14200/12 to round the value before encoding…
ProtocolGuy
  • 144
  • 8
1
vote
2 answers

How to decode JSON Into different types?

I have this local JSON File, which contains title, start and end. I want to create dates from the start and end keys, but a String for the title, so I can create Event objects. Right now I have decoded everything into Strings. So I'm trying to…