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

Passing data to new ViewController through Segue

I am trying to pass a string of data through a segue. The JSON data has been parsed using JSONDecoder and appears properly in the console. Everything is working fine except for when I try to pass the data to a details ViewController I either receive…
JSharpp
  • 459
  • 1
  • 9
  • 21
2
votes
1 answer

JSONDecodeError when using Pandas Datareader

Following this tutorial, I get the error in the screenshot below when I run the command: df = web.DataReader("TSLA", 'morningstar', start, end) This is the error I get. What to do?
Neil Dey
  • 449
  • 2
  • 7
  • 18
2
votes
1 answer

Decodable JSONDecoder handle different coding keys for the same value

I'm using Swift decodable protocol to parse my JSON response: { "ScanCode":"4122001131", "Name":"PINK", "attributes":{ "type":"Product", "url":"" }, "ScanId":"0000000kfbdMA" } I'm running…
Missa
  • 145
  • 1
  • 9
2
votes
1 answer

Migrating from PHP 7.1.x to PHP 7.2.x json_decode() change

The official doc says: The json_decode() function option, JSON_OBJECT_AS_ARRAY, is now used if the second parameter (assoc) is NULL. Previously, JSON_OBJECT_AS_ARRAY was always ignored. This code (AFAIK) accomplishes this change and condition…
voskys
  • 121
  • 1
  • 9
2
votes
1 answer

How to decode nested JSON array and object with JSONDecoder in swift 4.1 and xcode 9.3?

I am new to iOS and I am having trouble decoding a nested JSON file which has many nested arrays and objects. I am using JSONDecoder to decode, but somewhere I am making a mistake or not decoding correctly. my decoder is let myOrder = try…
Neck
  • 611
  • 1
  • 7
  • 21
2
votes
1 answer

Is it possible to decode additional parameters using JSONDecoder?

We have some response returned by backend: { "name": "Some name", "number": 42, ............ "param0": value0, "param1": value1, "param2": value2 } Model structure for response: struct Model: Codable { let name: String …
iOS User
  • 101
  • 1
  • 8
2
votes
3 answers

JSONDecoder using Protocol

I'm using a protocol to create several structs which I use to decode using JSONDecoder. Here's a code sample of what I'm trying to achieve. protocol Animal: Codable { var name: String { get } var age: Int { get } } struct Dog: Animal { …
user9041624
  • 246
  • 4
  • 14
2
votes
1 answer

Swift 4 JSONDecoder optional variable

I have a Codable struct myObj: public struct VIO: Codable { let id:Int?; ... var par1:Bool = false; //default to avoid error in parsing var par2:Bool = false; } When I do receive JSON, I don't have par1 and par2 since these…
AlexP
  • 449
  • 2
  • 9
  • 25
2
votes
1 answer

Decode lowercase and uppercase JSON keys in Swift 4

I have the following struct that represents a JSON: struct Todo: Codable { let ID: Int? let LAST_DT_ADD: String? let LAST_ID:Int? } And when I use decode the same way: let decoder = JSONDecoder() do { let todo = try…
Dmitry
  • 125
  • 2
  • 9
2
votes
2 answers

How to decode variable from json when key is changing according to user input?

I am trying to parse some JSON response coming from CoinmarketCap using the JSONDecoder() in Swift 4. But the problem is that the response from json is changing according to user input. e.g if user wants the price in eur, the output is following: [ …
Tarvo Mäesepp
  • 4,477
  • 3
  • 44
  • 92
1
vote
1 answer

Can't Covert This Json To Normal Json PHP

{"response":"{\"reqEntityData\":{\"txnid\":\"YSCPAN99458555\",\"reqTs\":\"2023-08-19T16:54:34.118Z\",\"entityId\":\"SoftmintUAT\",\"dscProvider\":\"e-Mudhra Sub CA Class 2 for Document Signer…
1
vote
2 answers

JSONDecoder and escaped newlines - when to process these?

If you JSON-decode material containing a value that contains a backslashed "n" to indicate a newline, at what point should you replace it with a true newline? Here's an artificial example: let dict = ["key": "value\\nvalue"] let json = try!…
matt
  • 515,959
  • 87
  • 875
  • 1,141
1
vote
1 answer

How to use jsondecode for to decode public aws ssh keys from vault

I am trying to see how to go about retrieving some aws public ssh keys from vault using terraform. Vault is returning a string instead of an array of strings, so I looks like it would have to treat the whole response as a json object. How would I…
1
vote
2 answers

Swift not converting specific values to a float

My code is not working to convert 1.1 to a float. It works to convert 1.0, 1.25, and 1.5 to floats but doesn't convert 1.1. I do not understand why this would happen as they are all decimals with very few digits of precision required. This is when…
1
vote
2 answers

Swift json decoding fails when data is empty in function with generic types

I have written a function with generic type result. private func fetch(_ endpoint: Types.EndPoint, method: HTTPMethod = .get, body: Request? =…
Ryan Aluvihare
  • 225
  • 1
  • 2
  • 9