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

Decoding a JSON without keys in Swift 4

I'm using an API that returns this pretty horrible JSON: [ "A string", [ "A string", "A string", "A string", "A string", … ] ] I'm trying to decode the nested array using JSONDecoder, but it doesn't have a single key and I…
davidg
  • 651
  • 1
  • 7
  • 18
20
votes
1 answer

The `convertFromSnakeCase` strategy doesn't work with custom `CodingKeys` in Swift

I try to use Swift 4.1's new feature to convert snake-case to camelCase during JSON decoding. Here is the example: struct StudentInfo: Decodable { internal let studentID: String internal let name: String internal let testScore: String …
Howard
  • 268
  • 2
  • 7
19
votes
2 answers

Update responseJSON to responseDecodable in Swift

I'm new to Swift and I'm trying to upgrade some old Swift code. I'm getting the below warning: 'responseJSON(queue:dataPreprocessor:emptyResponseCodes:emptyRequestMethods:options:completionHandler:)' is deprecated: responseJSON deprecated and will…
thecoolmacdude
  • 2,036
  • 1
  • 23
  • 38
12
votes
1 answer

Python google-trans-new translate raises error: JSONDecodeError: Extra data:

While working on Google translate API, I found out some times google can't translate anything, while it keeps raising the same exception: Extra data. I have searched on the internet, I found a theory saying I have been blocked by Google translate…
Xia
  • 309
  • 1
  • 3
  • 9
9
votes
2 answers

Flask: orjson instead of json module for decoding

I'm using flask and have a lot of requests. The json module, which is used by flask, is quite slow. I automatically can use simplejson, but thats a bit slower, not faster. According to the documentation I can define a decoder (flask.json_decoder),…
Stefan Berger
  • 133
  • 1
  • 10
9
votes
2 answers

JSONDecodeError using Google Translate API with Python3

I've searched thoroughly on Stack Overflow but couldn't find an answer to this problem. I'm trying to use the Google Translate API (googletrans 2.2.0) for Python (3.6.2) and am trying to translate a set of non-English documents into English. I am…
8
votes
1 answer

valueNotFound error while parsing Json response in IOS

I am trying to parse a response using the JSONDecoder. If there is value for the corresponding key then it goes well, but if there is null value for a key then it fails to compile with the following error. valueNotFound(Swift.String,…
Anik Dey
  • 616
  • 8
  • 17
8
votes
5 answers

How to make the RealmSwift RealmOptional compatible with Swift Codable?

Im facing an issue where I can't make the RealmOptional compatible with swift new Codable feature with json decoder. Cosider the following Realm object. class School: Object, Codable { @objc dynamic var id: Int64 = 0 @objc dynamic var…
Shreesha Kedlaya
  • 340
  • 4
  • 19
7
votes
1 answer

How to preserve key order in a Django JSONField

I am having a hard time preserving the order of keys in a JSON object stored in a Django JSONField. I have tried using a custom encoder and decoder as per the docs, but the JSON object keeps re-ordeing itself: >>>from models import…
trubliphone
  • 4,132
  • 3
  • 42
  • 66
6
votes
1 answer

Swift JSONDecoder doesn't handle optional Bool as expected

In the following program, I have defined a struct with Bool, String and Int, both non-optional and option versions. When I run it, the optional String and Int decodes the json data as expected, but b2 never parses the value from the json, and is…
Peter Liaw
  • 961
  • 1
  • 7
  • 6
6
votes
1 answer

UIImage not equivalent when encoding/decoding

I've been doing some tests on my models to make sure they are equal when I encode them into JSON and then decode them back using JSONEncoder/Decoder. However, one of my tests failed, and the culprit was UIImage. I've made sure that no errors were…
PrayForTech
  • 353
  • 2
  • 10
6
votes
1 answer

Swift JSONDecoder can't decode valid JSON with escape characters

In playgrounds, the following code produces an error: import Foundation struct Model: Codable { let textBody: String enum CodingKeys: String, CodingKey { case textBody = "TextBody" } } let json = """ { "TextBody":…
Brandt
  • 333
  • 3
  • 11
6
votes
2 answers

Decoding arbitrary json field dynamically in Swift

TL;DR Is there a way that I can use JSONDecoder and write a function which will just read out from given json given field value of specified decodable type? Imaging I have the following json: { "product":{ "name":"PR1", "price":20 …
frangulyan
  • 3,580
  • 4
  • 36
  • 64
6
votes
1 answer

How to decode json with unknown key

everyone, how to serialize json struct, if one of field unknown? My json is: …
Eugeny
  • 308
  • 4
  • 12
6
votes
3 answers

Swift Codable - Parse JSON array which can contain different data type

I am trying to parse a JSON array which can be { "config_data": [ { "name": "illuminate", "config_title": "Blink" }, { "name": "shoot", "config_title": "Fire" } ] } or it can be of…
Ganesh Somani
  • 2,280
  • 2
  • 28
  • 37
1
2 3
35 36