Why is Swift failing to decode a simple JSON response? - ios

I am making a POST request, and the API it's calling (hosted on AWS Lambda/Gateway) returns one int. Yet, when Swift tries to handle the API call, its errors out with:
typeMismatch(Swift.Int, Swift.DecodingError.Context(codingPath: [], debugDescription: "Expected to decode Int but found a string/data instead.", underlyingError: nil))
Here is the task:
let task = URLSession.shared.dataTask(with: urlRequest) { [weak self] data, _, error in
guard let data = data, error == nil else {
return
}
do {
let decoded = try JSONDecoder().decode(Int.self, from: data)
DispatchQueue.main.async {
self?.session_id = decoded
}
}
catch {
print("Error: something went wrong calling api posting session", error)
}
}
When I make the same call in Postman, the response is just:
119
I have tried changing the decode line to
.decode(String.self, from: data)
but then it fails again because of some thread issues.
I have also tried changing the back end to return an actual json object, as well as adding an object to swift, with one property being the desired session_id (with very carefully checked types, tried with Optionals and not), but it returns with error:
typeMismatch(Swift.Dictionary<Swift.String, Any>, Swift.DecodingError.Context(codingPath: [], debugDescription: "Expected to decode Dictionary<String, Any> but found a string/data instead.", underlyingError: nil))
The weirdest part is that whenever I make a GET request, swift has been decoding everything fine. Where could my issue be?

Related

Xcode, The given data was not valid JSON, Can't read the JSON from API properly

