"Incorrect JSON format" - json

I am making a POST request with required body parameters, testing this POST request in POSTMAN gives me correct response, but when I am passing same body parameters from my code I am getting below error-:
Error Domain=NSCocoaErrorDomain Code=3840 "JSON text did not start with array or object and option to allow fragments not set." UserInfo={NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.
Code -:
func submitStudentAttendance(url:String,id:String,SessionID: Int,SchoolID:Int,BatchID:Int,ClassID:Int,SectionID:Int,Medium:Int,Date:String,accessToken:String) {
let params : [String:Any] = [
"$id":"1",
"SchoolID":1,
"SessionID":42,
"BatchID":106,
"ClassID":634,
"SectionID":1246,
"Medium":"English",
"Date":"0001-01-01T00:00:00",
"CreatedBy":118,
"CreatedOn":"2019-04-14T17:02:34",
"studentList":"",
"AttendenceData":"[{\"ID\":0,\"SchoolID\":null,\"SessionID\":null,\"ClassID\":634,\"SectionID\":1246,\"StudID\":\"DSM00399\",\"SubjectID\":null,\"Date\":\"0001-01-01T00:00:00\",\"Attendence\":\"P\",\"CreatedOn\":null,\"CreatedBy\":0,\"IsActive\":null,\"Remarks\":\"rem16\",\"timeID\":null,\"AtndType\":null,\"BatchID\":null,\"Medium\":null}]"
]
let urlRequest = NSURL(string: url)
var request = URLRequest(url: urlRequest! as URL)
let tokenString = "Bearer " + accessToken
request.httpMethod = "POST"
request.setValue(tokenString, forHTTPHeaderField: "Authorization")
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
let data = try! JSONSerialization.data(withJSONObject: params, options: [])
let json = NSString(data: data, encoding: String.Encoding.utf8.rawValue)
if let json = json {
print(json)
}
request.httpBody = json!.data(using: String.Encoding.utf8.rawValue);
let alamoRequest = Alamofire.request(request as URLRequestConvertible)
alamoRequest.responseString {
response in
switch response.result {
case .success:
if response.data != nil {
do{
let json = try JSON(data: response.data!)
print(json)
}catch{
print(error)
}
}
break
case .failure(let error):
print(error)
}
}
}
The body part is exactly same as in POSTMAN, but still not working. Can anyone let me know what's going wrong here? Is this server side issue or I am sending body request in wrong format?
POSTMAN RESPONSE -:

When the params as declared in your question is converted into JSON, it looks like this. Here, AttendanceData value is not converted as proper JSON structure. It looks like a string.
JSON from your param:
{
"Date": "0001-01-01T00:00:00",
"CreatedBy": 118,
"$id": "1",
"BatchID": 106,
"ClassID": 634,
"Medium": "English",
"studentList": "",
"SchoolID": 1,
"SectionID": 1246,
"AttendenceData": "[{\"ID\":0,\"SchoolID\":null,\"SessionID\":null,\"ClassID\":634,\"SectionID\":1246,\"StudID\":\"DSM00399\",\"SubjectID\":null,\"Date\":\"0001-01-01T00:00:00\",\"Attendence\":\"P\",\"CreatedOn\":null,\"CreatedBy\":0,\"IsActive\":null,\"Remarks\":\"rem16\",\"timeID\":null,\"AtndType\":null,\"BatchID\":null,\"Medium\":null}]",
"CreatedOn": "2019-04-14T17:02:34",
"SessionID": 42
}
But if you declare param as shown below, and converted to JSON, AttendanceData value is properly converted to JSON.
let params : [String:Any] = [
"$id":"1",
"SchoolID":1,
"SessionID":42,
"BatchID":106,
"ClassID":634,
"SectionID":1246,
"Medium":"English",
"Date":"0001-01-01T00:00:00",
"CreatedBy":118,
"CreatedOn":"2019-04-14T17:02:34",
"studentList":"",
"AttendenceData":[
"ID":0,"SchoolID":"null","SessionID":"null","ClassID":634,"SectionID":1246,"StudID":"DSM00399","SubjectID":"null","Date":"0001-01-01T00:00:00","Attendence":"P","CreatedOn":"null","CreatedBy":0,"IsActive":"null","Remarks":"rem16","timeID":"null","AtndType":"null","BatchID":"null","Medium":"null"
]
]
JSON
{
"$id": "1",
"BatchID": 106,
"CreatedBy": 118,
"ClassID": 634,
"studentList": "",
"SectionID": 1246,
"SessionID": 42,
"AttendenceData": {
"IsActive": "null",
"AtndType": "null",
"SectionID": 1246,
"timeID": "null",
"SessionID": "null",
"Attendence": "P",
"Date": "0001-01-01T00:00:00",
"Remarks": "rem16",
"Medium": "null",
"SchoolID": "null",
"BatchID": "null",
"ClassID": 634,
"StudID": "DSM00399",
"SubjectID": "null",
"CreatedBy": 0,
"ID": 0,
"CreatedOn": "null"
},
"Date": "0001-01-01T00:00:00",
"CreatedOn": "2019-04-14T17:02:34",
"Medium": "English",
"SchoolID": 1
}
Hope it could also solve your problem.

Related

No value associated with key CodingKeys with Unsplash API

Im trying to decode this json from the Unsplash API, but the ContentView is coming up as blank, and if I print the results then I am getting the "No value associated with key CodingKeys" error. Its strange, because I'm following this very new tutorial https://www.youtube.com/watch?v=CmOe9vNopjU. I am very puzzled, because this should be simple, but I am new to Swift.
here is the full error
"No value associated with key CodingKeys(stringValue: \"total\", intValue: nil) (\"total\").", underlyingError: nil))
here is the decoding request
class SearchObjectController : ObservableObject {
static let shared = SearchObjectController()
private init() {}
var token = "my client id"
#Published var results = [Result]()
#Published var searchText : String = "Forest"
func search () {
let url = URL(string: "https://api.unsplash.com/search/photos?query=\(searchText)")
var request = URLRequest(url: url!)
request.httpMethod = "GET"
request.setValue("Client-ID\(token)", forHTTPHeaderField: "Authorization")
let task = URLSession.shared.dataTask(with: request) { (data, response, error) in
guard let data = data else {return}
do {
let res = try JSONDecoder().decode(Results.self, from: data)
self.results.append(contentsOf: res.results)
print(self.results)
} catch {
print(error)
}
}
task.resume()
}
}
and these are the Structs i created for that request
struct Results : Codable {
var total : Int
var results : [Result]
}
struct Result : Codable {
var id : String
var description : String?
var urls : URLs
}
struct URLs : Codable {
var small : String
}
the json format for Unplash API Requests looks like this
{
"total": 133,
"total_pages": 7,
"results": [
{
"id": "eOLpJytrbsQ",
"created_at": "2014-11-18T14:35:36-05:00",
"width": 4000,
"height": 3000,
"color": "#A7A2A1",
"blur_hash": "LaLXMa9Fx[D%~q%MtQM|kDRjtRIU",
"likes": 286,
"liked_by_user": false,
"description": "A man drinking a coffee.",
"user": {
"id": "Ul0QVz12Goo",
"username": "ugmonk",
"name": "Jeff Sheldon",
"first_name": "Jeff",
"last_name": "Sheldon",
"instagram_username": "instantgrammer",
"twitter_username": "ugmonk",
"portfolio_url": "http://ugmonk.com/",
"profile_image": {
"small": "https://images.unsplash.com/profile-1441298803695-accd94000cac?ixlib=rb-0.3.5&q=80&fm=jpg&crop=faces&cs=tinysrgb&fit=crop&h=32&w=32&s=7cfe3b93750cb0c93e2f7caec08b5a41",
"medium": "https://images.unsplash.com/profile-1441298803695-accd94000cac?ixlib=rb-0.3.5&q=80&fm=jpg&crop=faces&cs=tinysrgb&fit=crop&h=64&w=64&s=5a9dc749c43ce5bd60870b129a40902f",
"large": "https://images.unsplash.com/profile-1441298803695-accd94000cac?ixlib=rb-0.3.5&q=80&fm=jpg&crop=faces&cs=tinysrgb&fit=crop&h=128&w=128&s=32085a077889586df88bfbe406692202"
},
"links": {
"self": "https://api.unsplash.com/users/ugmonk",
"html": "http://unsplash.com/#ugmonk",
"photos": "https://api.unsplash.com/users/ugmonk/photos",
"likes": "https://api.unsplash.com/users/ugmonk/likes"
}
},
"current_user_collections": [],
"urls": {
"raw": "https://images.unsplash.com/photo-1416339306562-f3d12fefd36f",
"full": "https://hd.unsplash.com/photo-1416339306562-f3d12fefd36f",
"regular": "https://images.unsplash.com/photo-1416339306562-f3d12fefd36f?ixlib=rb-0.3.5&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=1080&fit=max&s=92f3e02f63678acc8416d044e189f515",
"small": "https://images.unsplash.com/photo-1416339306562-f3d12fefd36f?ixlib=rb-0.3.5&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=400&fit=max&s=263af33585f9d32af39d165b000845eb",
"thumb": "https://images.unsplash.com/photo-1416339306562-f3d12fefd36f?ixlib=rb-0.3.5&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=200&fit=max&s=8aae34cf35df31a592f0bef16e6342ef"
},
"links": {
"self": "https://api.unsplash.com/photos/eOLpJytrbsQ",
"html": "http://unsplash.com/photos/eOLpJytrbsQ",
"download": "http://unsplash.com/photos/eOLpJytrbsQ/download"
}
},
// more photos ...
]
}
It's a common practice for a lot of APIs to return a detailed description of the error in the response body together with the error code when the request can't be processed. Seems that it's your case since the JSON parsing fails on the first field of the structure. Check the error and response.statusCode in the dataTask's completion block first to be sure that the API has successfully processed your request and returned valid data.
It was a missing a space between Client-ID and (token) that was causing the error.

