Array in JSON not parsing with custom decoding function in Swift - json

This question might seem similar to other questions with the same title. However, my decodable is in a function called load.
Here is what my JSON file looks like:
[
{
"id": 1001
"name": "tempName"
"item1": [
{
"id": 1101
"element": "tempElement"
},
{
"id": 1201
"element": "tempElement2"
},
]
}
]
I've built a struct temp to access the information within my JSON file after decoding it. I am not sure if it is extremely relevant but this is what it looks like:
struct Temp: Hashable, Codable, Identifiable {
var id: Int
var name: String
var item1: ExampleItem
}
struct ExampleItem: Hashable, Codable, Identifiable{
var id: Int
var element: String
}
My structs don't seem too crazy so I assume the problem occurs when I am parsing and decoding my JSON. This is the code I am using
let tempData: [Temp] = load("tempData.json")
func load<T: Decodable>(_ filename: String) -> T {
let data: Data
guard let file = Bundle.main.url(forResource: filename, withExtension: nil)
else {
fatalError("Couldn't find \(filename) in main bundle.")
}
do {
data = try Data(contentsOf: file)
} catch {
fatalError("Couldn't load \(filename) from main bundle:\n\(error)")
}
do {
let decoder = JSONDecoder()
return try decoder.decode(T.self, from: data)
} catch {
fatalError("Couldn't parse \(filename) as \(T.self):\n\(error)")
}
}
I think Expected to decode Dictionary<String, Any> but found an array instead bug is occurring when the code tries to decode item1 because item1 is an array. What should I change in my load function?

First of all your JSON is invalid. It should be comma-separated:
[
{
"id": 1001,
"name": "tempName",
"item1": [
{
"id": 1101,
"element": "tempElement"
},
{
"id": 1201,
"element": "tempElement2"
},
]
}
]
Then you need to parse item1 as an array:
struct Temp: Hashable, Codable, Identifiable {
var id: Int
var name: String
var item1: [ExampleItem] // <- declare as an array
}
Note: I suggest renaming item1 to items. For this you need to use CodingKeys (it is as a custom mapping between a model and a JSON):
struct Temp: Hashable, Codable, Identifiable {
enum CodingKeys: String, CodingKey {
case id, name, items = "item1"
}
var id: Int
var name: String
var items: [ExampleItem]
}

Related

How to decode in model when custom object's key missing from json

I have a Json in which there is the possibility that a few keys can be missing
Full JSON looks like this
{
"data": "some text",
"id": "3213",
"title": "title",
"description": "description",
"customObj1": [{
"id": "2423",
"count": 35
........
.......
}]
"customObj2": [{
"number": "2423",
"name": "john"
........
.......
}]
"customObj3": [{
"like": "2423",
"Other": 9
........
.......
}]
}
the custom object (1,2,3) may or may not be available in JSON, How can I write the model in swift using codable and struct?
here is how the model (dummy) look like
// MARK: - Response
struct Response: Codable {
let data, id, title, ResponseDescription: String
let customObj1: [CustomObj1]
let customObj2: [CustomObj2]
let customObj3: [CustomObj3]
enum CodingKeys: String, CodingKey {
case data, id, title
case ResponseDescription = "description"
case customObj1, customObj2, customObj3
}
}
// MARK: - CustomObj1
struct CustomObj1: Codable {
let id: String
let count: Int
}
// MARK: - CustomObj2
struct CustomObj2: Codable {
let number, name: String
}
// MARK: - CustomObj3
struct CustomObj3: Codable {
let like: String
let other: Int
enum CodingKeys: String, CodingKey {
case like
case other = "Other"
}
}
I have tried using decode init but need help
init(from decoder: Decoder) throws {
let values = try decoder.container(keyedBy: CodingKeys.self)
if (values.contains(. customObj1)) {
// here I need help }
else {
self.category = nil
}
}
}

Decode multiple layers of a nested JSON with Swift

