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

Use JSONDecoder in swift 4

I am using swift 4 xcode 9.2, I got the below error when using JSONDecoder. typeMismatch(Swift.Array, Swift.DecodingError.Context(codingPath: [], debugDescription: "Expected to decode Array but found a dictionary instead.", …
lpy
  • 25
  • 5
1
vote
1 answer

Extend JSONDecoder to allow for models to be serialized differently based on endpoint

I have many models that, depending on the endpoint, or serialized differently. My first attempt had a init(from decoder: Decoder) riddled with nested try catch blocks. I thought a better solution would be to extend JSONDecoder so that when I…
Gabe Spound
  • 568
  • 5
  • 28
1
vote
0 answers

json merge in multidimensional array decode not working

My json looks like this [ { "name":"test.xlsx", "type":"application\/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "tmp_name":"D:\\xampp\\tmp\\phpEFF8.tmp", "error":0, "size":3278935 }, { …
Lemon Kazi
  • 3,308
  • 2
  • 37
  • 67
1
vote
3 answers

SwiftUI: Display JSON Data with Text() instead of List

I have got a problem with displaying JSON data on a SwiftUI View. I tried several tutorials and read articles which are related to my problem, but nothing seems appropriate enough. For example everyone displays JSON data from an API with a fancy…
braendnew
  • 81
  • 1
  • 5
1
vote
4 answers

Swift Decodable, Endpoint returns completely different types

With API I'm working with, I have a case where 1 API Endpoint can return completely different responses, based on if the call was successful or not. In case of success, API Endpoint returns an Array of requested objects, in the root, something like…
jovanjovanovic
  • 4,158
  • 3
  • 22
  • 32
1
vote
1 answer

Swift Codable with Generics, holding common data from responses

I will try to explain what I want to do in a best possible way, just like I tried while Googling for the last couple of days. My application is communicating with few different APIs, but let's consider responses from one API first. Response from…
jovanjovanovic
  • 4,158
  • 3
  • 22
  • 32
1
vote
1 answer

JSONDecoder fails to find key that is present

When attempting to parse the response from the Create Team call in Github's REST api, JSONDecoder fails when parsing many of the snake cased keys for a Repository. When decoding through JSONSerialization, it is able to find all keys without…
lhowell
  • 101
  • 5
1
vote
1 answer

Python request returning a JSON Decoder Error

I'm trying to pull data from a website. I'm using Python request: users = requests.get('website name here', headers=headers).json() I'm getting this error: raise JSONDecodeError("Expecting value", s, err.value) from…
Jason000
  • 179
  • 2
  • 5
  • 14
1
vote
1 answer

Parse JSON with Decodable return empty Model

I'm trying to load local JSON file and parse using model which conforms to Decodable protocol. JSON file: [ { "body": {}, "header": { "returnCode": "200", "returnMessage": "Successfully Received", } } ] Response Message…
Harshal Wani
  • 2,249
  • 2
  • 26
  • 41
1
vote
1 answer

Swift 5 JSONDecoder decode JSON with field name having a space eg "post title": "Hello World"

I'm currently using JSONDecoder to parse json into objects with Swift 5. I'm just realizing now that part of the JSON is bad JSON. It has a field name with a space: "Post Title". I'm not sure why it is like this, and I'm aware it is bad practice for…
voo_doo_juju
  • 61
  • 1
  • 8
1
vote
1 answer

Swift4 Decodable - decode half the keys as Dictionary

I have the situation where the server sends me a model where i know of types and names of some keys, and don't about the others. However, the user can edit those other key value pairs of their own volition. Example: { "a": "B", "b": 42, …
zaitsman
  • 8,984
  • 6
  • 47
  • 79
1
vote
0 answers

"Expected to decode Dictionary but found a string/data instead."

I am trying to decode data from Firestore to json format but got that error. This is model class class UserModel : Codable{ var email: String? var name: String? var roles: [Roles]? init(email: String, name: String, roles: [Roles]) { …
Visal Sambo
  • 1,270
  • 1
  • 19
  • 33
1
vote
1 answer

Unable to get data from JSONDecoder ,that's right, I made up the structure?

I have such a structure, I took a piece to get the data. Here's my code.Why can't I get data from the Api?Can anyone help and example write in the answer.It seems like all are correct. I will be very grateful. [{ "id": 142636, "apiId":…
1
vote
1 answer

Two JSON data merging and ECHO with PHP

What is the difference from the previous questions? In other questions, the numbers of both JSON data were equal. This question does not equal JSON files. I have two JSON data. One of them contains only 2 values. The other one contains six…
Editor
  • 622
  • 1
  • 11
  • 24
1
vote
0 answers

Trying to use JSONDecoder() but am not able to parse JSON

I am trying to parse some JSON data from newsapi.org. I have a model class with the JSONDecoder() The model I am trying to parse the JSON data into is: struct ArticleService: Decodable { var totalResults:Int? var…
Johan Park
  • 17
  • 4