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:

393 questions
23
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
  • 551
  • 5
  • 18
17
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
  • 216
  • 2
  • 7
11
votes
2 answers

python requests randomly breaks with JSONDecodeError

I have been debugging for hours why my code randomly breaks with this error: JSONDecodeError: Expecting value: line 1 column 1 (char 0) This is the code I have: while True: try: submissions =…
mateocam
  • 161
  • 1
  • 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…
7
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
  • 566
  • 6
  • 16
7
votes
4 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
  • 298
  • 4
  • 15
6
votes
1 answer

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),…
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
  • 2,790
  • 25
  • 50
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
  • 1,848
  • 1
  • 24
  • 36
5
votes
4 answers

swift parse json - The data couldn’t be read because it isn’t in the correct format

Here is struct codable code: import Foundation public struct TaskID: Codable { let embedded: Embedded } public struct Embedded: Codable { let task: [Task] } public struct Task : Codable { let embedded: EmbeddedVariable let id :…
PvUIDev
  • 83
  • 3
  • 21
5
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
  • 313
  • 1
  • 10
5
votes
1 answer

Using Generics / Codable w/ API response 204 NO CONTENT

I am using generics and codable with URLSession. When I receive a response from an API, I check the status is in the 200 - 299 range and decode the data like so guard let data = data, let value = try? JSONDecoder().decode(T.self, from: data)…
Tim J
  • 901
  • 4
  • 17
4
votes
3 answers

How should I decode a json object using JSONDecoder if I am unsure of the keys

I have an api response in the following shape - { "textEntries":{ "summary":{ "id":"101e9136-efd9-469e-9848-132023d51fb1", "text":"some text", "locale":"en_GB" }, "body":{ …
Teddy K
  • 660
  • 2
  • 10
4
votes
2 answers

How do I decode JSON in Swift when it's an array and the first item is a different type than the rest?

Say the JSON looks like this: [ { "name": "Spot", "breed": "dalmation" }, { "color": "green", "eats": "lettuce" }, { "color": "brown", "eats": "spinach" }, { …
Doug Smith
  • 27,683
  • 54
  • 189
  • 363
4
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
  • 179
  • 2
  • 11
1
2 3
26 27