How to access JSON data from URL in Swift 5 - json

I'm creating a list of data from a JSON list from a website. However, URLSession.shared.dataTask is returning empty data.
I've tried printing outside the function of URLSession.shared.dataTask, JSONSerilization, JSONDecoder, Different forms of URLs
I've been working on this for almost a week and I still can't figure out what's the issue. My main guess right now is that it's because I'm printing inside the function, but from tutorials online I see that they all print inside.
Any help will be greatly appreciated! :)
if let url = URL(string: urlString.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!){
URLSession.shared.dataTask(with: url) { (data, response, error) in
guard let data = data else {
print("Data != Data")
return
}
let dataStr = String(decoding: data, as: UTF8.self)
print("DataStr" + dataStr) //Empty
do {
let dataParse = try? JSONDecoder().decode(OuterData.self, from: data)
let json = try JSONSerialization.jsonObject(with: data, options: .mutableContainers)
print(dataParse) //nil
print(json)//empty data
} catch let jsonError {
print ("jsonError")
print (jsonError)
}
}.resume()
//Created from https://app.quicktype.io/#l=go
struct Suggestion: Codable {
let id: String
let name: String
let audience_size: Int
let path = [String]()
let description: String?
let topic: Topic?
let disambiguation_category: String?
enum CodingKeys: String, CodingKey {
case id, name
case audience_size = "audience_size"
case path
case description = "description"
case topic
case disambiguation_category = "disambiguation_category"
}
}
struct OuterData: Codable{
let dataSuggestion: [Suggestion]
}
enum Topic: String, Codable {
case businessAndIndustry = "Business and industry"
case education = "Education"
case foodAndDrink = "Food and drink"
case hobbiesAndActivities = "Hobbies and activities"
case lifestyleAndCulture = "Lifestyle and culture"
case newsAndEntertainment = "News and entertainment"
case people = "People"
case shoppingAndFashion = "Shopping and fashion"
case sportsAndOutdoors = "Sports and outdoors"
case technology = "Technology"
case travelPlacesAndEvents = "Travel, places and events"
}
Expected Output:
{
"data": [
{
"id": "6008740787350",
"name": "Business and industry",
"audience_size": 1759626900,
"path": [
"Interests",
"Business and industry"
],
"description": ""
},
{
"id": "6003402305839",
"name": "Business",
"audience_size": 1231141110,
"path": [
"Interests",
"Business and industry",
"Business"
],
"description": "",
"topic": "Business and industry"
},
{
"id": "6003248297213",
"name": "Product (business)",
"audience_size": 935729470,
"path": [
"Interests",
"Additional Interests",
"Product (business)"
],
"description": null,
"topic": "Business and industry"
},
{
"id": "6004037932409",
"name": "Management",
"audience_size": 323096480,
"path": [
"Interests",
"Business and industry",
"Management"
],
"description": "",
"topic": "Business and industry"
},
{
"id": "6002840040679",
"name": "Music industry",
"audience_size": 251698230,
"path": [
"Interests",
"Additional Interests",
"Music industry"
],
"description": null,
"topic": "News and entertainment"
},
{
"id": "6002884511422",
"name": "Small business",
"audience_size": 87865033,
"path": [
"Interests",
"Business and industry",
"Small business"
],
"description": "",
"topic": "Business and industry"
},
{
"id": "6003165841322",
"name": "Distribution (business)",
"audience_size": 79327710,
"path": [
"Interests",
"Additional Interests",
"Distribution (business)"
],
"description": null,
"topic": "Business and industry"
},
{
"id": "6003342222945",
"name": "Show business",
"audience_size": 69642870,
"path": [
"Interests",
"Additional Interests",
"Show business"
],
"description": null,
"topic": "News and entertainment"
},
{
"id": "6002932439173",
"name": "Subscription business model",
"audience_size": 64490740,
"path": [
"Interests",
"Additional Interests",
"Subscription business model"
],
"description": null,
"topic": "Business and industry"
},
{
"id": "6003464157303",
"name": "Business Insider",
"audience_size": 62085690,
"path": [
"Interests",
"Additional Interests",
"Business Insider"
],
"description": null,
"topic": "Business and industry"
},
{
"id": "6003190330534",
"name": "Order (business)",
"audience_size": 60961920,
"path": [
"Interests",
"Additional Interests",
"Order (business)"
],
"description": null,
"topic": "Travel, places and events"
},
...
}

Simply replace
let dataSuggestion: [Suggestion]
with
let data: [Suggestion]
The names of the struct members must match the corresponding JSON keys unless you add CodingKeys.
And – as suggested in the comments – remove always the question mark in try? inside a do - catch block

Related

Web API how to parse unicode json

I'm really confused by API of web service
Some blocks of data has a format like that:
"ja": "犯罪\u8005の子孫た\u3061が暮\u3089\u3059スラム\u8857\u3002\u5883界線の\u5411\u3053\u3046の人\u3005か\u3089は「族民」\u3068\u3055\u3052\u3059まれ\u3001差\u5225\u3092受\u3051\u3066\u3044た\u3002孤\u5150\u3060\u3063た少年・ルドは\u3001育\u3066の親\u3067\u3042るレグト\u3068\u5171にスラム\u8857に住み\u3001常人離れ\u3057た身体能力\u3092武\u5668に生計\u3092立\u3066\u3066\u3044た\u3002\u3060が\u3042る日\u3001身に覚\u3048のな\u3044罪\u3092\u7740せ\u3089れ\u3001スラムの人\u3005\u3067\u3055\u3048\u6050れる「\u5948落」\u3078\u3068落\u3068\u3055れ\u3066\u3057ま\u3046\u2026\u2026\u3002"
I realized that Swift do not work with those format. But I really don't know in what step I should convert this
My parse func:
URLSession
.shared
.dataTask(with: request) { data, response, error in
if let data = data {
do {
if let sanitisedData = String(data: data, encoding: .utf8)!.map({ String($0) }).filter({ $0 != "\\" }).joined(separator: "").replacingOccurrences(of: "U(.*?)", with: "\\\\u$1", options: .regularExpression).data(using: .utf8) {
let decoder = JSONDecoder()
let decodedResponse = try decoder.decode(Welcome.self, from: sanitisedData)
DispatchQueue.main.async {
self.data = decodedResponse
}
}
} catch let jsonError as NSError {
print("JSON Decode Failed: \(jsonError.localizedDescription)")
}
return
}
}
.resume()
Output:
JSON Decode Failed: The data couldn’t be read because it isn’t in the correct format.
I tried to convert this data to string and that's the result:
犯罪u8005の子孫たu3061が暮u3089u3059スラムu8857u3002u5883界線のu5411u3053u3046の人u3005かu3089は「族民」u3068u3055u3052u3059まれu3001差u5225u3092受u3051u3066u3044たu3002孤u5150u3060u3063た少年・ルドはu3001育u3066の親u3067u3042るレグトu3068u5171にスラムu8857に住みu3001常人離れu3057た身体能力u3092武u5668に生計u3092立u3066u3066u3044たu3002u3060がu3042る日u3001身に覚u3048のなu3044罪u3092u7740せu3089れu3001スラムの人u3005u3067u3055u3048u6050れる「u5948落」u3078u3068落u3068u3055れu3066u3057まu3046u2026u2026u3002
Example of response:
{
"result": "ok",
"response": "collection",
"data": [
{
"id": "f2e906bb-8329-4f93-af70-b6344f18aa07",
"type": "manga",
"attributes": {
"title": {
"en": "This players think i\u2019m one of them"
},
"altTitles": [
{
"ru": "Э\u0442\u0438 \u0438\u0433\u0440ок\u0438 \u0434\u0443м\u0430ю\u0442, \u0447\u0442о я о\u0434\u0438н \u0438\u0437 н\u0438\u0445"
}
],
"description": {
"en": "I am the Dark Lord. The one who united the demons. One who wields limitless power and might. The one who has had enough of it all.\n\nThese people have attacked our lands again!\n\nFor a hundred years now.... vile players have attacked our hellish lands, mercilessly plundering and killing peaceful demons.\n\nToday I will become human, infiltrate the ranks of humanity and destroy their rotten system from within... while these players think I'm one of them.",
"ru": "Я - \u0422\u0435мны\u0439 по\u0432\u0435л\u0438\u0442\u0435ль. \u0422о\u0442, к\u0442о о\u0431ъ\u0435\u0434\u0438н\u0438л \u0434\u0435моно\u0432. \u0422о\u0442, к\u0442о о\u0431л\u0430\u0434\u0430\u0435\u0442 \u0431\u0435\u0441кон\u0435\u0447но\u0439 \u0432л\u0430\u0441\u0442ью \u0438 \u0441\u0438ло\u0439. \u0422о\u0442, ком\u0443 \u0432\u0441\u0435 э\u0442о н\u0430\u0434о\u0435ло.\n\nЭ\u0442\u0438 лю\u0434\u0438 \u0432 о\u0447\u0435\u0440\u0435\u0434но\u0439 \u0440\u0430\u0437 н\u0430п\u0430л\u0438 н\u0430 н\u0430\u0448\u0438 \u0437\u0435мл\u0438!\n\n\u0412о\u0442 \u0443\u0436\u0435 \u0441о\u0442ню л\u0435\u0442\u2026. М\u0435\u0440\u0437к\u0438\u0435 \u0438\u0433\u0440ок\u0438 \u0430\u0442\u0430к\u0443ю\u0442 н\u0430\u0448\u0438 \u0430\u0434\u0441к\u0438\u0435 \u0437\u0435мл\u0438, \u0431\u0435\u0441по\u0449\u0430\u0434но \u0433\u0440\u0430\u0431я \u0438 \u0443\u0431\u0438\u0432\u0430я м\u0438\u0440ны\u0445 \u0434\u0435моно\u0432.\n\n\u0412н\u0435\u0441\u0438\u0442\u0435 Э\u0422О!\n\n\u0421\u0435\u0433о\u0434ня я \u0441\u0442\u0430н\u0443 \u0447\u0435ло\u0432\u0435ком, п\u0440о\u0431\u0435\u0440\u0443\u0441ь \u0432 \u0440я\u0434ы \u0447\u0435ло\u0432\u0435\u0447\u0435\u0441\u0442\u0432\u0430 \u0438 \u0443н\u0438\u0447\u0442о\u0436\u0443 \u0438\u0445 п\u0440о\u0433н\u0438\u0432\u0448\u0443ю \u0441\u0438\u0441\u0442\u0435м\u0443 \u0438\u0437н\u0443\u0442\u0440\u0438... пок\u0430 э\u0442\u0438 \u0438\u0433\u0440ок\u0438 \u0434\u0443м\u0430ю\u0442, \u0447\u0442о я о\u0434\u0438н \u0438\u0437 н\u0438\u0445."
},
"isLocked": false,
"links": {
"raw": "https://remanga.org/manga/this-players-think-im-one-of-them?subpath=about"
},
"originalLanguage": "ru",
"lastVolume": "",
"lastChapter": "",
"publicationDemographic": "seinen",
"status": "ongoing",
"year": 2022,
"contentRating": "suggestive",
"tags": [
{
"id": "36fd93ea-e8b8-445e-b836-358f02b3d33d",
"type": "tag",
"attributes": {
"name": {
"en": "Monsters"
},
"description": [],
"group": "theme",
"version": 1
},
"relationships": []
},
{
"id": "391b0423-d847-456f-aff0-8b0cfc03066b",
"type": "tag",
"attributes": {
"name": {
"en": "Action"
},
"description": [],
"group": "genre",
"version": 1
},
"relationships": []
},
{
"id": "39730448-9a5f-48a2-85b0-a70db87b1233",
"type": "tag",
"attributes": {
"name": {
"en": "Demons"
},
"description": [],
"group": "theme",
"version": 1
},
"relationships": []
},
{
"id": "3e2b8dae-350e-4ab8-a8ce-016e844b9f0d",
"type": "tag",
"attributes": {
"name": {
"en": "Long Strip"
},
"description": [],
"group": "format",
"version": 1
},
"relationships": []
},
{
"id": "87cc87cd-a395-47af-b27a-93258283bbc6",
"type": "tag",
"attributes": {
"name": {
"en": "Adventure"
},
"description": [],
"group": "genre",
"version": 1
},
"relationships": []
},
{
"id": "a1f53773-c69a-4ce5-8cab-fffcd90b1565",
"type": "tag",
"attributes": {
"name": {
"en": "Magic"
},
"description": [],
"group": "theme",
"version": 1
},
"relationships": []
},
{
"id": "cdc58593-87dd-415e-bbc0-2ec27bf404cc",
"type": "tag",
"attributes": {
"name": {
"en": "Fantasy"
},
"description": [],
"group": "genre",
"version": 1
},
"relationships": []
},
{
"id": "e197df38-d0e7-43b5-9b09-2842d0c326dd",
"type": "tag",
"attributes": {
"name": {
"en": "Web Comic"
},
"description": [],
"group": "format",
"version": 1
},
"relationships": []
},
{
"id": "f5ba408b-0e7a-484d-8d49-4e9125ac96de",
"type": "tag",
"attributes": {
"name": {
"en": "Full Color"
},
"description": [],
"group": "format",
"version": 1
},
"relationships": []
}
],
"state": "published",
"chapterNumbersResetOnNewVolume": false,
"createdAt": "2022-01-10T17:15:53+00:00",
"updatedAt": "2022-01-10T17:26:26+00:00",
"version": 4,
"availableTranslatedLanguages": [
"ru"
]
},
"relationships": [
{
"id": "5537d8ed-16ed-4f6f-af75-ad7d7edb2ddc",
"type": "author"
},
{
"id": "36ca9e78-35a9-474d-b4e6-0e4065f0af87",
"type": "artist"
},
{
"id": "93e21cc1-1f61-46ce-8362-90d9d6888f63",
"type": "artist"
},
{
"id": "0e703bf4-1996-432c-8620-3c08f2edb37b",
"type": "cover_art"
}
]
}
],
"limit": 1,
"offset": 0,
"total": 57
}
If your server response is really without the unicode escaped, that is,
"犯罪\u8005の子孫た\u3061 ...", then try escaping the data as shown in this example code,
to get: "犯罪\\u8005の子孫た\\u3061 ..." (note the \\) and decode it like this:
URLSession
.shared
.dataTask(with: request) { data, response, error in
if let data = data {
do {
if let sanitisedData = String(data: data, encoding: .utf8)!
.replacingOccurrences(of: "\\", with: "\\\\") // <-- here
.data(using: .utf8) {
let decoder = JSONDecoder()
let decodedResponse = try decoder.decode(Welcome.self, from: sanitisedData)
DispatchQueue.main.async {
self.data = decodedResponse
}
}
} catch let jsonError as NSError {
print("JSON Decode Failed: \(jsonError.localizedDescription)")
}
return
}
}.resume()
EDIT-1:
you may also need to do this: .replacingOccurrences(of: "\n", with: "\\n")
You don't need to process the data returned at all if the result is simply a text (string to say welcome someone). My solution below is just loading a JSON file returned (as you mentioned above) and then decode the data to Welcome object below
struct Welcome: Codable {
let ja: String
init(from decoder: Decoder) throws {
let values = try decoder.container(keyedBy: CodingKeys.self)
ja = try values.decodeIfPresent(String.self, forKey: .ja) ?? ""
}
}
func readJsonFile(filename: String) -> String {
guard let fileUrl = Bundle.main.url(forResource: filename, withExtension: "json") else { fatalError() }
guard let jsonData = try? String(contentsOf: fileUrl) else {
return ""
}
return jsonData
}
func parseJsonFile() {
let jsonStr = readJsonFile(filename: "data")
let jsonData: Data = Data(jsonStr.utf8)
let decoder = JSONDecoder()
do {
let welcome = try decoder.decode(Welcome.self, from: jsonData)
print("\(welcome.ja)")
} catch {
print(error.localizedDescription)
}
}
this is the result when I print the welcome.ja's value:
犯罪者の子孫たちが暮らすスラム街。境界線の向こうの人々からは「族民」とさげすまれ、差別を受けていた。孤児だった少年・ルドは、育ての親であるレグトと共にスラム街に住み、常人離れした身体能力を武器に生計を立てていた。だがある日、身に覚えのない罪を着せられ、スラムの人々でさえ恐れる「奈落」へと落とされてしまう……。

unencode json in Zapier

I'm trying to make a Zapier zap to get a JSON from Olark.
This is the sample JSON Olark sends...
{
"kind": "Conversation",
"id": "EV695BI2930A6XMO32886MPT899443414",
"tags": ["olark", "customer"],
"items": [{
"kind": "MessageToVisitor",
"nickname": "John",
"timestamp": "1307116657.1",
"body": "Hi there. Need any help?",
"operatorId": "1234"
},
{
"kind": "MessageToOperator",
"nickname": "Bob",
"timestamp": "1307116661.25",
"body": "Yes, please help me with billing."
}],
"visitor": {
"kind": "Visitor",
"id": "9QRF9YWM5XW3ZSU7P9CGWRU89944341",
"fullName": "Bob Doe",
"emailAddress": "bob#example.com",
"phoneNumber": "(555) 555-5555",
"city": "Palo Alto",
"region": "CA",
"country": "United State",
"countryCode": "US",
"organization": "Widgets Inc.",
"ip": "123.4.56.78",
"browser": "Chrome 12.1",
"operatingSystem": "Windows",
"conversationBeginPage": "http://www.example.com/path",
"customFields": {
"myInternalCustomerId": "12341234",
"favoriteColor": "blue"
},
"chat_feedback": {
"comments": "Very helpful, thanks",
"friendliness": 5,
"knowledge": 5,
"overall_chat": 5,
"responsiveness": 5
}
},
"operators": {
"1234": {
"kind": "Operator",
"id": "1234",
"username": "jdoe",
"nickname": "John",
"emailAddress": "john#example.com"
}
},
"groups": [{
"name": "My Sales Group",
"id": "0123456789abcdef",
"kind": "Group"
}]
}
I can get what I want -- the email -- like this...
var obj = {};
var data = {};
data = JSON.parse(inputData.data);
obj.value = data.visitor["emailAddress"];
return obj;
However, the live data that comes from Olark is an encoded version of the JSON. It looks like this...
raw_body
data=%7B%22kind%22%3A+%22Conversation%22%2C+%22id%22%3A+%224pkhSGlkBYHz0gw83L6TF0UBa6rA39Bo%22%2C+%22manuallySubmitted%22%3A+false%2C+%22items%22%3A+%5B%7B%22kind%22%3A+%22MessageToOperator%22%2C+%22nickname%22%3A+%22juliachevron%40gmail.com%22%2C+%22timestamp%22%3A+%221588965389.388434%22%2C+%22body%22%3A+%22Hi.+I+have+a+stimulator+and+it+is+no+longer+working+at+the+higher+levels%22%2C+%22visitor_nickname%22%3A+%22juliachevron%40gmail.com%22%7D%2C+%7B%22kind%22%3A+%22MessageToVisitor%22%2C+%22nickname%22%3A+%22Christian%22%2C+%22operatorId%22%3A+%22663710%22%2C+%22timestamp%22%3A+%221588965397.901964%22%2C+%22body%22%3A+%22Hi+there%22%7D%2C+%7B%22kind%22%3A+%22MessageToVisitor%22%2C+%22nickname%22%3A+%22Christian%22%2C+%22operatorId%22%3A+%22663710%22%2C+%22timestamp%22%3A+%221588965408.398821%22%2C+%22body%22%3A+%22Can+you+explain+more%3F%22%7D%2C+%7B%22kind%22%3A+%22MessageToOperator%22%2C+%22nickname%22%3A+%22juliachevron%40gmail.com%22%2C+%22timestamp%22%3A+%221588965445.279921%22%2C+%22body%22%3A+%22it+is+only+flashing+up+to+the+2+and+sometimes+3%22%2C+%22visitor_nickname%22%3A+%22juliachevron%40gmail.com%22%7D%2C+%7B%22kind%22%3A+%22MessageToVisitor%22%2C+%22nickname%22%3A+%22Christian%22%2C+%22operatorId%22%3A+%22663710%22%2C+%22timestamp%22%3A+%221588965482.985280%22%2C+%22body%22%3A+%22Try+our+%5C%22oreo%5C%22+troubleshooting+test+from+page+12+of+the+User+Guide%22%7D%2C+%7B%22kind%22%3A+%22MessageToVisitor%22%2C+%22nickname%22%3A+%22Christian%22%2C+%22operatorId%22%3A+%22663710%22%2C+%22timestamp%22%3A+%221588965488.741729%22%2C+%22body%22%3A+%22It+involves+you+pressing+the+wet+sponges+into+each+other+as+you+turn+the+device+to+the+max+setting%22%7D%2C+%7B%22kind%22%3A+%22MessageToOperator%22%2C+%22nickname%22%3A+%22juliachevron%40gmail.com%22%2C+%22timestamp%22%3A+%221588965539.836595%22%2C+%22body%22%3A+%22thank+you%21%22%2C+%22visitor_nickname%22%3A+%22juliachevron%40gmail.com%22%7D%2C+%7B%22kind%22%3A+%22MessageToVisitor%22%2C+%22nickname%22%3A+%22Christian%22%2C+%22operatorId%22%3A+%22663710%22%2C+%22timestamp%22%3A+%221588965583.359044%22%2C+%22body%22%3A+%22This+test+is+to+make+sure+the+device+is+functioning+correctly%22%7D%2C+%7B%22kind%22%3A+%22MessageToVisitor%22%2C+%22nickname%22%3A+%22Christian%22%2C+%22operatorId%22%3A+%22663710%22%2C+%22timestamp%22%3A+%221588965619.075057%22%2C+%22body%22%3A+%22If+you+are+not+getting+to+the+higher+levels+when+the+sponges+are+on+your+temples%2C+it+may+mean+that+you+need+to+use+more+water%2C+tighten+the+headband+a+little+more%2C+or+that+you+need+to+replace+the+sponges%22%7D%2C+%7B%22kind%22%3A+%22MessageToOperator%22%2C+%22nickname%22%3A+%22juliachevron%40gmail.com%22%2C+%22timestamp%22%3A+%221588965698.050212%22%2C+%22body%22%3A+%22it+worked.+thanks%22%2C+%22visitor_nickname%22%3A+%22juliachevron%40gmail.com%22%7D%2C+%7B%22kind%22%3A+%22MessageToVisitor%22%2C+%22nickname%22%3A+%22Christian%22%2C+%22operatorId%22%3A+%22663710%22%2C+%22timestamp%22%3A+%221588965702.713077%22%2C+%22body%22%3A+%22You%27re+very+welcome.%22%7D%5D%2C+%22tags%22%3A+%5B%5D%2C+%22visitor%22%3A+%7B%22kind%22%3A+%22Visitor%22%2C+%22id%22%3A+%22YN7k5dd6nGH52Lvd3L6TF0VBb03B3roI%22%2C+%22fullName%22%3A+%22juliachevron%40gmail.com%22%2C+%22emailAddress%22%3A+%22juliachevron%40gmail.com%22%2C+%22ip%22%3A+%22%22%2C+%22city%22%3A+%22Oak+Park%22%2C+%22region%22%3A+%22IL%22%2C+%22country%22%3A+%22United+States%22%2C+%22countryCode%22%3A+%22US%22%2C+%22organization%22%3A+%22Comcast+Cable%22%2C+%22browser%22%3A+%22Chrome+80.0.3987.149%22%2C+%22operatingSystem%22%3A+%22Macintosh%22%2C+%22referrer%22%3A+%22https%3A%2F%2Fwww.google.com%2F%22%2C+%22conversationBeginPage%22%3A+%22https%3A%2F%2Fwww.fisherwallace.com%2F%22%2C+%22chat_feedback%22%3A+%7B%7D%7D%2C+%22operators%22%3A+%7B%22663710%22%3A+%7B%22kind%22%3A+%22Operator%22%2C+%22id%22%3A+%22663710%22%2C+%22nickname%22%3A+%22Christian%22%2C+%22emailAddress%22%3A+%22christian%40fisherwallace.com%22%2C+%22username%22%3A+%22fisherwallace%22%7D%7D%7D
And I get "SyntaxError: Unexpected token d in JSON at position 0" when I try to parse it with the code that works on the sample JSON.
Is there a way in Zapier to unencode the JSON?
There sure is! What you're seeing is percent encoding. Node.js can decode that, but to get valid json, you'll also need to replace the + characters with spaces.
Try this:
const inputWithReplacedSpaces = inputData.data.replace(/\+/g, '%20') // '%20' is a space
const jsonStr = decodeURIComponent(inputWithReplacedSpaces)
const data = JSON.parse(jsonStr)
return { email: data.visitor.emailAddress }
There were two issues. See screenshot.
I needed to set the Import Data to "data" in the Custom Run Javascript step. And also remove the "data=" from the string at the beginning of the Raw Body.

How to map Nested Array of Objects using Swift Object Mapper?

I am trying to map an array of objects using Object Mapper
I have this code so far, and my mapping is not successful
do {
if let data = data, let sectorData = Mapper<SubSectorsModel>().mapArrayOfArrays(JSONObject: try JSONSerialization.data(withJSONObject: data, options: [])) {
completionHandler(sectorData,(response as! HTTPURLResponse), error)
print("SectionData Received Successfully")
}
} catch {
completionHandler(nil,(response as! HTTPURLResponse), error)
print("Error parsing json get sector data: ", error.localizedDescription)
}
My Json data is as follows:
[
{
"SECTOR_NAME": "MANUFACTURERS",
"ID": "8",
"SECTOR": [
{
"ID": "144",
"NAME": "Biomass Processing"
},
{
"ID": "8",
"NAME": "Servicing engines and motors"
},
{
"ID": "23",
"NAME": "Furniture & fittings"
},
{
"ID": "31",
"NAME": "Fabrics & textiles"
},
{
"ID": "20",
"NAME": "Hand and machine tools"
},
{
"ID": "28",
"NAME": "Safety and security products"
},
{
"ID": "147",
"NAME": "Jewellery"
},
{
"ID": "156",
"NAME": "Beverages"
},
{
"ID": "165",
"NAME": "Stationery"
},
{
"ID": "9",
"NAME": "Industrial equipment"
},
{
"ID": "25",
"NAME": "Cleaning equipment"
},
{
"ID": "33",
"NAME": "Household consumer products"
},
{
"ID": "162",
"NAME": "Paper Products"
},
{
"ID": "170",
"NAME": "Memoribilia"
},
{
"ID": "143",
"NAME": "Food Products"
},
{
"ID": "22",
"NAME": "Automotive aviation marine and rail products"
},
{
"ID": "30",
"NAME": "Household appliances"
},
{
"ID": "151",
"NAME": "Iron Sheet"
},
{
"ID": "167",
"NAME": "Cosmetics"
},
{
"ID": "11",
"NAME": "Fuel Lubricants & Detergents"
},
{
"ID": "19",
"NAME": "Electrical appliances and equipment"
},
{
"ID": "27",
"NAME": "Packaging products"
},
{
"ID": "7",
"NAME": "Engines & parts"
},
{
"ID": "24",
"NAME": "Glass products"
},
{
"ID": "32",
"NAME": "Clothing & footwear"
},
{
"ID": "152",
"NAME": "Building Material"
},
{
"ID": "142",
"NAME": "Food Processing and Packaging"
},
{
"ID": "21",
"NAME": "Plastic products"
},
{
"ID": "29",
"NAME": "Pool & garden products"
},
{
"ID": "157",
"NAME": "Steel Products"
},
{
"ID": "138",
"NAME": "Optical Prescription Lenses"
},
{
"ID": "10",
"NAME": "Servicing & refurbishing"
},
{
"ID": "18",
"NAME": "Chemical"
},
{
"ID": "26",
"NAME": "Board paper and"
}
]
},
.
.
.
]
If you just want to send completionHandler anyway, use code below:
struct MyJsonStruct : Decodable {
struct SectorStruct : Decodable {
var ID : String
var NAME : String
}
var SECTOR_NAME : String
var ID : String
var SECTOR : [SectorStruct]
}
func handle(_ data : Data ) {
do {
let sectorData = try JSONDecoder().decode(MyJsonStruct.self, from: data) as MyJsonStruct
let yourArray = sectorData.SECTOR // if you need an array result
completionHandler(sectorData,(response as! HTTPURLResponse), error)
print("SectionData Received Successfully")
} catch {
completionHandler(nil,(response as! HTTPURLResponse), error)
print("Error parsing json get sector data: ", error.localizedDescription)
}
}

Grab an ID from particular array index

I have some JSON which returns the following:
"data": [
{
"created_time": "2010-09-03T16:07:14+0000",
"name": "Profile Pictures",
"id": "125287297520173"
},
{
"created_time": "2010-12-03T00:05:31+0000",
"name": "Mobile Uploads",
"id": "146617845387118"
},
{
"created_time": "2013-07-27T11:34:50+0000",
"name": "Timeline Photos",
"id": "546011742114391"
},
{
"created_time": "2017-01-04T19:02:40+0000",
"name": "Untitled Album",
"id": "1178578645524361"
},
{
"created_time": "2016-09-10T18:26:25+0000",
"name": "Untitled Album",
"id": "1076646985717528"
},
{
"created_time": "2016-07-06T18:27:09+0000",
"name": "OS X Photos",
"id": "1033031426745751"
},
{
"created_time": "2013-06-22T07:32:01+0000",
"name": "iOS Photos",
"id": "530462737002625"
},
{
"created_time": "2012-05-22T19:01:42+0000",
"name": "Cover Photos",
"id": "370987619616805"
},
{
"created_time": "2015-08-27T18:59:56+0000",
"name": "Untitled Album",
"id": "879780692070826"
},
{
"created_time": "2014-12-06T16:13:01+0000",
"name": "DRMC 2005 Batch",
"id": "761469943901902"
},
{
"created_time": "2013-06-16T09:01:17+0000",
"name": "Instagram Photos",
"id": "528368577212041"
},
{
"created_time": "2012-09-09T17:37:55+0000",
"name": "Liverpool Exclusive",
"id": "416230538425846"
},
{
"created_time": "2012-09-10T16:31:52+0000",
"name": "LIVERPOOL FC TOUR",
"id": "416540875061479"
},
{
"created_time": "2010-06-11T19:37:20+0000",
"name": "cars",
"id": "104577376257832"
},
{
"created_time": "2011-03-29T23:50:18+0000",
"name": "Camera+ Photos",
"id": "174268382622064"
}
]
I would like to grab the id of the field "name": "Profile Pictures" . I am using SwiftyJSON to cast the types and so far I managed to do this :
let dictionary = JSON(result)
// print("albums **************\(dictionary)")
if let data = dictionary["data"].array {
print("data of profilePicture ******* \(data)")
let index = data.index{ $0["name"] == "Profile Pictures" }
}
}
I could detech the name of profilePicture as you can see but now I want to grab the id of that json object. Please help.
Use filter to get the dictionary you're interested in…
let dictionary = JSON(result)
// print("albums **************\(dictionary)")
if let data = dictionary["data"].array {
print("data of profilePicture ******* \(data)")
if let dict = data.filter{ $0["name"] == "Profile Pictures" }.first as? [String: String] {
let id = dict["id"]
}
}
let dictionary = JSON(result)
// print("albums ID are **************\(dictionary)")
if let data = dictionary["data"].array {
print("data of profilePicture ******* \(data)")
if let dict = data.first(where: { ($0["name"].string ) == "Profile Pictures" }) {
let id = dict["id"]
print("my desired id : ********* \(id)")
}
}

Extracting usable data from an API response in Swift

I picked up a project from a previous dev that kind of left a mess. We've moved to a new API with a better structure and I'm very confused as to how I can get this to work.
I've got an API that I'm trying to parse the data from so it's in a usable form and I'm new enough that I could use some help.
Could someone take a look at the following code and kind of guide me and explain what's going on here andy maybe how to get it to work properly? I've been beating my head against the wall on this one for a few days now.
Here's a sample of the JSON response that I'm getting:
{
“Green Shirt": [
{
"id": "740",
"name": “Nice Green Shirt",
"quantity": "0",
"make": "",
"model": "",
"price": “15.00",
"size": "XXS",
"sku": null,
"image": "https:\/\/google.com\/green_shirt.jpg",
"new_record": false,
"category_name": "",
"bar_code": "",
},
{
"id": "743",
"name": "Green Shirt",
"quantity": “68",
"make": "",
"model": "",
"price": “20.00",
"size": "XS",
"sku": null,
"image": "https:\/\/google.com\/green_shirt.jpg",
"new_record": false,
"category_name": "",
"bar_code": "",
}
],
“Dark Blue Jeans": [
{
"id": "1588",
"name": "Dark Blue Jeans",
"quantity": "0",
"make": "",
"model": "",
"price": "0.00",
"size": “S",
"sku": null,
"image": "https:\/\/google.com\/dark_blue_jeans.jpg",
"new_record": false,
"category_name": "",
"bar_code": "",
"category": null
},
{
"id": "1559",
"name": "Dark Blue Jeans",
"quantity": "4",
"make": "",
"model": "",
"price": "0.00",
"size": “XL",
"sku": null,
"image": "https:\/\/google.com\/dark_blue_jeans.jpg",
"new_record": false,
"category_name": "",
"bar_code": "",
"category": null
}
],
“White Belt": [
{
"id": "1536",
"name": "White Belt",
"quantity": "37",
"make": "",
"model": "",
"price": "0.00",
"size": "One Size",
"sku": null,
"image": "https:\/\/google.com\/white_belt.jpg",
"new_record": false,
"category_name": "",
"bar_code": "",
"category": null
}
]
}
Class that should take the JSON response
public class Inventory {
public var products = [Product]()
public init(type: Product.Type, data:[[String:AnyObject]]) {
for productData in data {
products.append(type(data: productData))
}
}
}
Product Class
public class Product {
public var data:[String:AnyObject]
required public init(data: [String:AnyObject]) {
self.data = data
}
}
Main Product Class
class MainProduct : Product {
var id:String? {
return data["id"] as? String
}
var name:String {
return data["model"] as! String
}
var sizes:[String:Int] {
if let items = data["quantities"] as? [String:Int] {
return items
} else {
return [String:Int]()
}
}
var upc :String? {
return data["UPC"] as? String
}
var price:Double {
if let price = data["price"] as? Double {
return price
} else if let price = data["price"] as? NSString {
return price.doubleValue
} else {
fatalError("Did not get a price")
}
}
}
You are missing the code needed to load the JSON file and parse it. Also, the JSON you have posted has some illegal characters which will choke the parser. e.g.: { “Green Shirt": [ The first quote is a curly quote. First, you need to clean all of these up.
Then you could save the JSON along with your source files in, say "data.json". If you did that, you could parse the JSON like this:
var error: NSError? = nil
let path = NSBundle.mainBundle().pathForResource("data", ofType: "json")
let jsonData = NSData(contentsOfFile: path!, options: .DataReadingMappedIfSafe, error: &error)
let jsonResult = NSJSONSerialization.JSONObjectWithData(jsonData!, options: NSJSONReadingOptions.AllowFragments, error: &error) as! NSDictionary
Then you could pull the product data from the parsed JSON dictionary into your Product objects.