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
3
votes
2 answers

Swift - Decode array of an arrays JSON data

I'm using Swift 5 and I'm trying to create a struct to hold the contents of an Google Sheets API Call. I'm stuck with "values" key which values i want to fetch, change to Int type and store at separate array variable which i can use lately. Here's…
SimbaNew
  • 33
  • 4
3
votes
2 answers

Swift Generic Json Parser

I have this generic JSON Parser that works decoding Arrays class JSONParserFromStruct { typealias result = (Result<[T], Error>) -> Void func downloadList(of _: T.Type, from data: Data, …
Dan
  • 151
  • 3
  • 12
3
votes
1 answer

Swift 4 JSONDecoder Utility Function for Arbitrary Struct

I'm trying to create a utility function which given some Data and a struct which conforms to Decodable can decode the JSON data into the struct as follows: func decodeDataToModel(data : Data?, model : Decodable) -> Decodable? { guard let data =…
Alk
  • 5,215
  • 8
  • 47
  • 116
3
votes
1 answer

iOS - JSONSerialization VS JSONDecoder and usage

So i am getting the follow json : { "output": [ "{\"cameraIsOnboarded\" : true}" ], "exit_code": 0 } i tried to decode it with the model below: struct CameraOnboard: Codable { var output: [Output] //var exit_code: Int? } struct Output:…
ironRoei
  • 2,049
  • 24
  • 45
3
votes
1 answer

How to use JSONDecoder to decode JSON with unknown type?

Here is my scenario: I have a swift WebSocket server and a Javascript client. Over this same WebSocket I will be sending various objects that correspond to different Codable types. It is simple enough to decode if the correct type is known. The…
Joshua Goossen
  • 1,714
  • 1
  • 17
  • 36
3
votes
2 answers

Instance member 'decode' cannot be used on type 'JSONDecoder'; did you mean to use a value of this type instead

I implementing JSONDecoder on to get the JSON data from Wordpress json my struct is in the other swift file i get this error here's my code. on let article I get the error URLSession.shared.dataTask(with: url!){ (data,response ,err) in guard…
Guren
  • 182
  • 2
  • 14
3
votes
0 answers

'Any?' does not conform to 'Decodable'

I have DataModel data type: class DataModel: Decodable { var data : Any? var name : String? } server json: { "data":"success", "name":"john" } or { "data": { "result": { "shopcode": 0, "shopname":…
Karim
  • 322
  • 4
  • 20
3
votes
1 answer

How to handle JSON format inconsistencies with Swift 4 Codable?

I need to parse JSON of which one of the field value is either an array: "list" : [ { "value" : 1 } ] or an empty string, in case there's no data: "list" : "" Not nice, but I can't change the format. I'm looking at converting my…
meaning-matters
  • 21,929
  • 10
  • 82
  • 142
3
votes
1 answer

How to decode custom JSON values using JSONDecoder

Backend returns custom JSON value for location. As shown in example: { "location": (54.000000, 21.000000) } For parsing JSON I am using this code: let json = """ { "location": (54.000000, 21.000000) } """ struct Location: Codable { …
Ramis
  • 13,985
  • 7
  • 81
  • 100
2
votes
0 answers

Memory leak using swift JSONEncoder. serialized data is still left in app's memory after logout [iOS]

I run memory dump on my app's memory and find out that it still holds some sensitive data that was not supposed to stay there after logout. (note: I have generated the memory dump while in debug mode). after some investigation I have found out…
vigdora
  • 319
  • 4
  • 11
2
votes
1 answer

helm : error converting YAML to JSON: yaml: line xx: did not find expected key

I have a json encoded string in my values.yaml file -> values.yaml network: cidrs : "[\"123.123.123.123/32\",\"123.124.125.125/32\"]" Now , I want to use this value as a list of string in my network policy egress ipblock. But I'm not able to…
devcodes
  • 1,038
  • 19
  • 38
2
votes
2 answers

How do I handle errors within a struct when parsing JSON in Swift

I am calling an API and then decoding it with the simplified code below guard let url = URL(string: "someURL") else { return } let task = URLSession.shared.dataTask(with: url) { data, response, error in let decoder = JSONDecoder() if…
James Taga
  • 65
  • 4
2
votes
3 answers

Swift: hook into Decodable.init() for an unspecified key?

I have some JSON I would like to decode with a JSONDecoder. Trouble is, the name of one of the properties is helpfully dynamic when sent from the server. Like this: { "someRandomName": [ [1,2,3], [4,5,6] ], "staticName": 12345 } How can I…
serlingpa
  • 12,024
  • 24
  • 80
  • 130
2
votes
1 answer

How to create ViewModel from local json with swift higher order function in swift iOS

I've a local json and created data model which can parse and get data, I wanted to create a view model from data model to use in the view controller can any suggest how to to with swift higher order functions. JSON: { "segment": { …
iOSDuDe
  • 35
  • 5
2
votes
1 answer

Incompatible types while writing JSON to BigQuery table

I have a function that recieves JSON data from API server and upload it to BigQuery table with specified schema. JSON data has some fields that should be STRING but contain only digits. Here is a sample of json…
1 2
3
35 36