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

Why can't I decode my JSON file in Swift?

I broke my original JSON file up into two different JSON files, but now I can't decode the second one. I've run it through multiple validators and it seems to be valid JSON, and I tried making certain properties optional, but to no avail. Here's…
Mira
  • 131
  • 1
  • 9
1
vote
1 answer

Consuming JSON API using PHP

I have read and tried some code that I found in other posts with similar issues but I couldn't find a solution. I'm using this API: https://api.ipma.pt/open-data/forecast/meteorology/cities/daily/1100900.json which returns this: { "owner":…
devnull
  • 13
  • 4
1
vote
4 answers

How to implement custom decoder, in a case of array JSON structure, with swift Decodable?

If an array in JSON is at root level, then code is simple and beautiful: JSONDecoder().decode([T].self, data) But how does it work under the hood? I want to know this in order to implement a custom decoder (with the same calling style) in a case…
Roman
  • 1,309
  • 14
  • 23
1
vote
2 answers

JSONDecoder() deal with Null values only in Swift

Here is the JSON response I have. Struct: struct Welcome: Codable { let name: String let id: Int } JSON: { "name": "Apple", "id": 23 } This is the structure of JSON but the name will be null sometimes. So, I want to replace with…
Mount Ain
  • 316
  • 3
  • 15
1
vote
1 answer

JSONDecoder can't decode Array but can decode String

I have a struct that I would like to parse from Json: struct Subsidiary: Decodable { let id: Int? let subsidiary_ref: String? let name: String? let address: String? } I try to parse it like: let sub: [Subsidiary] = try!…
Marko Zadravec
  • 8,298
  • 10
  • 55
  • 97
1
vote
1 answer

How do I decode from openFoodFacts JSON(Swift)

I'm fairly new to decoding from a JSON file. My question is, how do I decode to find this: "product_name_en": "Coca Cola"(there's a lot more to that file, but not the node) My struct is set up as: struct ParseStructs : Decodable { var…
JohnM
  • 73
  • 7
1
vote
1 answer

why JSONDecoder gives typeMismatch for valid json

I am using JSONDecoder to convert JSON into Objects. Here is the code. It usually works fine. do{ print("Hi") if Data != nil{ let model = try JSONDecoder().decode(Base.self,…
Naveen
  • 77
  • 6
1
vote
1 answer

Swift JSON decoder with different values

I want to decode a dictionary with different values. So while the key will always be of type String, the value will have the same superclass (like Shape) but might be composed out of different subclasses (like Rectangle, Circle). I want to be able…
CodingT
  • 13
  • 2
1
vote
1 answer

JSONDecodeError: Expecting value: line 1 column 1 (char 0) for Keras+Rest API Application

I am trying to return an HTTP response from my keras model. @app.route("/predict", methods=["POST"]) def predict(): # initialize the data dictionary that will be returned from the # view data = {"success": False} # ensure an image…
1
vote
3 answers

Decode JSON with empty object using JSONDecoder

I want to decode JSON using JSONDecoder. It is working as expected, but for the JSON where inner object is empty JSONDecoder throws an error The data couldn’t be read because it is missing. Sample JSON on Error: { "header": { "code":…
Dawood Mujib
  • 337
  • 4
  • 14
1
vote
1 answer

typeMismatch in Decoding model again JSONEncoder and JSONDecoder swift 5

I am trying to update values in model and using JSON operation decode and encode. I have created model according to data. Everything is fine working but if I decode again my model it gives me typeMismatch error. I have tried but no success. Any one…
user12435629
1
vote
1 answer

How do you properly clear a json_encode error state?

I am using a PHP 5.6.40 development environment. Many of my classes implement JsonSerializable. The jsonSerialize method of one of these classes uses json_decode, since the value of one of its data members may or may not be encoded as JSON. The…
hutch90
  • 341
  • 3
  • 15
1
vote
1 answer

Accessing nested object from JSON when it is a dynamic number represented as string

I'm accessing the data from an API with XCode(10.2.1) and Swift(5.0) and ran into a problem I cannot seem to find the answer to. I am able to get data from all other parts of the API apart from one, which has been named as a number string "750", im…
Kriss
  • 13
  • 2
1
vote
0 answers

Is it possible to read a JSON sub-object directly as a JSON DOM object or Data or some other generic format?

Is there a JSON DOM that can read any JSON, regardless of type, and store it into something like a Dictionary hierarchy? Consider this JSON and code... var jsonData = """ { "type":"ListWrapper", "objectData": { …
Mark A. Donohoe
  • 28,442
  • 25
  • 137
  • 286
1
vote
1 answer

Fields in custom data model are "nil" after decoding API response with JSONDecoder().decode in Swift 5

I've defined a custom data model for a User Object in Swift like so: user data model I've got a function that pulls User data from an API like so: get data from api Here's the response when calling the same endpoint with Postman: api response And…
Bernhard Engl
  • 243
  • 1
  • 2
  • 12