I writing because I need to search a json-key passed in a function like a string. Do you have any suggestion on how I could implement it? Once I find the key I also need to edit the value. Here there is the code I wrote until now:
JSON:
{
"JSONRoot": {
"version": 1,
"Town": {
"hour": 0,
"latitude": "",
"longitude": 0,
"latitudine": 0
},
"MeasurePoints": {
"MeasurePoint": [
{
"code": "",
"codelocation": "",
}
]
},
"Wakeup": {
"startH": 6,
"startM": 0,
"maxAttempts": 3,
"maxRetry": 10
},
"Config": {
"port": 12345,
"A": {
"writable": true,
"value": 12
},
"B": {
"writable": true,
"value": 8
},
},
"Sales": {
"Stores": {
"Store": [
{
"description": "A description",
"type": "1",
"Floors": {
"basement": true,
"number": 2
},
"Doors": {
"type": "",
"number": 7
},
"Lights": {
"number": 20
}
},
{
"description": "A description",
"type": "4",
"Floors": {
"basement": none,
"number": 1
},
"Doors": {
"type": "",
"number": 4
},
"Lights": {
"number": 8
}
}
]
}
}
}
}
Structs with codable:
// MARK: - JSONConfig
struct JsonConfig: Codable {
let jsonRoot: JSONRoot?
enum CodingKeys: String, CodingKey {
case jsonRoot = "JSONRoot"
}
}
// MARK: - JSONRoot
struct JSONRoot: Codable {
let version: Int?
let measurePoints: MeasurePoints?
let wakeup: Wakeup?
let config: Config?
let sale: Sale?
enum CodingKeys: String, CodingKey {
case version
case measurePoints = "MeasurePoints"
case wakeup = "Wakeup"
case config = "Config"
case sale = "Sale"
}
}
// MARK: - Stores
struct Stores: Codable {
let stores: [Store]?
enum CodingKeys: String, CodingKey {
case stores = "Stores"
}
}
// MARK: - Store
struct Store: Codable {
let storeDescription: String?
let type: Int?
let floors: Floors?
let doors: Doors?
let lights: Lights?
enum CodingKeys: String, CodingKey {
case storeDescription = "description"
case type
case floors = "Floors"
case doors = "Doors"
case lights = "Lights"
}
}
// MARK: - Floors
struct Floors: Codable {
let basement: Bool?
let number: Int?
}
// MARK: - Doors
struct Doors: Codable {
let type: String?
let number: Int?
}
// MARK: - Lights
struct Lights: Codable {
let number: Int?
}
// MARK: - MeasurePoints
struct MeasurePoints: Codable {
let measurePoint: [MeasurePoint]?
enum CodingKeys: String, CodingKey {
case measurePoint = "MeasurePoint"
}
}
// MARK: - MeasurePoint
struct MeasurePoint: Codable {
let code, codeLocation: String?
}
// MARK: - Config
struct Config: Codable {
let port: Int?
let a, b: K?
enum CodingKeys: String, CodingKey {
case port
case a = "A"
case b = "B"
}
}
// MARK: - K
struct K: Codable {
let writable: Bool?
let value: Int?
}
// MARK: - Wakeup
struct Wakeup: Codable {
let startH, startM, maxAttempts, maxRetry: Int?
}
Function to search for a key:
func setKeyValue(jsonKey: String, value: String) {
let decoder = JSONDecoder()
let jsonData = Data(C.jsonString.utf8)
if let jsonResult = try? decoder.decode(JsonConfig.self, from: jsonData) {
// At this point I have the jsonKey = "JSONRoot.Wakeup.maxRetry" but how I can use it to search for
// the key in the jsonResult?
}
}
Obviously I need to create a new struct to edit the json but one step at a time.
Using JSONSerialisation is probably the most straightforward way here
var value: Any?
do {
if let jsonResult = try JSONSerialization.jsonObject(with: data) as? [String: Any] {
let keys = "JSONRoot.Wakeup.maxRetry".split(separator: ".").map {String($0)}
var dict = jsonResult
for i in 0..<keys.count {
if let temp = dict[keys[i]] as? [String:Any] {
dict = temp
continue
}
value = dict[keys[i]]
}
}
} catch {
print(error)
}
Note that this doesn't support arrays but a solution for that is very dependent on how the search key syntax would handle an array
If my thinking is correct as you, you can try with this code.
override func viewDidLoad() {
super.viewDidLoad()
let jsonString = """
{
"JSONRoot": {
"version": 1,
"Town": {
"hour": 0,
"latitude": "",
"longitude": 0,
"latitudine": 0
},
"MeasurePoints": {
"MeasurePoint": [{
"code": "",
"codelocation": ""
}]
},
"Wakeup": {
"startH": 6,
"startM": 0,
"maxAttempts": 3,
"maxRetry": 10
},
"Config": {
"port": 12345,
"A": {
"writable": true,
"value": 12
}
},
"Sales": {
"Stores": {
"Store": [{
"description": "A description",
"type": "1",
"Floors": {
"basement": true,
"number": 2
},
"Doors": {
"type": "",
"number": 7
},
"Lights": {
"number": 20
}
},
{
"description": "A description",
"type": "4",
"Floors": {
"basement": "none",
"number": 1
},
"Doors": {
"type": "",
"number": 4
},
"Lights": {
"number": 8
}
}
]
}
}
}
}
"""
editJson(jsonString)
}
func editJson(_ jsonString: String) {
do{
let jsonData = Data(jsonString.utf8)
var jsonObject = try JSONSerialization.jsonObject(with: jsonData)
parseDict(&jsonObject)
print("jsonObject: \(String(describing: jsonObject))")
}catch let error {
print(error.localizedDescription)
}
}
func parseDict(_ jsonObject: inout Any) {
if let _ = jsonObject as? String {
return
} else if var dictionary = jsonObject as? Dictionary<String, Any> {
for (key, value) in dictionary {
var nextObject = value
parseDict(&nextObject)
if let value = getValueWith(key), let _ = dictionary.removeValue(forKey: key) {
dictionary[key] = value
} else {
dictionary[key] = nextObject
}
}
jsonObject = dictionary
}else if let array = jsonObject as? Array<Any> {
var updatedArray = array
for (index, value) in array.enumerated() {
var nextObject = value
parseDict(&nextObject)
updatedArray[index] = nextObject
}
jsonObject = updatedArray
}
}
func getValueWith(_ key: String) -> String? {
return [
"description" : "Amit (amitpstu1#gmail.com) ... so on"
][key]
}
You can refresh your memory or learn more here:
https://developer.apple.com/documentation/foundation/archives_and_serialization/using_json_with_custom_types
You would be looking at merge json from different depths section. Using encodable extension etc.
You could also look here: In Swift, can one use a string to access a struct property? If you want to roll your own search function, like a modified dfs or something.
Related
I'm using JSONDecoder to decode incoming websocket messages from an API. Messages come from the websockettask as a String. Right now I have my Codable struct as such:
struct JsonRPCMessage: Codable {
let jsonrpc: String
let result: String?
let method: String?
let id: Int?
}
Then I just decode it like:
let message = try decoder.decode(JsonRPCMessage.self, from: data!)
This has worked fine for about half of the endpoints in the API which just return a single String for result. The others return a dictionary. When I change the type of result to Dictionary, the struct no longer conforms to Codable. When it's left as a string, the decoder returns a type mismatch error at runtime. Plus, changing the type to dictionary would break functionality for the rest of the api's features.
Looking for ideas to decode and access the string to value pairs in that dictionary as well as check for dictionary or string before sending it to the decoder.
Here are some samples of the different types of response I need to be able to sort and parse:
{
"jsonrpc": "2.0",
"result": {
"klippy_connected": true,
"klippy_state": "ready",
"components": [
"klippy_connection",
"history",
"octoprint_compat",
"update_manager"
],
"failed_components": [],
"registered_directories": [
"config",
"logs",
"gcodes",
"config_examples",
"docs"
],
"warnings": [],
"websocket_count": 4,
"moonraker_version": "v0.7.1-659-gf047167",
"missing_klippy_requirements": [],
"api_version": [1, 0, 5],
"api_version_string": "1.0.5"
},
"id": 50
}
{
"jsonrpc": "2.0",
"method": "notify_proc_stat_update",
"params": [
{
"moonraker_stats": {
"time": 1663016434.5099802,
"cpu_usage": 0.74,
"memory": 35716,
"mem_units": "kB"
},
"cpu_temp": null,
"network": {
"lo": { "rx_bytes": 2568, "tx_bytes": 2568, "bandwidth": 0.0 },
"tunl0": { "rx_bytes": 0, "tx_bytes": 0, "bandwidth": 0.0 },
"ip6tnl0": { "rx_bytes": 0, "tx_bytes": 0, "bandwidth": 0.0 },
"eth0": {
"rx_bytes": 2529302,
"tx_bytes": 13891023,
"bandwidth": 7005.14
}
},
"system_cpu_usage": {
"cpu": 25.62,
"cpu0": 1.98,
"cpu1": 1.0,
"cpu2": 0.0,
"cpu3": 100.0
},
"system_memory": {
"total": 8039920,
"available": 7182640,
"used": 857280
},
"websocket_connections": 4
}
]
}
{
"jsonrpc": "2.0",
"result": "ok",
"id": 50
}
In this case, the better option is to receive the same JSON in each case, but if you can't control that then you can implement custom decoding using init(from:).
struct JsonRPCMessage: Decodable {
enum CodingKeys: String, CodingKey {
case jsonrpc, result, method, id
}
let jsonrpc: String
let result: String?
let method: String?
let id: Int?
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
self.jsonrpc = try container.decode(String.self, forKey: .jsonrpc)
if let dic = try container.decodeIfPresent([String: Any].self, forKey: .result) {
self.result = dic["YourKey"] as? String
}
else {
self.result = try container.decodeIfPresent(String.self, forKey: .result)
}
// If you have a custom type for Result than
if let result = try container.decodeIfPresent(YourResultType.self, forKey: .result) {
self.result = result.propertyOfYourResult
}
else {
self.result = try container.decodeIfPresent(String.self, forKey: .result)
}
self.method = try container.decodeIfPresent(String.self, forKey: .method)
self.id = try container.decodeIfPresent(Int.self, forKey: .id)
}
}
You're trying to force 3 different JSON shapes into the same Swift struct. This is generally not advised. You could do custom decoding as #Nirav suggested, but then you're essentially adding business logic decoding to your models and this will quickly grow out of hand and be untestable.
I believe a far better solution is to create 3 different struct and try to decode one, if not, try decoding the other, etc... and handle any error as appropriate, and test this behaviour:
import Foundation
import SwiftUI
let json1 = """
{
"jsonrpc": "2.0",
"result": {
"klippy_connected": true,
"klippy_state": "ready",
"components": [
"klippy_connection",
"history",
"octoprint_compat",
"update_manager"
],
"failed_components": [],
"registered_directories": [
"config",
"logs",
"gcodes",
"config_examples",
"docs"
],
"warnings": [],
"websocket_count": 4,
"moonraker_version": "v0.7.1-659-gf047167",
"missing_klippy_requirements": [],
"api_version": [1, 0, 5],
"api_version_string": "1.0.5"
},
"id": 50
}
""".data(using: .utf8)!
struct JSON1: Codable {
var jsonrpc: String
var result: JSONResult
var id: Int
struct JSONResult: Codable {
var klippy_connected: Bool
var klippy_state: String
var components: [String]
var failed_components: [String]
var registered_directories: [String]
// etc...
}
}
let json2 = """
{
"jsonrpc": "2.0",
"method": "notify_proc_stat_update",
"params": [
{
"moonraker_stats": {
"time": 1663016434.5099802,
"cpu_usage": 0.74,
"memory": 35716,
"mem_units": "kB"
},
"cpu_temp": null,
"network": {
"lo": { "rx_bytes": 2568, "tx_bytes": 2568, "bandwidth": 0.0 },
"tunl0": { "rx_bytes": 0, "tx_bytes": 0, "bandwidth": 0.0 },
"ip6tnl0": { "rx_bytes": 0, "tx_bytes": 0, "bandwidth": 0.0 },
"eth0": {
"rx_bytes": 2529302,
"tx_bytes": 13891023,
"bandwidth": 7005.14
}
},
"system_cpu_usage": {
"cpu": 25.62,
"cpu0": 1.98,
"cpu1": 1.0,
"cpu2": 0.0,
"cpu3": 100.0
},
"system_memory": {
"total": 8039920,
"available": 7182640,
"used": 857280
},
"websocket_connections": 4
}
]
}
""".data(using: .utf8)!
struct JSON2: Codable {
var jsonrpc: String
var params: [JSONParams]
var method: String
struct JSONParams: Codable {
var moonraker_stats: MoonrakerStats
// etc...
struct MoonrakerStats: Codable {
var time: Double
var cpu_usage: Double
// etc...
}
}
}
let json3 = """
{
"jsonrpc": "2.0",
"result": "ok",
"id": 50
}
""".data(using: .utf8)!
struct JSON3: Codable {
var jsonrpc: String
var result: String
var id: Int
}
let data = [json1, json2, json3].randomElement()!
let decoder = JSONDecoder()
if let decoded = try? decoder.decode(JSON1.self, from: data) {
print("we have json 1")
print(decoded)
} else if let decoded = try? decoder.decode(JSON2.self, from: data) {
print("we have json 2")
print(decoded)
} else if let decoded = try? decoder.decode(JSON3.self, from: data) {
print("we have json 3")
print(decoded)
} else {
print("we don't know what we have")
}
I'm accessing the data from an API with XCode(10.2.1) and Swift(5.0) and ran into a problem I cannot seem to find the answer to. I am able to get data from all other parts of the API apart from one, which has been named as a number string "750", im not sure how to grab that data when I can't use a variable that jsonDecoder can read?
This is an example of what I know won't work but gives you an idea of what I'm trying to do.
class Images: Codable {
let "750": String
init("750": String){
self."750" = "750"
}
}
Here's a snippet from the API I'm trying to get the images from:
"id": "069f7f26",
"sku": "AP",
"title": "Pizza",
"description": "A really great pizza",
"list_price": "9.95",
"is_vatable": true,
"is_for_sale": false,
"age_restricted": false,
"box_limit": 2,
"always_on_menu": false,
"volume": null,
"zone": null,
"created_at": "2017-03-06T10:52:43+00:00",
"attributes": [
{
"id": "670f0e7c",
"title": "Allergen",
"unit": null,
"value": "Products manufactured in a nut environment"
},
{
"id": "c29e7",
"title": "Weight",
"unit": "g",
"value": "300"
}
],
"tags": [
],
"images": {
"750": {
"src": "https:\/\/some_website.co.uk\/cms\/product_image\some_image.jpg",
"url": "https:\/\/some_website.co.uk\/cms\/product_image\some_image.jpg",
"width": 750
}
}
},
I setup an example that better match your situation in order to give you an overview on how to parse and access your JSON information dynamically with a Dictionary data type:
import Foundation
let jsonData = """
{
"images": {
"750": {
"src": "https:\\/\\/some_website.co.uk/cms/product_image/some_image.jpg",
"url": "https:\\/\\/some_website.co.uk/cms/product_image/some_image.jpg",
"width": 750
}
}
}
"""
let json = jsonData.data(using: .utf8)!
public struct Results: Codable {
public var images: [String:Image] = [:]
enum CodingKeys: String, CodingKey {
case images = "images"
}
}
public struct Image: Codable {
public var src: String = ""
public var url: String = ""
public var width: Int = 0
enum CodingKeys: String, CodingKey {
case src = "src"
case url = "url"
case width = "width"
}
}
if let results = try? JSONDecoder().decode(Results.self, from: json) {
let imagesDict = results.images
for (key, value) in imagesDict {
print("Key: \(key)")
print("Value: \(value)")
}
}
If you try this snippet it will give you this output printed:
Key: 750
Value: Image(src: "https://some_website.co.uk/cms/product_image/some_image.jpg", url: "https://some_website.co.uk/cms/product_image/some_image.jpg", width: 750)
You can try out the snippet above online, if you copy paste it here and run it: http://online.swiftplayground.run/
### UPDATE (in response to comment)
In response to your comment, I found it easier to setup another example to show you how you can achieve that with your exact code sample that you shared in the comment itself.
I left everything as class and just added images in order to leave you an overview on how to achieve that.
In the end, I'd suggest to rename Products and Attributes into Product and Attribute. Also if there is no strong reason on why you choosed the model to be class, just change them to struct and as well if there is no strong reasons to keep most of the attributes of each model optional give them a default value as I did in the example above if you are always expecting some values/attributes to be there.
You can try and run this snippet as well in http://online.swiftplayground.run to try it out:
import Foundation
let jsonData = """
{
"data": [
{
"title": "titlex",
"description": "descx",
"list_price": "123,456",
"attributes": [
{
"title": "titlex",
"unit": "unitx",
"value": "valuex"
}
],
"images": {
"750": {
"src": "https:\\/\\/some_website.co.uk/cms/product_image/some_image.jpg",
"url": "https:\\/\\/some_website.co.uk/cms/product_image/some_image.jpg",
"width": 750
}
}
}
]
}
"""
let json = jsonData.data(using: .utf8)!
class AllProducts: Codable {
let data: [Products]
init(data: [Products]) {
self.data = data
}
}
class Products: Codable {
let title: String?
let description: String?
let list_price: String?
let attributes: [Attributes]?
let images: [String:Image]?
init(title: String, description: String, list_price: String, attributes: [Attributes], images: [String:Image]) {
self.title = title
self.description = description
self.list_price = list_price
self.attributes = attributes
self.images = images
}
}
class Attributes: Codable {
let title: String?
let unit: String?
let value: String?
init(title: String, unit: String, value: String) {
self.title = title
self.unit = unit
self.value = value
}
}
class Image: Codable {
let src: String?
let url: String?
let width: Int?
init(src: String, url: String, width: Int) {
self.src = src
self.url = url
self.width = width
}
}
// parsing/decoding
if let results = try? JSONDecoder().decode(AllProducts.self, from: json) {
if let imagesDict = results.data[0].images {
// there is an "images" for product at position 0 (the only one in my json example)
for (key, value) in imagesDict {
print("Key: \(key)")
print("Value src: \(value.src)")
print("Value url: \(value.url)")
print("Value width: \(value.width)")
}
}
}
Output
Key: 750
Value src: Optional("https://some_website.co.uk/cms/product_image/some_image.jpg")
Value url: Optional("https://some_website.co.uk/cms/product_image/some_image.jpg")
Value width: Optional(750)
I am trying to parse json data in decodable in swift 4. It prints nil value. i could not find what issue is this?
here is model class:
public struct TaskID: Decodable {
let embedded: Embedded?
let count: Int?
enum CodingKeys: String, CodingKey {
case count = "count"
case embedded = "_embedded"
}
}
public struct Embedded: Decodable {
let task: [Task]?
enum CodingKeys: String, CodingKey {
case task = "task"
}
}
public struct Task : Decodable {
let id : String?
let name: String?
let assignee: String?
let created: String?
let processDefinitionId: String?
enum CodingKeys: String, CodingKey {
case id = "id"
case name = "name"
case assignee = "assignee"
case created = "created"
case processDefinitionId = "processDefinitionId"
}
}
Here is json:
{
"_links": {
"self": {
"href": "/task"
}
},
"_embedded": {
"task": [
{
"_links": {
"assignee": {
"href": "/user/demo"
},
"execution": {
"href": "/execution/1b64cf75-0616-11ea-8860-120ef5ab2c25"
},
"identityLink": {
"href": "/task/1b64f688-0616-11ea-8860-120ef5ab2c25/identity-links"
},
"processDefinition": {
"href": "/process-definition/quickEvaluation:1:129ce2b1-0616-11ea-8860-120ef5ab2c25"
},
"processInstance": {
"href": "/process-instance/1b64cf75-0616-11ea-8860-120ef5ab2c25"
},
"self": {
"href": "/task/1b64f688-0616-11ea-8860-120ef5ab2c25"
}
},
"_embedded": {
"variable": []
},
"id": "1b64f688-0616-11ea-8860-120ef5ab2c25",
"name": "Quick Evaluation",
"assignee": "demo",
"created": "2019-11-13T13:04:20.687+0000",
"due": null,
"followUp": null,
"delegationState": null,
"description": null,
"executionId": "1b64cf75-0616-11ea-8860-120ef5ab2c25",
"owner": null,
"parentTaskId": null,
"priority": 50,
"processDefinitionId": "quickEvaluation:1:129ce2b1-0616-11ea-8860-120ef5ab2c25",
"processInstanceId": "1b64cf75-0616-11ea-8860-120ef5ab2c25",
"taskDefinitionKey": "QuickEvaluation",
"caseExecutionId": null,
"caseInstanceId": null,
"caseDefinitionId": null,
"suspended": false,
"formKey": "a8apps:suryoday:gng:v0.1.0:kycUpload",
"tenantId": null
},
{
"_links": {
"assignee": {
"href": "/user/demo"
},
"execution": {
"href": "/execution/412a03b7-06ae-11ea-8860-120ef5ab2c25"
},
"identityLink": {
"href": "/task/412a2aca-06ae-11ea-8860-120ef5ab2c25/identity-links"
},
"processDefinition": {
"href": "/process-definition/quickEvaluation:1:129ce2b1-0616-11ea-8860-120ef5ab2c25"
},
"processInstance": {
"href": "/process-instance/412a03b7-06ae-11ea-8860-120ef5ab2c25"
},
"self": {
"href": "/task/412a2aca-06ae-11ea-8860-120ef5ab2c25"
}
},
"_embedded": {
"variable": [
{
"_links": {
"self": {
"href": "/process-instance/412a03b7-06ae-11ea-8860-120ef5ab2c25/variables/loanAmount"
}
},
"_embedded": null,
"name": "loanAmount",
"value": "650000",
"type": "String",
"valueInfo": {}
},
{
"_links": {
"self": {
"href": "/process-instance/412a03b7-06ae-11ea-8860-120ef5ab2c25/variables/firstName"
}
},
"_embedded": null,
"name": "firstName",
"value": "Kamesh",
"type": "String",
"valueInfo": {}
}
]
},
"id": "412a2aca-06ae-11ea-8860-120ef5ab2c25",
"name": "Quick Evaluation",
"assignee": "demo",
"created": "2019-11-14T07:13:27.558+0000",
"due": null,
"followUp": null,
"delegationState": null,
"description": null,
"executionId": "412a03b7-06ae-11ea-8860-120ef5ab2c25",
"owner": null,
"parentTaskId": null,
"priority": 50,
"processDefinitionId": "quickEvaluation:1:129ce2b1-0616-11ea-8860-120ef5ab2c25",
"processInstanceId": "412a03b7-06ae-11ea-8860-120ef5ab2c25",
"taskDefinitionKey": "QuickEvaluation",
"caseExecutionId": null,
"caseInstanceId": null,
"caseDefinitionId": null,
"suspended": false,
"formKey": "a8apps:suryoday:gng:v0.1.0:kycUpload",
"tenantId": null
}
]
},
"count": 13
}
Here is urlrequest:
// MARK: - URLRequestConvertible
func asURLRequest() throws -> URLRequest {
let url = try K.ProductionServer.baseURL.asURL()
var urlRequest = URLRequest(url: url.appendingPathComponent(path))
print(urlRequest)
// HTTP Method
urlRequest.httpMethod = method.rawValue
let authToken = UserDefaults.standard.string(forKey: "authToken")
let bearerToken: String = "Bearer " + (authToken ?? "")
print("baearer token::\(bearerToken)")
// Common Headers
urlRequest.setValue(ContentType.json.rawValue, forHTTPHeaderField: HTTPHeaderField.acceptType.rawValue)
urlRequest.setValue(ContentType.json.rawValue, forHTTPHeaderField: HTTPHeaderField.contentType.rawValue)
urlRequest.setValue(bearerToken, forHTTPHeaderField: HTTPHeaderField.authentication.rawValue)
// Parameters
if let parameters = parameters {
do {
urlRequest.httpBody = try JSONSerialization.data(withJSONObject: parameters, options: [])
} catch {
throw AFError.parameterEncodingFailed(reason: .jsonEncodingFailed(error: error))
}
}
return urlRequest
}
Here is alamofire request:
import Foundation
import Alamofire
public class APIClient {
#discardableResult
private static func performRequest<T:Decodable>(route:APIRouter, decoder: JSONDecoder = JSONDecoder(), completion:#escaping (AFResult<T>)->Void) -> DataRequest {
return AF.request(route)
.responseDecodable (decoder: decoder){ (response: AFDataResponse<T>) in
completion(response.result)
print("framework response::",response.result)
}
}
public static func taskID(id: String, completion:#escaping (AFResult< [TaskID]>)->Void) {
performRequest(route: APIRouter.TaskById(id: id), completion: completion)
}
}//APIClient
Initially it shows Dictionary, Swift.DecodingError.Context(codingPath: [], debugDescription: "Expected to decode Dictionary but found an array instead. but now i'm getting nil in console log. Do not know why i'm getting nil value. Is struct is correct based on my json response? I'm struggling with getting the nested data.
Any help much appreciated pls..
Your struct is correct. See playground below for proof; I don't get an error. nil is very common result when you don't have the entire data set and you assume that some optional field in the response is non optional just because you see it in your sample data (your sample is not necessarily representative). Absent an actual spec from the server, you need to figure out what field is failing to decode and you may need to get a bunch of data to figure out which fields are really optional. You can do that by either putting in error handling code in your AF project above or just by pasting your response into my playground below. Either way the decoding error should tell you which field was not present.
import PlaygroundSupport
import UIKit
import WebKit
public struct TaskID: Decodable {
let embedded: Embedded?
let count: Int?
enum CodingKeys: String, CodingKey {
case count = "count"
case embedded = "_embedded"
}
}
public struct Embedded: Decodable {
let task: [Task]?
enum CodingKeys: String, CodingKey {
case task = "task"
}
}
public struct Task : Decodable {
let id : String?
let name: String?
let assignee: String?
let created: String?
let processDefinitionId: String?
enum CodingKeys: String, CodingKey {
case id = "id"
case name = "name"
case assignee = "assignee"
case created = "created"
case processDefinitionId = "processDefinitionId"
}
}
let data = """
{
"_links": {
"self": {
"href": "/task"
}
},
"_embedded": {
"task": [
{
"_links": {
"assignee": {
"href": "/user/demo"
},
"execution": {
"href": "/execution/1b64cf75-0616-11ea-8860-120ef5ab2c25"
},
"identityLink": {
"href": "/task/1b64f688-0616-11ea-8860-120ef5ab2c25/identity-links"
},
"processDefinition": {
"href": "/process-definition/quickEvaluation:1:129ce2b1-0616-11ea-8860-120ef5ab2c25"
},
"processInstance": {
"href": "/process-instance/1b64cf75-0616-11ea-8860-120ef5ab2c25"
},
"self": {
"href": "/task/1b64f688-0616-11ea-8860-120ef5ab2c25"
}
},
"_embedded": {
"variable": []
},
"id": "1b64f688-0616-11ea-8860-120ef5ab2c25",
"name": "Quick Evaluation",
"assignee": "demo",
"created": "2019-11-13T13:04:20.687+0000",
"due": null,
"followUp": null,
"delegationState": null,
"description": null,
"executionId": "1b64cf75-0616-11ea-8860-120ef5ab2c25",
"owner": null,
"parentTaskId": null,
"priority": 50,
"processDefinitionId": "quickEvaluation:1:129ce2b1-0616-11ea-8860-120ef5ab2c25",
"processInstanceId": "1b64cf75-0616-11ea-8860-120ef5ab2c25",
"taskDefinitionKey": "QuickEvaluation",
"caseExecutionId": null,
"caseInstanceId": null,
"caseDefinitionId": null,
"suspended": false,
"formKey": "a8apps:suryoday:gng:v0.1.0:kycUpload",
"tenantId": null
},
{
"_links": {
"assignee": {
"href": "/user/demo"
},
"execution": {
"href": "/execution/412a03b7-06ae-11ea-8860-120ef5ab2c25"
},
"identityLink": {
"href": "/task/412a2aca-06ae-11ea-8860-120ef5ab2c25/identity-links"
},
"processDefinition": {
"href": "/process-definition/quickEvaluation:1:129ce2b1-0616-11ea-8860-120ef5ab2c25"
},
"processInstance": {
"href": "/process-instance/412a03b7-06ae-11ea-8860-120ef5ab2c25"
},
"self": {
"href": "/task/412a2aca-06ae-11ea-8860-120ef5ab2c25"
}
},
"_embedded": {
"variable": [
{
"_links": {
"self": {
"href": "/process-instance/412a03b7-06ae-11ea-8860-120ef5ab2c25/variables/loanAmount"
}
},
"_embedded": null,
"name": "loanAmount",
"value": "650000",
"type": "String",
"valueInfo": {}
},
{
"_links": {
"self": {
"href": "/process-instance/412a03b7-06ae-11ea-8860-120ef5ab2c25/variables/firstName"
}
},
"_embedded": null,
"name": "firstName",
"value": "Kamesh",
"type": "String",
"valueInfo": {}
}
]
},
"id": "412a2aca-06ae-11ea-8860-120ef5ab2c25",
"name": "Quick Evaluation",
"assignee": "demo",
"created": "2019-11-14T07:13:27.558+0000",
"due": null,
"followUp": null,
"delegationState": null,
"description": null,
"executionId": "412a03b7-06ae-11ea-8860-120ef5ab2c25",
"owner": null,
"parentTaskId": null,
"priority": 50,
"processDefinitionId": "quickEvaluation:1:129ce2b1-0616-11ea-8860-120ef5ab2c25",
"processInstanceId": "412a03b7-06ae-11ea-8860-120ef5ab2c25",
"taskDefinitionKey": "QuickEvaluation",
"caseExecutionId": null,
"caseInstanceId": null,
"caseDefinitionId": null,
"suspended": false,
"formKey": "a8apps:suryoday:gng:v0.1.0:kycUpload",
"tenantId": null
}
]
},
"count": 13
}
""".data(using: .utf8)!
let decoder = JSONDecoder()
do {
let decoded = try decoder.decode(TaskID.self, from: data)
print(decoded)
} catch ( let error ) {
print(error.localizedDescription)
}
import Foundation
// MARK: - Task
struct Task: Codable {
let links: VariableLinks?
let embedded: TaskEmbedded?
let count: Int?
enum CodingKeys: String, CodingKey {
case links = "_links"
case embedded = "_embedded"
case count = "count"
}
}
// MARK: - TaskEmbedded
struct TaskEmbedded: Codable {
let task: [TaskElement]?
enum CodingKeys: String, CodingKey {
case task = "task"
}
}
// MARK: - TaskElement
struct TaskElement: Codable {
let links: TaskLinks?
let embedded: TaskEmbeddedClass?
let id: String?
let name: String?
let assignee: String?
let created: String?
let due: String?
let followUp: String?
let delegationState: String?
let taskDescription: String?
let executionId: String?
let owner: String?
let parentTaskId: Int?
let priority: Int?
let processDefinitionId: String?
let processInstanceId: String?
let taskDefinitionKey: String?
let caseExecutionId: Int?
let caseInstanceId: Int?
let caseDefinitionId: Int?
let suspended: Bool?
let formKey: String?
let tenantId: Int?
enum CodingKeys: String, CodingKey {
case links = "_links"
case embedded = "_embedded"
case id = "id"
case name = "name"
case assignee = "assignee"
case created = "created"
case due = "due"
case followUp = "followUp"
case delegationState = "delegationState"
case taskDescription = "description"
case executionId = "executionId"
case owner = "owner"
case parentTaskId = "parentTaskId"
case priority = "priority"
case processDefinitionId = "processDefinitionId"
case processInstanceId = "processInstanceId"
case taskDefinitionKey = "taskDefinitionKey"
case caseExecutionId = "caseExecutionId"
case caseInstanceId = "caseInstanceId"
case caseDefinitionId = "caseDefinitionId"
case suspended = "suspended"
case formKey = "formKey"
case tenantId = "tenantId"
}
}
// MARK: - TaskEmbeddedClass
struct TaskEmbeddedClass: Codable {
let variable: [Variable]?
enum CodingKeys: String, CodingKey {
case variable = "variable"
}
}
// MARK: - Variable
struct Variable: Codable {
let links: VariableLinks?
let embedded: String?
let name: String?
let value: String?
let type: String?
let valueInfo: ValueInfo?
enum CodingKeys: String, CodingKey {
case links = "_links"
case embedded = "_embedded"
case name = "name"
case value = "value"
case type = "type"
case valueInfo = "valueInfo"
}
}
// MARK: - VariableLinks
struct VariableLinks: Codable {
let linksSelf: SelfClass?
enum CodingKeys: String, CodingKey {
case linksSelf = "self"
}
}
// MARK: - SelfClass
struct SelfClass: Codable {
let href: String?
enum CodingKeys: String, CodingKey {
case href = "href"
}
}
// MARK: - ValueInfo
struct ValueInfo: Codable {
}
// MARK: - TaskLinks
struct TaskLinks: Codable {
let assignee: SelfClass?
let execution: SelfClass?
let identityLink: SelfClass?
let processDefinition: SelfClass?
let processInstance: SelfClass?
let linksSelf: SelfClass?
enum CodingKeys: String, CodingKey {
case assignee = "assignee"
case execution = "execution"
case identityLink = "identityLink"
case processDefinition = "processDefinition"
case processInstance = "processInstance"
case linksSelf = "self"
}
}
Also, if you feeling lazy you can use quicktype.io, As nil is a very common response from decodable, you can you an extension for DataRequest to parse and get the values from the request just like using responseDecodable, here you can use something like this,
extension DataRequest {
fileprivate func decodableResponseSerializer<T: Decodable>() -> DataResponseSerializer<T> {
return DataResponseSerializer { _, response, data, error in
guard error == nil else { return .failure(error!) }
guard let data = data else {
return .failure(AFError.responseSerializationFailed(reason: .inputDataNil))
}
return Result { try newJSONDecoder().decode(T.self, from: data) }
}
}
#discardableResult
fileprivate func responseDecodable<T: Decodable>(queue: DispatchQueue? = nil, completionHandler: #escaping (DataResponse<T>) -> Void) -> Self {
return response(queue: queue, responseSerializer: decodableResponseSerializer(), completionHandler: completionHandler)
}
}
and then you can call it like this
Alamofire.request(url, method:.post, parameters: parameters, headers: headers)
.responseDecodable { (response: DataResponse< Task >) in completion(response.result) }
as you were getting something like this, it only comes when you have a different type of data rather than which you have specified.
Is there anyone can help me fix my model? It seems it does not match with the JSON from API Response.
JSON response from postman
{
"error_code": 0,
"data": [
{
"kode": "001",
"name": "BANK INDONESIA PUSAT JAKARTA"
},
{
"kode": "002",
"name": "PT. BANK RAKYAT INDONESIA (Persero) Tbk."
},
{
"kode": "003",
"name": "BANK EKSPOR INDONESIA"
}
],
"msg": "OK"
}
Last Model Edited:
struct ObjectBank: Codable {
let errorCode: Int
let data: [Bank]
let msg: String
enum CodingKeys : String, CodingKey {
case errorCode = "error_code" , data , msg
}
}
struct Bank: Codable {
let kode: String
let name: String
}
Still got error like this
Store model using alamofire
private static func performRequest<T:Decodable>(route:APIRouter,
decoder: JSONDecoder = JSONDecoder(), completion:#escaping
(Result<T>)->Void) -> DataRequest {
// Alamofire.request(route).responseJSON {
// response in
// print(response)
// }
return Alamofire.request(route).responseJSONDecodable (decoder:
decoder){ (response: DataResponse<T>) in
//print(response)
completion(response.result)
}
}
data is an array not dictionary
let data:[Bank]
//
struct ObjectBank: Codable {
let errorCode: Int
let data: [Bank]
let msg: String
enum CodingKeys : String, CodingKey {
case errorCode = "error_code" , data , msg
}
}
struct Bank: Codable {
let kode: String
let name: String
}
//
do {
let dic = try JSONDecoder().decode(ObjectBank.self,data)
}
catch {
print(error)
}
The structure of your response is ok in principle which you can see using the following Playground:
import Cocoa
let jsonData = """
{
"error_code": 0,
"data": [
{
"kode": "001",
"name": "BANK INDONESIA PUSAT JAKARTA"
},
{
"kode": "002",
"name": "PT. BANK RAKYAT INDONESIA (Persero) Tbk."
},
{
"kode": "003",
"name": "BANK EKSPOR INDONESIA"
}
],
"msg": "OK"
}
""".data(using: .utf8)!
struct ObjectBank: Codable {
let errorCode: Int
let data: [Bank]
let msg: String
enum CodingKeys : String, CodingKey {
case errorCode = "error_code" , data , msg
}
}
struct Bank: Codable {
let kode: String
let name: String
}
do {
let banks = try JSONDecoder().decode(ObjectBank.self, from: jsonData)
print(banks)
} catch {
print(error)
}
This will parse without error. Since I do not know AlamoFire very well I have to assume that there is something going wrong with the type of your completion closure. It will "somehow" have to guess that you want to parse ObjectBank in order to make any sense of your response.
Maybe you would have an easier time with responseData?
I am trying to parse JSON using decodable in Swift 4
The JSON is as below:
{
"_id": {
"$oid": "5afceaa0c1743f37b4ee1cf8"
},
"Con_id": "xxx",
"S_id": "xxx",
"I_Image": [
{
"$binary": {
"base64": "",
"subType": "00"
}
}
],
"T_Agreements": [
{
"Ag_Type": "xxx",
"Ap_Type": "xxx",
"Header": {
"Date": {
"$numberInt": "0"
},
"Company": "xxx",
"O_Code": "xxx",
"Lo": "xxx",
"Completed": true
},
"T_Particular": {
"C_Name": "NA",
"C_Address": "NA",
"C_Landline": "NA",
"ROC": "NA",
"T_Name": "xxx",
"Gender": "M",
"NR": "xxx",
"Dob": "22/08/1977",
"C_No": "xxx",
"T_Address": "xxx",
"S_No": "xxx",
"Sign": "NA",
"Proposed": "xxx",
"Completed": true
},
"D_Agreement": {
"C_Period": {
"P_Yr": {
"$numberInt": "2"
},
"P_Mth": {
"$numberInt": "0"
},
"From": {
"$numberInt": "0"
},
"To": {
"$numberInt": "0"
}
},
"First_Term": {
"R_A": {
"$numberInt": "800000"
},
"From_Date": {
"$numberInt": "0"
},
"To_Date": {
"$numberInt": "0"
}
},
"Second_Term": {
"R_A": "0",
"From_Date": {
"$numberInt": "0"
},
"To_Date": {
"$numberInt": "0"
}
},
"S_D": {
"$numberInt": "800"
},
"S_D_M": {
"$numberInt": "0"
},
"C_F": {
"$numberInt": "0"
},
"D_C_F": {
"$numberInt": "0"
},
"Maintenance_Fee": {
"$numberInt": "0"
},
"Others": {
"Description": "NA",
"O_F": {
"$numberInt": "0"
}
},
"D_C": "NA",
"Completed": true
},
"T_S": {
"$binary": {
"base64": "",
"subType": "00"
}
},
"L_Rep": {
"Name": "xxx",
"Rep_Sig": {
"$binary": {
"base64": "",
"subType": "00"
}
}
},
"Remarks": ""
}
],
"Supp": {
"B_N": "xxx",
"B_O_H": "12",
"C_B_O": "xxx",
"A_S": "Nil",
"F_T_Description": "xxx",
"C_T": "xxx",
"Completed": true
},
"FAdjustment": [
{
"Date": {
"$numberInt": "0"
},
"R_Increased": true,
"Pro_R": {
"$numberInt": "20066660"
},
"A_P_From": {
"$numberInt": "0"
},
"A_P_To": {
"$numberInt": "0"
},
"E_Date": {
"$numberInt": "0"
},
"A_Reason": "xxx",
"Req": {
"Name": "xxx",
"Des": "xxx",
"Sig": {
"$binary": {
"base64": "",
"subType": "00"
}
},
"Date": {
"$numberInt": "0"
}
},
"A1": {
"Name": "xxx",
"Des": "xxx",
"Sig": {
"$binary": {
"base64": "",
"subType": "00"
}
},
"Date": {
"$numberInt": "0"
}
},
"A2": {
"Name": "xxx",
"Designation": "xxx",
"Sig": {
"$binary": {
"base64": "",
"subType": "00"
}
},
"Date": {
"$numberInt": "0"
}
}
}
],
"Status": "xxx",
"Re": {
"Out": "0"
},
"Termination": {
"Terminated": false,
"Reason": "NA"
}
}
I am able to easily parse things such as conID with my Struct:
struct Welcome: Codable {
let id: ID?
let conID, sID: String?
let iImage: [IImage]?
let tAgreements: [TAgreement]?
let supp: Supp?
let fAdjustment: [FAdjustment]?
let status: String?
let re: Re?
let termination: Termination?
enum CodingKeys: String, CodingKey {
case id = "_id"
case conID = "Con_id"
case sID = "S_id"
case iImage = "I_Image"
case tAgreements = "T_Agreements"
case supp = "Supp"
case fAdjustment = "FAdjustment"
case status = "Status"
case re = "Re"
case termination = "Termination"
}
}
struct FAdjustment: Codable {
let date: APFrom?
let rIncreased: Bool?
let proR, aPFrom, aPTo, eDate: APFrom?
let aReason: String?
let req, a1, a2: A1?
enum CodingKeys: String, CodingKey {
case date = "Date"
case rIncreased = "R_Increased"
case proR = "Pro_R"
case aPFrom = "A_P_From"
case aPTo = "A_P_To"
case eDate = "E_Date"
case aReason = "A_Reason"
case req = "Req"
case a1 = "A1"
case a2 = "A2"
}
}
struct A1: Codable {
let name, des: String?
let sig: IImage?
let date: APFrom?
let designation: String?
enum CodingKeys: String, CodingKey {
case name = "Name"
case des = "Des"
case sig = "Sig"
case date = "Date"
case designation = "Designation"
}
}
struct APFrom: Codable {
let numberInt: String?
enum CodingKeys: String, CodingKey {
case numberInt = "$numberInt"
}
}
struct IImage: Codable {
let binary: Binary?
enum CodingKeys: String, CodingKey {
case binary = "$binary"
}
}
struct Binary: Codable {
let base64, subType: String?
}
struct ID: Codable {
let oid: String?
enum CodingKeys: String, CodingKey {
case oid = "$oid"
}
}
struct Re: Codable {
let out: String?
enum CodingKeys: String, CodingKey {
case out = "Out"
}
}
struct Supp: Codable {
let bN, bOH, cBO, aS: String?
let fTDescription, cT: String?
let completed: Bool?
enum CodingKeys: String, CodingKey {
case bN = "B_N"
case bOH = "B_O_H"
case cBO = "C_B_O"
case aS = "A_S"
case fTDescription = "F_T_Description"
case cT = "C_T"
case completed = "Completed"
}
}
struct TAgreement: Codable {
let agType, apType: String?
let header: Header?
let tParticular: TParticular?
let dAgreement: DAgreement?
let tS: IImage?
let lRep: LRep?
let remarks: String?
enum CodingKeys: String, CodingKey {
case agType = "Ag_Type"
case apType = "Ap_Type"
case header = "Header"
case tParticular = "T_Particular"
case dAgreement = "D_Agreement"
case tS = "T_S"
case lRep = "L_Rep"
case remarks = "Remarks"
}
}
struct DAgreement: Codable {
let cPeriod: CPeriod?
let firstTerm: FirstTerm?
let secondTerm: SecondTerm?
let sD, sDM, cF, dCF: APFrom?
let maintenanceFee: APFrom?
let others: Others?
let dC: String?
let completed: Bool?
enum CodingKeys: String, CodingKey {
case cPeriod = "C_Period"
case firstTerm = "First_Term"
case secondTerm = "Second_Term"
case sD = "S_D"
case sDM = "S_D_M"
case cF = "C_F"
case dCF = "D_C_F"
case maintenanceFee = "Maintenance_Fee"
case others = "Others"
case dC = "D_C"
case completed = "Completed"
}
}
struct CPeriod: Codable {
let pYr, pMth, from, to: APFrom?
enum CodingKeys: String, CodingKey {
case pYr = "P_Yr"
case pMth = "P_Mth"
case from = "From"
case to = "To"
}
}
struct FirstTerm: Codable {
let rA, fromDate, toDate: APFrom?
enum CodingKeys: String, CodingKey {
case rA = "R_A"
case fromDate = "From_Date"
case toDate = "To_Date"
}
}
struct Others: Codable {
let description: String?
let oF: APFrom?
enum CodingKeys: String, CodingKey {
case description = "Description"
case oF = "O_F"
}
}
struct SecondTerm: Codable {
let rA: String?
let fromDate, toDate: APFrom?
enum CodingKeys: String, CodingKey {
case rA = "R_A"
case fromDate = "From_Date"
case toDate = "To_Date"
}
}
struct Header: Codable {
let date: APFrom?
let company, oCode, lo: String?
let completed: Bool?
enum CodingKeys: String, CodingKey {
case date = "Date"
case company = "Company"
case oCode = "O_Code"
case lo = "Lo"
case completed = "Completed"
}
}
struct LRep: Codable {
let name: String?
let repSig: IImage?
enum CodingKeys: String, CodingKey {
case name = "Name"
case repSig = "Rep_Sig"
}
}
struct TParticular: Codable {
let cName, cAddress, cLandline, roc: String?
let tName, gender, nr, dob: String?
let cNo, tAddress, sNo, sign: String?
let proposed: String?
let completed: Bool?
enum CodingKeys: String, CodingKey {
case cName = "C_Name"
case cAddress = "C_Address"
case cLandline = "C_Landline"
case roc = "ROC"
case tName = "T_Name"
case gender = "Gender"
case nr = "NR"
case dob = "Dob"
case cNo = "C_No"
case tAddress = "T_Address"
case sNo = "S_No"
case sign = "Sign"
case proposed = "Proposed"
case completed = "Completed"
}
}
struct Termination: Codable {
let terminated: Bool?
let reason: String?
enum CodingKeys: String, CodingKey {
case terminated = "Terminated"
case reason = "Reason"
}
}
I'm struggling with getting the nested stuff out.
How do I get things from T_Agreements like Ag_Type out individually and everything from T_Particular out individually?
I have tried
let jsonUrlString = "URL REMOVED REFER TO JSON ABOVE"
guard let url = URL(string: jsonUrlString) else { return }
URLSession.shared.dataTask(with: url) { (data, response, err) in
guard let data = data else { return }
do {
let decoder = JSONDecoder()
let welcome = try! decoder.decode(Welcome.self, from: data)
let tagree = try! decoder.decode(TAgreement.self, from: data)
print(tagree) // This returns all the header inside T_Agreements but the values are all nil
print(tagree.tParticular.cAddress) // This returns nil too
}
}.resume()
Much thanks and appreciation
The line let welcome = try! decoder.decode(Welcome.self, from: data) loads the json data into a Welcome data model, which matches the structure of the json. Trying to reload the same json data into other data models (as you do in the next line) won't work. You need to extract the data directly from the populated data models you've created.
To get TAgreements, you just grab that data from the welcome model:
let tagreements = welcome.tAgreements
To get Ag_Type out, you get the populated data from the first element of tagreements:
let agType = tagreements[0].agType