Hello I am creating an app with Xcode and I am having the following problem, I created this API (if you enter the link you'll see the JSON data) https://proyecto-idts6.epizy.com/models/getCategorias.php
If you dont want to enter the link here is how this si how the structure of the JSON looks like:
{
"items":[
{
"categorie":"Fruits",
"id_categorie":"1"
},
{
"categorie":"Animals",
"id_categorie":"2"
},
{
"categorie":"Juices",
"id_categorie":"3"
},
{
"categorie":"Vegetables",
"id_categorie":"4"
},
{
"categorie":"Alcohol",
"id_categorie":"5"
},
{
"categorie":"Desserts",
"id_categorie":"6"
}
]
}
The problem I have is that when I try to decode the data from the API it cant't be decoded properly, I am trying to recreate the same code of this youtube video, but with my API: https://www.youtube.com/watch?v=sqo844saoC4
What I want basically is to print the categories and storage each of them in variables (because i'll need to move the variables between screens)
This is how my code looks like:
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let url = "https://proyecto-idts6.epizy.com/models/getCategorias.php"
getData(from: url)
//Here is where i want to storage the variables from the JSON
}
private func getData(from url: String) {
let task = URLSession.shared.dataTask(with: URL(string: url)!, completionHandler: { data, response, error in
guard let data = data, error == nil else {
print("something went wrong")
return
}
do {
let result = try JSONDecoder().decode([ResultItem].self, from: data)
print(result)
}
catch {
print("failed to convert\(error)")
}
})
task.resume()
}
}
struct Response: Codable {
let items: [ResultItem]
}
struct ResultItem: Codable {
let categorie: String
}
My goal is to have variables for example like this: categorie1=("the category 1 called from the JSON"), categorie2=("the category 2 called from the JSON"), categorie3=("the category 3 called from the JSON"),...
The problem is not in the decoding but in the remote API.
Your endpoint (https://proyecto-idts6.epizy.com/models/getCategorias.php) instead of returning a JSON is returning the following HTML
<html><body><script type="text/javascript" src="/aes.js" ></script><script>function toNumbers(d){var e=[];d.replace(/(..)/g,function(d){e.push(parseInt(d,16))});return e}function toHex(){for(var d=[],d=1==arguments.length&&arguments[0].constructor==Array?arguments[0]:arguments,e="",f=0;f<d.length;f++)e+=(16>d[f]?"0":"")+d[f].toString(16);return e.toLowerCase()}var a=toNumbers("f655ba9d09a112d4968c63579db590b4"),b=toNumbers("98344c2eee86c3994890592585b49f80"),c=toNumbers("f5490e280a5e50f74932909856c3d3a3");document.cookie="__test="+toHex(slowAES.decrypt(c,2,a,b))+"; expires=Thu, 31-Dec-37 23:55:55 GMT; path=/"; location.href="https://proyecto-idts6.epizy.com/models/getCategorias.php?i=1";</script><noscript>This site requires Javascript to work, please enable Javascript in your browser or use a browser with Javascript support</noscript></body></html>
So you are trying to decode that HTML content, which clearly leads to the error your reported
failed to convertdataCorrupted(Swift.DecodingError.Context(codingPath: [], debugDescription: "The given data was not valid JSON.", underlyingError: Optional(Error Domain=NSCocoaErrorDomain Code=3840 "Invalid value around line 1, column 0." UserInfo={NSDebugDescription=Invalid value around line 1, column 0., NSJSONSerializationErrorIndex=0})))

Swift URLRequest to Parse Platform REST API (JSON) & Decode into Struct

I am trying to use specific capabilities not available in the available iOS SDK for Parse Platform (Server), but that I know are available with the REST API. Specifically to use a DISTINCT query.
Using the Parse Dashboard and REST API client app on my dev computer (Rested.app), I have verified the following query completes as expected:
curl -X GET \
-H "X-Parse-Application-Id: someAppID" \
-H "X-Parse-Master-Key: someKey" \
-G \
--data-urlencode "distinct=TeeTime" \
http://somehost:1337/parse/aggregate/CompEntry
Which successfully returns data:
{
"results": [
{
"__type": "Date",
"iso": "2020-08-29T07:00:00.000Z"
},
{
"__type": "Date",
"iso": "2020-08-29T07:09:00.000Z"
}
] }
The original data is from, which has 3 rows, 2 of which share the same TeeTime:
And a screenshot of the output from the Rested.app:
Now I am trying to convert this for my Swift / iOS project.
I am trying to move the downloaded data into a new struct to represent the object(s), using the Codable/Decodable approach and matching the JSON property names. The code I have so far is below (placed some comments inline too). The Struct definitions occur in separate .swift files, but so long as outside the main ViewController definition.
struct TeeTimeData: Codable {
let results: [Results]
}
struct Results: Codable {
let __type: String
let iso: String // TODO: THIS SHOULD BE A DIFFERENT DATA TYPE - I HAVE PICKED HARDER DATA TYPE TO START WITH!
}
Then within the main ViewController struct:
class ViewController: UIViewController {
#IBAction func buttonGetTeeTimes(_ sender: UIButton) {
if let url = URL(string: "http://somehost:1337/parse/aggregate/CompEntry") {
var request = URLRequest(url: url)
request.addValue("someAppID", forHTTPHeaderField: "X-Parse-Application-Id")
request.addValue("someKey", forHTTPHeaderField: "X-Parse-Master-Key")
request.httpMethod = "GET"
let params = ["distinct": "TeeTime"] // TODO: THIS VAR IS NOT ACTUALLY BEING TIED TO THE REQUEST EITHER - 2nd PROBLEM...
let session = URLSession(configuration: .default)
let requestTask = session.dataTask(with: request) { (data, response, error) in
if error != nil {
print("API Error: \(error)")
}
if let dataUnwrap = data {
// TODO: MOVE THIS INTO NEW CLASS (DataModel, etc)
let decoder = JSONDecoder()
do {
let decodedData = try decoder.decode(TeeTimeData.self, from: dataUnwrap)
print(decodedData)
} catch {
print("Decode Error: \(error)")
}
}
}
requestTask.resume()
}
}
}
And the console output is:
Decode Error: keyNotFound(CodingKeys(stringValue: "__type", intValue:
nil), Swift.DecodingError.Context(codingPath: [CodingKeys(stringValue:
"results", intValue: nil), _JSONKey(stringValue: "Index 0", intValue:
0)], debugDescription: "No value associated with key
CodingKeys(stringValue: "__type", intValue: nil) ("__type").",
underlyingError: nil))
My first guess is the 2 underscores, "__type", at the start of the property definition?
Answer provided by #Larme in the comments to the opening post. Contributions from #gcharita that my syntax should have worked if it was the same structure as the other tests I was performing (it wasn't - see below).
The error was occurring because the results being returned was a different JSON structure because the distinct=TeeTime filter wasn't being applied. So it was returning a JSON object with all the rows of data from the class, not those specified in my TeeTimeData struct object.
Appending ?distinct=TeeTime to the end of the URL string resolved the issue and returns:
Decoded data: TeeTimeData(results: [Parse_2.Results(__type: "Date",
iso: "2020-08-29T07:00:00.000Z"), Parse_2.Results(__type: "Date", iso:
"2020-08-29T07:09:00.000Z")])
It also means I no longer require the let params = ["distinct": "TeeTime"] code as well.