Getting invalid json when calling JSONSerialization in Swift

I am trying to access a URL and parse the JSON output. Printing JSON in utf8 encoding
Code:
let urlString:String = "https://developer.nrel.gov/api/.../"
let pvURL = URL(string: urlString)
let dataTask = URLSession.shared.dataTask(with:pvURL!) { (data, response, error) in
do {
let json = try JSONSerialization.jsonObject(with: data!)
print(String(data: data!, encoding: .utf8)!)
}catch let err {
print("error: \(err.localizedDescription)")
}
}
dataTask.resume()
Prints the following output. I try this JSON in an online JSON parser it fails. Gives error in the first line itself.
{"inputs":{"lat":"29.93","lon":"-95.61","system_capacity":"30.30","azimuth":"180","tilt":"40","array_type":"1","module_type":"1","losses":"10"},"errors":[],"warnings":[],"version":"1.0.1","ssc_info":{"version":45,"build":"Linux 64 bit GNU/C++ Jul 7 2015 14:24:09"},"station_info":{"lat":29.93000030517578,"lon":-95.62000274658203,"elev":41.0,"tz":-6.0,"location":"None","city":"","state":"Texas","solar_resource_file":"W9562N2993.csv","distance":964},"outputs":{"ac_monthly":[3480.57373046875,3440.078369140625,3992.6513671875,3977.071533203125,4074.91357421875,3701.75,3897.655517578125,4248.00390625,4023.283447265625,4157.29931640625,3605.156005859375,3342.12890625],"poa_monthly":[139.791015625,140.18896484375,164.8218536376953,164.47149658203125,173.2971649169922,159.90576171875,169.84793090820312,186.20114135742188,173.14492797851562,176.2291717529297,148.28318786621094,136.62326049804688],"solrad_monthly":[4.509387493133545,5.006748676300049,5.316833972930908,5.4823832511901855,5.590230941772461,5.3301920890808105,5.4789652824401855,6.00648832321167,5.77149772644043,5.684812068939209,4.94277286529541,4.407201766967773],"dc_monthly":[3644.867919921875,3606.52001953125,4179.85107421875,4158.3193359375,4252.9140625,3865.03369140625,4069.092041015625,4432.62744140625,4198.369140625,4336.99609375,3767.055419921875,3490.091064453125],"ac_annual":45940.55859375,"solrad_annual":5.293959140777588,"capacity_factor":17.308107376098633}}`
whereas if I access the urlString in a browser gives valid json:
{
"inputs": {
"lat": "29.93",
"lon": "-95.61",
"system_capacity": "30.30",
"azimuth": "180",
"tilt": "40",
"array_type": "1",
"module_type": "1",
"losses": "10"
},
"errors": [],
"warnings": [],
"version": "1.0.1",
"ssc_info": {
"version": 45,
"build": "Linux 64 bit GNU/C++ Jul 7 2015 14:24:09"
},
"station_info": {
"lat": 29.93000030517578,
"lon": -95.62000274658203,
"elev": 41,
"tz": -6,
"location": "None",
"city": "",
"state": "Texas",
"solar_resource_file": "W9562N2993.csv",
"distance": 964
},
"outputs": {
"ac_monthly": [
3480.57373046875,
3440.078369140625,
3992.6513671875,
3977.071533203125,
4074.91357421875,
3701.75,
3897.655517578125,
4248.00390625,
4023.283447265625,
4157.29931640625,
3605.156005859375,
3342.12890625
],
...
}
In your code you are converting the JSON data into an JSON object(Array, Dictionary).
But In browser the JSON data is printing as String and not as JSON object(Array, Dictionary).
So if you want to print the JSON string in your code as well, you can print as like below.
let string = String(data: self, encoding: .utf8)
print("JSON String:\(String(describing: string))")
Hope it helps.

swift json parsing based on parameter

I am having the following web service response in which I need parse the parameters based on "MainServiceCategories_ID"
Here is the response
{
"Entity": {
"ID": 20021,
"UserTypeID": 1,
"UserType": null,
"UserID": 30046,
"Code": "lPx1lTEq",
"Name": "بدراء",
"EnglishName": "Badra",
"IconProfileImageID": "",
"MainImageProfileImageID": "EED372C3-5C4F-E711-8100-0CC47A343427",
"LocationLng": 78.37021,
"LocationLat": 17.432563,
"Services": [
{
"ID": 11788,
"MainServiceCategories_ID": 3,
"ServiceCategories_ID": 6,
"Children": [
{
"Parent_ID": 6,
"Name": “John”,
"EnglishName": “Johnny”,
},
{
"Parent_ID": 6,
"Name": “Ronny”,
"EnglishName": “Ronny”,
}]
},
{
"ID": 11788,
"MainServiceCategories_ID": 2,
"ServiceCategories_ID": 6,
"Children": [
{
"Parent_ID": 6,
"Name": “Samuel”,
"EnglishName": “Samuel”,
},
{
"Parent_ID": 6,
"Name": “Badri”,
"EnglishName": “Badri”,
}]
},
{
"ID": 11788,
"MainServiceCategories_ID": 3,
"ServiceCategories_ID": 6,
"Children": [
{
"Parent_ID": 6,
"Name": “emma”,
"EnglishName": “Emma”,
},
{
"Parent_ID": 6,
"Name": “Sean”,
"EnglishName": “Sean”,
}]
}]
this is the code I am writing to do it
do{
let entityDic = responseDictionary["Entity"] as? [String: Any]
let servicesDic = entityDic?["Services"] as? [Any]
for i in 0 ..< servicesDic!.count {
let services = servicesDic?[i] as? [String: Any]
let availableServices = services!["EnglishName"]
servicesNamesArr.append(availableServices! as! String)
let children = services!["Children"]
childrenServices.append(children!)
let MainServiceCategories_ID = services!["MainServiceCategories_ID"]
MainServiceCategoriesID.append(MainServiceCategories_ID!)
}
print("childrenServices \(childrenServices)")
}
catch let error{
print(error)
}
I am able to get upto children dictionaries. Now the thing is I am trying to parse "English name" under children section and separate them based on "MainServiceCategories_ID" where I am getting fatal error in unwrapping the value.
for example if MainServiceCategories_ID =1 then that "English Name" need to be store in an array and if MainServiceCategories_ID =2 then into another array
The problem is, you are trying to parse "EnglishName" as a key of an element of the "Services" array and not as a key of a "Children" element. "Services" does not have a key "EnglishName", so you can't store that in servicesNamesArr. servicesDic should also be an Array of Dictionaries and not an array of Any objects.
See below code using guard statements using safe optional unwrapping:
do{
guard let entityDic = responseDictionary["Entity"] as? [String: Any] else {return}
guard let servicesDic = entityDic?["Services"] as? [[String:Any]] else {return}
for service in services {
guard let MainServiceCategoryID = service["MainServiceCategories_ID"] as? Int else {return}
MainServiceCategoriesID.append(MainServiceCategoryID)
guard let children = service["Children"] as? [[String:Any]] else {return}
childrenServices.append(children)
if MainServiceCategoryID == 3 {
for child in children {
if let availableService = child["EnglishName"] as? String {
servicesNamesArr.append(availableService)
}
}
}
}
print("childrenServices \(childrenServices)")
} catch let error{
print(error)
}

How to parse data which starts with an array [?

So in my swift 3 xcode project, I want to parse some data using JSOmN by consuming a web service called "http://tour-pedia.org/api/"
And the data within it for example is set like this:
[
{
"address": "Science Park 904",
"category": "attraction",
"id": 30884,
"lat": 52.355320008998,
"lng": 4.9574317242814,
"location": "Amsterdam",
"name": "Dakterras Science Park",
"originalId": "4d8b3370bc848cfa1043ea2b",
"polarity": 0,
"subCategory": "Scenic Lookout",
},
{
"address": "Science Park 201",
"category": "attraction",
"id": 30661,
"lat": 52.356701093273,
"lng": 4.9529844809109,
"location": "Amsterdam",
"name": "In 'de Natuur' rondom Science Park",
"originalId": "4da2ede7c6e96ea85e1ede5d",
"polarity": 0,
"subCategory": "Field",
}
]
I want to use the address, location and name field.
so far i have done this:
func fetchInfos(){
let urlRequest = URLRequest(url: URL(string: "http://tour-pedia.org/api/")!)
let task = URLSession.shared.dataTask(with: urlRequest){ (data,response,error) in
if error != nil {
print(error)
return
}
do{
let json = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as! [String : AnyObject]
} catch let error {
print(error)
}
}
}
but i don't know how to continue on from here.
Any help will be appreciated, thank you!
Let us assume your string s is the json with sqaure brackets
String s = "[{\"a\":\"b\"}]";
s = s.substring(1, s.length() - 1);
//do all your processing on s now, as the sqaure brackets will be removed !!

Accessing JSON data in swift

I am trying to access JSON data in my swift code and I'm having trouble getting it to return correctly. Here is my JSON code:
[
{
"id": "1",
"isImage": "0",
"name": "test name",
"post": "test post",
"time": "10:27",
"ip": "192.168.1.1",
"city ": "Columbus",
"latlong": "39.896418,-82.9751105",
"clientID": "clientID",
"popularity": "300"
},
{
"id": "2",
"isImage": "0",
"name": "test name two",
"post": "test post two",
"time": "13:37",
"ip": "192.168.1.1",
"city ": "Columbus",
"latlong": "39.896418,-82.9751105",
"clientID": "clientID",
"popularity": "69"
}
]
I'd just like to know how to access the data by their keys json[0].['id'] or?
I am currently using this json.swift module and trying to access the data with
func jsonHandle(data: NSString) {
var parsedJSON = JSON(data)
var id = parsedJSON[0].["id"]
NSLog("\(id)")
}
but it returns nothing. Any Ideas?
You can call the JSON(string:...) rendition and eliminate the period between the [0] and the ["id"]:
func jsonHandle(data: NSString) {
let parsedJSON = JSON(string: data)
var id = parsedJSON[0]["id"]
NSLog("\(id)")
}
Or, if you had a NSData you could use the JSON(data: ...) rendition:
func jsonHandle(data: NSData) {
let parsedJSON = JSON(data: data)
let id = parsedJSON[0]["id"]
NSLog("\(id)")
}
Or, if you wanted to use the native NSJSONSerialization, rather than that third-party library, you could:
func jsonHandle(data: NSData) {
var error: NSError?
let parsedJSON = NSJSONSerialization.JSONObjectWithData(data, options: nil, error: &error) as NSArray
let id = parsedJSON[0]["id"]
NSLog("\(id)")
}
Personally, I'd lean towards the standard NSJSONSerialization approach as it's a tried and true approach, but that's your call.