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

Python json.load JSONDecodeError: Extra data error when trying load multiple json dictionaries

So I have to import a json file in an identical format shown below: { "name": "bob" } { "name": "sarah" } This is the function I am trying to use to open it: def read_json_file(file): with open(file, "r") as r: response =…
python_help
  • 121
  • 1
  • 12
2
votes
1 answer

Problem decoding data from json with a specific structure

I'm trying to decode this response data: { "headers": {}, "original": { "code": 201, "success": true, "message": "Message" }, "exception": null } With this struct: struct MarcaAguaResData: Codable { let original:…
Edward Pizzurro
  • 575
  • 4
  • 15
2
votes
1 answer

Swift JSON decoding dictionary fails

I have a structure as shown below: struct ItemList: Decodable { var items: [UUID: Int] } Example JSON data I get is: { "items": { "b4f8d2fa-941f-4f9a-a98c-060bbd468575": 418226428193, "81efa661-4845-491b-8bf4-06d5dff1d5f8":…
Bram
  • 2,718
  • 1
  • 22
  • 43
2
votes
1 answer

How to decode an http response data from URLSession and map to a response structure swift

I am new to swift programming..was able to obtain a successful response from URLSession but I am unable to parse (decode) the data object to my desired APIResponse Structure this is my url request code: func load(urlRequest: URLRequest,…
Rijo Samuel
  • 281
  • 3
  • 10
2
votes
1 answer

google.protobuf.message.DecodeError: Wrong wire type in tag Error in Protocol Buffer

I trying decrypt my data using google protocol buffer in python sample.proto file:- syntax = "proto3"; message SimpleMessage { string deviceID = 1; string timeStamp = 2; string data = 3; } After that, I have generated python files using the proto…
2
votes
1 answer

The data couldn’t be read because it isn’t in the correct format. Swift 5

I am trying to decode data from https://swapi.dev/. I get json correctly with response code 200, but the decoder could not read the data because the format is incorrect. I tried it in a lot of different ways. I am trying to get info on peoples. Here…
drebin96
  • 156
  • 1
  • 13
2
votes
1 answer

Some variables returns nil while I want to decode my data with JSONDecoder()

i am using Urlsession and JSONDecoder to get and decode my data but somehow some variables on Weather struct returns nil while im decoding it. it may be something related to utf8 but I can't figure out how to handle. var request = URLRequest(url:…
MertGurcan
  • 23
  • 4
2
votes
2 answers

swift: How can I decode an array of json objects, without creating a struct, that holds an array of said objects?

My data looks like this: "places": [ { "id": 15, "name": "København", "typeId": 6, "coordinates": { "lat": "55.6760968", "lng": "12.5683372" }, …
2
votes
0 answers

JSONDecoder: saving raw JSON for some fields

I am using JSONDecoder to save my JSON data into objects. But I would like some part of the JSON to be saved as is. Is that possible? Say this is my JSON "customer": { "personal": { "name": "John Doe", "misc": { …
user3539959
  • 383
  • 3
  • 15
2
votes
0 answers

Issue with building data model in swift for Oxford dictionary API

I'm trying to implement data model using Oxford Dictionary API, I've created the below data model in Swift based on the documentation, but Xcode is throwing the below error that does not show exactly where the problem is, thus paralyzing me, but I…
Avinash R
  • 21
  • 1
2
votes
1 answer

how to read local JSON with Alamofire in Swift

I have a local json file and I access the file path with Bundle.main.path function, but I get an error. Service class Service { fileprivate var baseURL: String? init(baseURL: String) { self.baseURL = baseURL } …
Ufuk Köşker
  • 1,288
  • 8
  • 29
2
votes
2 answers

keynotfound on id decoding JSON

I'm getting the following error when trying to decode JSON. What's odd is I've used similar code on other endpoints and no issue using UUID. error: keyNotFound(CodingKeys(stringValue: "id", intValue: nil), Swift.DecodingError.Context(codingPath:…
Robert
  • 809
  • 3
  • 10
  • 19
2
votes
2 answers

Swift: key not found JSON decoding

I am trying to decode a JSON object, from this website: https://www.thesportsdb.com/api/v1/json/1/search_all_leagues.php?c=France&s=Soccer I would like to store them in an array of Soccer elements, and show them in cells. This is the code I did, but…
volvic2L
  • 35
  • 1
  • 5
2
votes
5 answers

Parsing complex JSON where data and "column headers" are in separate arrays

I have the following JSON data I get from an API: {"datatable": {"data" : [ ["John", "Doe", "1990-01-01", "Chicago"], ["Jane", "Doe", "2000-01-01", "San Diego"] ], "columns": [ { "name": "First", "type": "String" }, {…
Joseph
  • 9,171
  • 8
  • 41
  • 67
2
votes
1 answer

handle two diffrent http json response swift

I have an API that gives me two different responses. here is when data is correct and I have objects: { "latlng": [My Objects] } and here is when my data is empty: [] so my question is how to handle this empty response when I'm using…
MoeinDeveloper
  • 251
  • 1
  • 2
  • 11