JSONDecoder - "Expected to decode Dictionary<String, Any> but found a string/data instead."

So i am trying to decode a json and get this error.
This is the JSON :
{ "SERVERWebSystemInfoGet": {
"Return Code" : 0,
"Return String" : "No Error",
"Info" : "{\"IT\":\"IT109200310_0\",\"MAC\":\"00:40:7F:41:F8:81\",\"UUID\":\"uuid:858fba00-d3a0-11dd-a001-00407f41f881\",\"SN\":\"ENG031\",\"ModelNumber\":\"DH-390 2MP\",\"ModelName\":\"DH-390 2MP\",\"FwVer\":\"v1.0.0.34\",\"HwVer\":\"\",\"FriendlyName\":\"DH-390 2MP ENG031\",\"UpTime\":548}" }
}
This are my models :
struct Information: Codable {
let ModelName : String?
}
struct GetInformation: Codable {
let Info: [String: Information]?
}
struct WebSystemInfo: Codable {
let SERVERWebSystemInfoGet: GetInformation?
}
This is the method :
func parseGetInfo(data: Data) {
do {
let info = try JSONDecoder().decode(WebSystemInfo.self, from: data)
print(info)
} catch let error{
print(error)
}
}
And this is the error that i get :
typeMismatch(Swift.Dictionary
Swift.DecodingError.Context(codingPath: [CodingKeys(stringValue: "SERVERWebSystemInfoGet", intValue: nil),
CodingKeys(stringValue: "Info", intValue: nil)],
debugDescription: "Expected to decode Dictionary but found a string/data instead.", underlyingError: nil))
This
"Info" : "{\"IT\":\"IT109200310_0\",\"MAC\":\"00:40:7F:41:F8:81\",\"UUID\":\"uuid:858fba00-d3a0-11dd-a001-00407f41f881\",\"SN\":\"ENG031\",\"ModelNumber\":\"DH-390 2MP\",\"ModelName\":\"DH-390 2MP\",\"FwVer\":\"v1.0.0.34\",\"HwVer\":\"\",\"FriendlyName\":\"DH-390 2MP ENG031\",\"UpTime\":548}" }
is a json string not a dictionary you need
let Info:String?
This happens because the Info value is actually a string and not a dictionary.
Notice that it starts with quotes.
Change the model to return Dictionary instead of String.
You copied JSON which has escaped bits: ” with \”, which makes the info-dictionary a string.
Try the following string with the escaping removed whether you can decode it.
{
"SERVERWebSystemInfoGet": {
"Return Code": 0,
"Return String": "No Error",
"Info": {
"IT": "IT109200310_0",
"MAC": "00:40:7F:41:F8:81",
"UUID": "uuid:858fba00-d3a0-11dd-a001-00407f41f881",
"SN":"ENG031",
"ModelNumber": "DH-390 2MP",
"ModelName": "DH-390 2MP",
"FwVer": "v1.0.0.34",
"HwVer": "x",
"FriendlyName": "DH-390 2MP ENG031",
"UpTime": "548"
}
}
}
Then you can think about changing the server output if you can, or decoding info manually if you can’t by following this guide, it starts at Manual Encoding and Decoding with the important bits.

How to remove xml tags before send to the json parser in swift

I want to use JSON data in my app. So I am using this webservice calling method to convert my json data to an array.
func getData(path: String, completion: (dataArray: NSArray)->()) {
let semaphore = dispatch_semaphore_create(0)
// var datalistArray = NSArray()
let baseUrl = NSBundle.mainBundle().infoDictionary!["BaseURL"] as! String
let fullUrl = "\(baseUrl)\(path)"
print("FULL URL-----HTTPClient \(fullUrl)")
guard let endpoint = NSURL(string:fullUrl) else {
print("Error creating endpoint")
return
}
let request = NSMutableURLRequest(URL: endpoint)
NSURLSession.sharedSession().dataTaskWithRequest(request,completionHandler: {(data,response,error) in
do {
guard let data = data else {
throw JSONError.NoData
}
guard let json = try NSJSONSerialization.JSONObjectWithData(data, options: []) as? NSArray else {//NSJSONReadingOptions.AllowFragments
throw JSONError.ConversionFailed
}
print(json)
if let data_list:NSArray = json {
completion(dataArray: data_list)
dispatch_semaphore_signal(semaphore);
}
}catch let error as JSONError {
print(error.rawValue)
} catch let error as NSError {
print(error.debugDescription)
}
}) .resume()
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
}
But now my service sending json data within xml tags like <string xmlns="http://tempuri.org/">json data</string so I am getting an exception when I try to convert my json data. The exception is this.
Error Domain=NSCocoaErrorDomain Code=3840 "Invalid value around character 0." UserInfo={NSDebugDescription=Invalid value around character 0.}
What should I change in my code to remove those tags before sending to json parser?
Please help me.
Thanks
I think your response that you get from server is in xml format not in json. If it is in xml format then you must do xml parsing instead of json parsing.
NSJSONSerialization.JSONObjectWithData is json parsing that give json object from data (data in json format).
But if you getting response in xml format from server then you should use NSXMLParser to parse the data.
If you don't have much idea about it then you can refer tutorial like
XML Parsing using NSXMLParse in Swift by The appguruz or can use third party libraries.

