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
votes
1 answer

Decode JSON array inside a JSON dictionary without creating Boilerplate Types

The JSON: let jsonString = """ { "groups": [ { "id": "oruoiru", "testProp": "rhorir", "name": "* C-Level", "description": "C-Level" }, { "id": "seese", "testProp": "seses", …
Anton Unt
  • 1,835
  • 1
  • 21
  • 47
-1
votes
2 answers

Present results with alamofire/Swift

When I try to present the results I received this message "Response could not be decoded because of error: The data couldn’t be read because it isn’t in the correct format." This is my format and its right I think. import Foundation // MARK: -…
georgekak
  • 73
  • 11
-1
votes
1 answer

JSONDecoder does not work for Swift code (SWIFT UI)

I'm trying to decode data from JSONDecoder and API for my app. fetchDate() in ContentVeiw.swift works, but decodedResponse is nil which means JSONDecoder needs to be fixed. How do I fix this? Here's my code. ContentView.Swift func fetchData() async…
-1
votes
1 answer

Swift 5 Decode JSON from response and discard parent node

I would like to decode this JSON using codingkeys and decoder methods in my Swift code. I would rather just create two data models : Photos that contains the page information and the array of type Photo and not have to create another type. I tried…
Anton Unt
  • 1,835
  • 1
  • 21
  • 47
-1
votes
2 answers

Printing from a dictionary single values

Currently trying to get to print all strTeam from the teams:[Team] dictionary. Any guidance here will be appreciated. If you see any other mistakes please let me know. Thank you. import UIKit class ViewController2: UIViewController { override…
-1
votes
1 answer

Python Jira Rest Api code generates invalid string

I am trying to connect to Jira Rest Api using Python and trying to save data into text file but i am getting huge string returned as response which seems to be invalid string and it also contains html tags. I am trying to connect Jira using Api…
Andrew
  • 3,632
  • 24
  • 64
  • 113
-1
votes
1 answer

Swiftui The data couldn’t be read because it isn’t in the correct format

I am getting the above error when i run my code, using an api. I double checked copying the json file and running it locally it worked correctly, i cant seem to get where the error is coming from, Countries.swift struct APIResult: Codable { var…
EMPIRE
  • 57
  • 7
-1
votes
1 answer

Swift: decode JSON with unknown keys?

I would like to decode a simple JSON file, the thing is that the top keys will all be different: { "pikachu": { "name": "Pikachu", "number": 25 }, "bulbasaur": { "name": "Bulbasaur", "number": 1 }, …
Dannis Case
  • 607
  • 2
  • 5
  • 10
-1
votes
1 answer

Error: "Expected to decode Dictionary but found an array instead." — but I haven't defined a dictionary?

I'm working on a creative project, and I'm trying to decode content from an API database using Swift's JSONDecoder() function. I've built my structs, a getData() function, and I've set up a do-try-catch for the JSONDecoder() function. I'm having…
cassilly
  • 29
  • 4
-1
votes
1 answer

Extract Array of Strings from Decoded JSON in Swift

I am using JSONDecoder to decode JSON obtained from an API into an array of objects each of which contains a key value pair of interest. For that key, I would like to obtain an array of the values (that are strings). The JSON decodes fine into an…
user6631314
  • 1,751
  • 1
  • 13
  • 44
-1
votes
1 answer

Swift Json Single Quote Parse

I have a problem while parsing json which includes single quote. I am using JSONDecoder. I added response from API at below and I don't want to do any replacement or some regex operations. Are there any workaround for that? "{\'value1\': true,…
Cagdas
  • 101
  • 2
  • 9
-1
votes
1 answer

Convert quoted number from JSON (swift)

I have a struct like bellow: struct MyStruct: Codable { var id: Int? } and the JSON that i was receiving from server was like this: { "id": 12345 } But now server-side decided to send all numbers as quoted numbers like this: { …
-1
votes
2 answers

Laravel JSON to Array

I have to convert json to arry from the laravel notifications table data field, This is the the data shown on dd($user->notifications); This is how the response shows in the postman I need to show the payload within [] as below { "payload": […
Jareer
  • 242
  • 5
  • 24
-1
votes
2 answers

how to decode json object in laravel which is passed as form_data in postman

In first image you can see how I am sending a json object as form data in order_details parameter In second image I am getting a response while decoding it in Laravel 7 public function makeOrder(Request $request) { $validator =…
-1
votes
1 answer

How to use throw from within do block?

I would like to throw error from within do block, if that is possible I have a function getWeatherBy, it is signed as a throwing function. My code elsewhere in the app is depending on this throwing. I have inside of this function a…
ShoeMaker
  • 149
  • 1
  • 2
  • 9