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
2
votes
1 answer

Custom intialiser on Primitive types for JSONDecoder

How do I customise the behaviour of JSONDecoder for primitive types like Int, Bool? Here is the problem: Backend cannot be relied upon for types. Eg: Bool can come as true/false or "true"/"false"(bool can come wrapped in double quotes) We have at…
Kaunteya
  • 3,107
  • 1
  • 35
  • 66
2
votes
1 answer

Swift Codable: Use a parent's key as as value

I have a JSON that has ID's in the root level: { "12345": { "name": "Pim" }, "54321": { "name": "Dorien" } } My goal is to use Codable to create an array of User objects that have both name and ID properties. struct…
Pim
  • 2,128
  • 18
  • 30
2
votes
1 answer

How to maintain object state after JSON Encode/Decode in PHP

The below method have changed property instance in object into array but the requirement have different, the result of response will be same required after manipulation. 200, …
Query Master
  • 6,989
  • 5
  • 35
  • 58
2
votes
0 answers

use CodingKeys to access the a value of a variable of a struct

I am creating a custom JSON decoding method for my struct and I need to loop over all its parameters. I thought about using CodingKeys but I dont know how to achieve it ( or even if it is feasible ). Here is the code I am trying to reduce : struct…
Olympiloutre
  • 2,268
  • 3
  • 28
  • 38
2
votes
1 answer

Swift: Cannot invoke 'decode' with an argument list of type '([Idea], from: Data)'

I've been trying to figure out the best solution for data persistence for an app I'm working on and I decided a locally stored JSON file will be the best balance of simplicity and functionality. What I need to save is an array of custom structs, and…
CristianMoisei
  • 2,071
  • 2
  • 22
  • 28
2
votes
1 answer

How would I decode a NSDecimalNumber without loss of precision?

is there any way to tell JSONDecoder to convert incoming decimals to Strings? public struct Transaction: Decodable { public let total: NSDecimalNumber? enum CodingKeys: String, CodingKey { case total = "AMOUNT" } public…
Anton Tropashko
  • 5,486
  • 5
  • 41
  • 66
2
votes
2 answers

decode a Codable struct from API call and a mockup file simultaneously

I am currently working on a project where I am calling a webservice that returns me a JSON that I parse using Codable, like so : My Struct: struct User: Codable { var name: String var age: Int } API response : { "name": "Romiro", "age":…
Olympiloutre
  • 2,268
  • 3
  • 28
  • 38
2
votes
2 answers

Decoding numerical snake_case keys with JSONDecoder

I have the following JSON object which is to be converted to an object using JSONDecoder: { "first_key": 3, "image_1000x1000": "location" } This maps to the following Swift model: struct Response: Decodable { var firstKey: Int var…
Jack Greenhill
  • 10,240
  • 12
  • 38
  • 70
2
votes
2 answers

Parse JSON with JSONDecoder in Swift 5 using structs

I get from a Rest API a result in json format. Now I wanna parse this JSON with the JSONDecoder but I don't really understand the structure of my response. For that I already tried to create structs to get the "name" of "FirstUser". { "User":[ …
Testik
  • 59
  • 1
  • 7
2
votes
1 answer

How to overcome delay in navigation to other viewcontroller iOS swift?

I have recently stopped using Alamofire for making HTTP Networking calls as now in swift 4 Apple have introduced Codable-protocol which is very powerful and fast compared to Alamofire. Everything works fine as expected even my response in faster…
2
votes
1 answer

How can I use JSONDecoder / Codable with this date format?

I get the following date back from an API. { "createdDate": "2019-03-22T15:53:06.663Z" } I'd like to decode this and store it as a Date type. My JSONDecoder is not able to decode this however. I have tried to extend it with extension…
Tim J
  • 1,211
  • 1
  • 14
  • 31
2
votes
2 answers

Swift : The data couldn’t be read because it isn’t in the correct format

I try to call the POST Api with Alamofire but it's showing me an error of incorrect format. This is my JSON response: [ { "_source": { "nome": "LOTERIAS BELEM", "endereco": "R DO COMERCIO, 279", "uf": "AL", "cidade":…
Krunal Nagvadia
  • 1,083
  • 2
  • 12
  • 33
2
votes
1 answer

Invalid JSON while parsing \n (quoted) string

I have a problem with a Codable parsing... that's my example code: class Test: Codable { let resultCount: Int? let quote: String? } var json = """ { "resultCount" : 42, "quote" : "My real quote" } """.data(using: .utf8)! var…
DungeonDev
  • 1,176
  • 1
  • 12
  • 16
2
votes
4 answers

Decoding Nested JSON with Swift 4 only returns one object

I'm trying to decode JSON that I receive from a network request, but it is only decoding the first object. The JSON looks like this { "lastUpdatedOn": "2018-08-21T14:38:38.251Z", "players": [ { "player": { "id": 10138, …
user10377655
2
votes
0 answers

searchBar in Swift

program code how to add a search for the data of tableview struct jsonstruct:Decodable { var name:String var Id : Int = 0 let MobileNumber:String let gender:String enum CodingKeys: String, CodingKey { case name = "name" case Id = "Id" …
Sruthi C S
  • 131
  • 6