Parse JSON in SWIFT

Hi i try to find a way to parse JSON in SWIFT, this works great for me but i run into a problem.
I let the user enter a username that is used for the JSON URL -> if the user type in a valid username all works fine.
But if he enter a wrong username my parsing fails, this is correct too, but for now my app only crashes and i looking for a way to make a work around.
This is my Code where it crashes,
let url0 = NSURL(string: newUrlPath!)
let session0 = NSURLSession.sharedSession()
let task0 = session0.dataTaskWithURL(url0!, completionHandler: {data, response, error -> Void in
if (error != nil) {
println(error)
} else {
let summonorID_JSON = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: nil) as NSDictionary
The Xcode Error
Error Domain=NSURLErrorDomain Code=-1002 "The operation couldn’t be
completed. (NSURLErrorDomain error -1002.)" UserInfo=0x7c12d610
{NSErrorFailingURLKey=XX, NSErrorFailingURLStringKey=XX,
NSUnderlyingError=0x7c12c8d0 "The operation couldn’t be completed.
(kCFErrorDomainCFNetwork error -1002.)"} fatal error: unexpectedly
found nil while unwrapping an Optional value
All is fine cause this is the return page i get from my Request
https://br.api.pvp.net/api/lol/br/v1.4/summoner/by-name/smirknaitiax?api_key=5c7d4d4f-f320-43d5-8647-643c9f6ee5de
And yes he can't parse this into a NSDirectory as its no JSON that returns (as its normally is) is there a way to take care that if this page comes up (so the user entered a wrong username) that i can exit my loop/take a other way ;)?
You are using many operations which could all fail, and Swift is quite unforgiving about failure. Your code will crash if newURLPath is nil, if url0 is nil because newURLPath wasn't a valid URL.
So your URL request might return an error (the request itself failed), but you have the case that the URL request succeeded but gives unexpected results (not a JSON dictionary). Your code ending in "as NSDictionary" tells Swift: "I know I might not get a dictionary, but convert what you get to a dictionary and crash if this doesn't work". Just change this to
if let parsedJSON = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: nil)
{
// Will still crash if the server sends a valid JSON array
let summonorID_JSON = parsedJSON as NSDictionary
}
else
{
// data wasn't valid JSON, handle it.
}
The difference is that the optional value returned by the JSON parser will be accepted without crashing, and you check whether you received valid JSON or not.
Since you are getting 404 on this request, I assume that this will happen every time something is bad with username, you should handle server response to fit that. First thing will be to check what server returned:
let httpResp: NSHTTPURLResponse = response as NSHTTPURLRespons
At this point you can access statusCode property, that will tell you if request was good or not (404). Having that information you can decide what to do, and for example, you can modify your code something like this:
let url0 = NSURL(string: newUrlPath!)
let session0 = NSURLSession.sharedSession()
let task0 = session0.dataTaskWithURL(url0!, completionHandler: {data, response, error -> Void in
if (error != nil) {
println(error)
} else {
let httpResp: NSHTTPURLResponse = response as NSHTTPURLRespons
httpResp.statusCode != 404 {
let summonorID_JSON = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: nil) as NSDictionary
} else {
// Handle error at this point, tell user to retype username etc.
}
})
NSURL is a failable initializer and exactly this happens when you give an invalid url: It fails to initialize.
So wrap your code in an conditional unwrap:
if let url0 = NSURL(string: newUrlPath!) {
...
}
The url0 becomes nil if user enter wrong data. If you use the nil value as url0! app will crash.
When you add a ! after a variable you tell the compiler the value will not be nil.
so to avoid the crash, you have to check for nil condition before calling
let task0 = session0.dataTaskWithURL(url0!, completionHandler: {data, response, error -> Void in

Resources