I have trouble decoding json data using SwiftUI, I have the following json.
{
"data": [
{
"id": "project:xxxxxx",
"project_manager": {
"employee_id": "employee:xxxxxx",
"id": "employee:xxxxxx",
"person_id": "person: xxxxxx",
"name": "Peter Post"
},
"project_status": {
"id": "projectstatus:xxxxxx",
"label": "active"
},
"created": "2019-01-08 15:39:59",
"modified": "2019-01-24 14:39:13",
"created_at": "2019-01-08 15:39:59",
"updated_at": "2019-01-24 14:39:13",
"url": "https://url.com/projects/project/view?id=000",
...
I'm decoding the json with the following code
import Foundation
struct Projects: Decodable {
let data: [Data]
}
struct Data : Decodable, Identifiable {
let id: String
let url: String
let organization: Organization?
let project_status: ProjectStatus?
}
struct Organization : Decodable, Identifiable {
let id: String?
let name: String?
}
struct ProjectStatus: Decodable, Identifiable {
let id: String?
let label: String?
}
import Foundation
import SwiftUI
import Combine
class NetworkingManager: ObservableObject {
#Published var projectList = Projects(data: [])
init() {
var request = URLRequest(url: URL(string: "https://api-url/projects")!,timeoutInterval: Double.infinity)
request.addValue("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", forHTTPHeaderField: "Authentication-Key")
request.addValue("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", forHTTPHeaderField: "Authentication-Secret")
URLSession.shared.dataTask(with: request) { (data, _, _) in
guard let data = data else { return }
let projectList = try! JSONDecoder().decode(Projects.self, from: data)
DispatchQueue.main.async {
self.projectList = projectList
print(self.projectList)
}
}.resume()
}
}
import SwiftUI
struct ContentView : View {
#ObservedObject var networkingManager = NetworkingManager()
var body: some View {
VStack {
List(networkingManager.projectList.data, id: \.id) { project in
HStack {
Text(project.id)
Text(project.url)
}
}
}
}
}
struct ContentView_Previews : PreviewProvider {
static var previews: some View {
ContentView()
}
}
This results in a list of id and url string values but my question is: How can I list multiple levels of the json files. Do I have to decode each level of the json I want to use or is there a better way?
It was less complicated than I thought. I got a nil value back when I was calling project_status?.label
this was resolved when I called it like this:
project_status?.label ?? self.defaultString

Parsing Array of dictionaries in Swift using Codable

I have a JSON response from an API and I cannot figure out how to convert it into an user object (a single user) using Swift Codable. This is the JSON (with some elements removed for ease of reading):
{
"user": [
{
"key": "id",
"value": "093"
},
{
"key": "name",
"value": "DEV"
},
{
"key": "city",
"value": "LG"
},
{
"key": "country",
"value": "IN"
},
{
"key": "group",
"value": "OPRR"
}
]
}
You could do it in two steps if you want to. First declare a struct for the received json
struct KeyValue: Decodable {
let key: String
let value: String
}
Then decode the json and map the result into a dictionary using the key/value pairs.
do {
let result = try JSONDecoder().decode([String: [KeyValue]].self, from: data)
if let array = result["user"] {
let dict = array.reduce(into: [:]) { $0[$1.key] = $1.value}
Then encode this dictionary into json and back again using a struct for User
struct User: Decodable {
let id: String
let name: String
let group: String
let city: String
let country: String
}
let userData = try JSONEncoder().encode(dict)
let user = try JSONDecoder().decode(User.self, from: userData)
The whole code block then becomes
do {
let decoder = JSONDecoder()
let result = try decoder.decode([String: [KeyValue]].self, from: data)
if let array = result["user"] {
let dict = array.reduce(into: [:]) { $0[$1.key] = $1.value}
let userData = try JSONEncoder().encode(dict)
let user = try decoder.decode(User.self, from: userData)
//...
}
} catch {
print(error)
}
A bit cumbersome but no manual key/property matching is needed.
you could try a struct with like this
struct Model: Codable {
struct User: Codable {
let key: String
let value: String
}
let singleuser: [User]
}
Create a struct with 2 variables key and value
public struct UserModel: Codable {
public struct User: Codable {
public let key: String
public let value: String
}
public let user: [User]
}
After that using JSONDecoder to decode your string.
func decode(payload: String) {
do {
let template = try JSONDecoder().decode(UserModel.self, from: payload.data(using: .utf8)!)
} catch {
print(error)
}
}

Parsing nested JSON using Codable

So I'm trying to parse JSON that looks something like this using Codable in Swift.
{
"abilities": [
{
"ability": {
"name": "chlorophyll",
"url": "https://pokeapi.co/api/v2/ability/34/"
},
"is_hidden": true,
"slot": 3
},
{
"ability": {
"name": "overgrow",
"url": "https://pokeapi.co/api/v2/ability/65/"
},
"is_hidden": false,
"slot": 1
}
],
"name": "SomeRandomName"
}
Now it gets confusing when you're trying to get nested data. Now I'm trying to get the name, which is easy. I'm also trying to get the ability name, this is where its gets complicated for me. After some research this is what I came up with.
class Pokemon: Codable {
struct Ability: Codable {
var isHidden: Bool
struct AbilityObject: Codable {
var name: String
var url: String
}
var ability: AbilityObject
private enum CodingKeys: String, CodingKey {
case isHidden = "is_hidden"
case ability
}
}
var name: String
var abilities: [Ability]
}
Now is there any better way in doing this, or am I stuck doing it like this.
Grab your JSON response and dump it in this site.
It'll generate these structs without Codable. Add Codable so they look like this:
struct Pokemon: Codable {
let abilities: [AbilityElement]
let name: String
struct AbilityElement: Codable {
let ability: Ability
let isHidden: Bool
let slot: Int
struct Ability: Codable {
let name: String
let url: String
}
}
}
For keys with snake_case, you can just declare a JSONDecoder and specify the keyDecodingStrategy as .convertFromSnakeCase. No need to muck around with coding keys if you're just converting from snake case. You only need them if you're renaming keys.
If you have other situations where you need to create custom coding keys for your responses or alter key names, this page should prove helpful.
You can dump this in a playground and play around with it:
let jsonResponse = """
{
"abilities": [
{
"ability": {
"name": "chlorophyll",
"url": "https://pokeapi.co/api/v2/ability/34/"
},
"is_hidden": true,
"slot": 3
},
{
"ability": {
"name": "overgrow",
"url": "https://pokeapi.co/api/v2/ability/65/"
},
"is_hidden": false,
"slot": 1
}
],
"name": "SomeRandomName"
}
"""
struct Pokemon: Codable {
let abilities: [AbilityElement]
let name: String
struct AbilityElement: Codable {
let ability: Ability
let isHidden: Bool
let slot: Int
struct Ability: Codable {
let name: String
let url: String
}
}
}
var pokemon: Pokemon?
do {
let jsonDecoder = JSONDecoder()
jsonDecoder.keyDecodingStrategy = .convertFromSnakeCase
if let data = jsonResponse.data(using: .utf8) {
pokemon = try jsonDecoder.decode(Pokemon.self, from: data)
}
} catch {
print("Something went horribly wrong:", error.localizedDescription)
}
print(pokemon)

Swift FAILURE: type Mismatch(Swift.Array<Any>)

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?