Swift Codable Map Children by ID - swift

I have the following code.
import Foundation
let jsonData = """
[
{"id": "1", "firstname": "Tom", "lastname": "Smith", "age": "28"},
{"id": "2", "firstname": "Bob", "lastname": "Smith"},
{"id": "3", "firstname": "Jim", "lastname": "Smith", "parentid": "2"},
{"id": "4", "firstname": "Ray", "lastname": "Smith", "parentid": "3"}
]
""".data(using: .utf8)!
class Person: Codable {
let id: String
let firstName, lastName: String
let age: String?
let parentid: String?
let children: [Person]?
enum CodingKeys : String, CodingKey {
case firstName = "firstname"
case lastName = "lastname"
case age
case parentid
case id
}
init(from decoder: Decoder) throws {
let values = try decoder.container(keyedBy: CodingKeys.self)
firstName = try values.decode(String.self, forKey: .firstName)
lastName = try values.decode(String.self, forKey: .lastName)
age = try values.decodeIfPresent(String.self, forKey: .age)
parentid = try values.decodeIfPresent(String.self, forKey: .parentid)
id = try values.decodeIfPresent(String.self, forKey: .id)
}
}
let decoded = try JSONDecoder().decode([Person].self, from: jsonData)
print(decoded)
So I have that children property. I basically want decoded to be an array with 2 Person objects (Tom and Bob). decoded[1].children should be an array with 1 Person object (Jim). And decoded[1].children[0] should be an array with 1 Person object (Ray).
How can I achieve this using the Swift Codable system?

You can do something like:
if let decoded = try? JSONDecoder().decode([Person].self, from: jsonData)
{
for person in decoded
{
person.children = decoded.filter({ (child) -> Bool in
return person.id == child.parentid
})
}
}
In the above code, after getting decoded array I just traversed it using a for loop to identify the children of each person if exist.

Related

Serialize json for its sample as a string

i'm trying to serialize a json but i have an error could someone give me a guide please i'm doing wrong i'm new to swift[enter image description here][1]
let code = "00001"
let firstName = "Joe"
let lastName = "Doe"
let middleName = "Mc."
let age = 100
let weight = 45
let jsonObject: [String: [String:Any]] = [
"code": code, <---- Cannot convert value of type 'String' to expected dictionary value type '[String : Any]'
"attributeMap": [
"first_name": firstName,
"middle_name": middleName,
"last_name": lastName,
"age": age,
"weight": weight
]
]
if let data = try? JSONSerialization.data(withJSONObject: jsonObject, options: .prettyPrinted),
let str = String(data: data, encoding: .utf8) {
print("===> \(str)")
}
--I would like a result like this JSON format
{
"code": "00001",
"attributeMap": {
"first_name": "Joe",
"middle_name": "Mc",
"last_name": "Doe",
"age": "23",
"weight": "home.zul"
}
}
If it repeatable operation I will prefer to use more "swifty" way. Let's define two structs
Person
struct Person: Encodable {
// Keys
enum CodingKeys: String, CodingKey {
case firstName = "first_name"
case middleName = "middle_name"
case lastName = "last_name"
case age
case weight
}
// Properties
let firstName: String
let middleName: String
let lastName: String
let age: Int
let weight: Double
}
The next one
Entity
struct Entity: Encodable {
// Keys
enum CodingKeys: String, CodingKey {
case code
case person = "attributeMap"
}
// Properties
let code: String
let person: Person
}
Usage
let person = Person(
firstName: "Joe",
middleName: "Mc.",
lastName: "Doe",
age: 100,
weight: 45
)
let entity = Entity(
code: "200",
person: person
)
if
let data = try? JSONEncoder().encode(entity),
let json = String(data: data, encoding: .utf8)
{
// Do something
}
Result
{"attributeMap":{"age":100,"last_name":"Doe","middle_name":"Mc.","weight":45,"first_name":"Joe"},"code":"200"}

Swift: How to convert Array of Dictionary to structure?

I have:
array = [["name": String, "lastName": String],
["name": String, "lastName": String],
["name": String, "lastName": String]]
(a: [Сlass.[String:String]]) -> [Class.SomeStruct] {}
How to make a structure with its properties from this array?
Like this:
struct SomeStruct {
let name: String
let lastName: String
}
You can use map or compactMap to transform your dictionaries into structs.
let array = [["name": "String", "lastName": "String"],
["name": "String", "lastName": "String"],
["name": "String", "lastName": "String"]]
struct SomeStruct {
let name: String
let lastName: String
}
let values = array.compactMap { data -> SomeStruct? in
guard let name = data["name"], let lastName = data["lastName"] else {
return nil
}
return SomeStruct(name: name, lastName: lastName)
}
Note: The compactMap will silently ignore all dictionaries that doesn't contains a name or a lastName key.
Use Codable
// MARK: - SomeStructElement
struct SomeStructElement: Codable, Equatable {
let name: Int?
let lastName: String?
enum CodingKeys: String, CodingKey {
case name = "name"
case lastName = "lastName"
}
}
And then use JSONDecoder:
let someStructArray: Array<SomeStructElement> = try? JSONDecoder().decode(Array<SomeStructElement>.self, from: data) ?? []
Source: Encoding and Decoding Custom Types (Apple developer)

Decoding this simple JSON struct is not working

I have a simple model which I defined to decode a struct.
But it is failing at decoding.
Can any one tell me what i am doing wrong?
struct Model: Codable {
let firstName: String
let lastName: String
let age: Int
enum Codingkeys: String, CodingKey {
case firstName = "first_name"
case lastName = "last_name"
case age
}
}
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let session = URLSession.shared
let url = URL(string: "https://learnappmaking.com/ex/users.json")!
let task = session.dataTask(with: url) { (data, response, error) in
let decoder = JSONDecoder()
let d = try! decoder.decode([Model].self, from: data!) //fails here
print(d)
}
task.resume()
}
}
I double checked to see if the json was correct, but it still fails to decode.
Error shown
Thread 5: Fatal error: 'try!' expression unexpectedly raised an error:
Swift.DecodingError.keyNotFound(CodingKeys(stringValue: "firstName",
intValue: nil), Swift.DecodingError.Context(codingPath:
[_JSONKey(stringValue: "Index 0", intValue: 0)], debugDescription: "No
value associated with key CodingKeys(stringValue: \"firstName\",
intValue: nil) (\"firstName\").", underlyingError: nil))
It keeps searching for firstName but i specifically have a enum to check for first_name.
This is the JSON Payload
[
{
"first_name": "Ford",
"last_name": "Prefect",
"age": 5000
},
{
"first_name": "Zaphod",
"last_name": "Beeblebrox",
"age": 999
},
{
"first_name": "Arthur",
"last_name": "Dent",
"age": 42
},
{
"first_name": "Trillian",
"last_name": "Astra",
"age": 1234
}
]
I know I can add decoder.keyDecodingStrategy = .convertFromSnakeCase but I want to know why the existing code is not working?
The code is correct, but apparently there is some problem with your model (although convertFromSnakeCase does work)
I retyped the struct and the error went away. Please copy and paste this
struct Model : Decodable {
let firstName : String
let lastName : String
let age : Int
private enum CodingKeys : String, CodingKey { case firstName = "first_name", lastName = "last_name", age }
}
Some of the values are optional, to be safe make all let as optional, It will work for sure.
struct Model: Codable {
let firstName: String?
let lastName: String?
let age: Int?
enum Codingkeys: String, CodingKey {
case firstName = "first_name"
case lastName = "last_name"
case age
}
}

Codable. How decode dictionary to property [duplicate]

How does the Swift 4 Decodable protocol cope with a dictionary containing a key whose name is not known until runtime? For example:
[
{
"categoryName": "Trending",
"Trending": [
{
"category": "Trending",
"trailerPrice": "",
"isFavourit": null,
"isWatchlist": null
}
]
},
{
"categoryName": "Comedy",
"Comedy": [
{
"category": "Comedy",
"trailerPrice": "",
"isFavourit": null,
"isWatchlist": null
}
]
}
]
Here we have an array of dictionaries; the first has keys categoryName and Trending, while the second has keys categoryName and Comedy. The value of the categoryName key tells me the name of the second key. How do I express that using Decodable?
The key is in how you define the CodingKeys property. While it's most commonly an enum it can be anything that conforms to the CodingKey protocol. And to make dynamic keys, you can call a static function:
struct Category: Decodable {
struct Detail: Decodable {
var category: String
var trailerPrice: String
var isFavorite: Bool?
var isWatchlist: Bool?
}
var name: String
var detail: Detail
private struct CodingKeys: CodingKey {
var intValue: Int?
var stringValue: String
init?(intValue: Int) { self.intValue = intValue; self.stringValue = "\(intValue)" }
init?(stringValue: String) { self.stringValue = stringValue }
static let name = CodingKeys.make(key: "categoryName")
static func make(key: String) -> CodingKeys {
return CodingKeys(stringValue: key)!
}
}
init(from coder: Decoder) throws {
let container = try coder.container(keyedBy: CodingKeys.self)
self.name = try container.decode(String.self, forKey: .name)
self.detail = try container.decode([Detail].self, forKey: .make(key: name)).first!
}
}
Usage:
let jsonData = """
[
{
"categoryName": "Trending",
"Trending": [
{
"category": "Trending",
"trailerPrice": "",
"isFavourite": null,
"isWatchlist": null
}
]
},
{
"categoryName": "Comedy",
"Comedy": [
{
"category": "Comedy",
"trailerPrice": "",
"isFavourite": null,
"isWatchlist": null
}
]
}
]
""".data(using: .utf8)!
let categories = try! JSONDecoder().decode([Category].self, from: jsonData)
(I changed isFavourit in the JSON to isFavourite since I thought it was a mispelling. It's easy enough to adapt the code if that's not the case)
You can write a custom struct that functions as a CodingKeys object, and initialize it with a string such that it extracts the key you specified:
private struct CK : CodingKey {
var stringValue: String
init?(stringValue: String) {
self.stringValue = stringValue
}
var intValue: Int?
init?(intValue: Int) {
return nil
}
}
Thus, once you know what the desired key is, you can say (in the init(from:) override:
let key = // whatever the key name turns out to be
let con2 = try! decoder.container(keyedBy: CK.self)
self.unknown = try! con2.decode([Inner].self, forKey: CK(stringValue:key)!)
So what I ended up doing is making two containers from the decoder — one using the standard CodingKeys enum to extract the value of the "categoryName" key, and another using the CK struct to extract the value of the key whose name we just learned:
init(from decoder: Decoder) throws {
let con = try! decoder.container(keyedBy: CodingKeys.self)
self.categoryName = try! con.decode(String.self, forKey:.categoryName)
let key = self.categoryName
let con2 = try! decoder.container(keyedBy: CK.self)
self.unknown = try! con2.decode([Inner].self, forKey: CK(stringValue:key)!)
}
Here, then, is my entire Decodable struct:
struct ResponseData : Codable {
let categoryName : String
let unknown : [Inner]
struct Inner : Codable {
let category : String
let trailerPrice : String
let isFavourit : String?
let isWatchList : String?
}
private enum CodingKeys : String, CodingKey {
case categoryName
}
private struct CK : CodingKey {
var stringValue: String
init?(stringValue: String) {
self.stringValue = stringValue
}
var intValue: Int?
init?(intValue: Int) {
return nil
}
}
init(from decoder: Decoder) throws {
let con = try! decoder.container(keyedBy: CodingKeys.self)
self.categoryName = try! con.decode(String.self, forKey:.categoryName)
let key = self.categoryName
let con2 = try! decoder.container(keyedBy: CK.self)
self.unknown = try! con2.decode([Inner].self, forKey: CK(stringValue:key)!)
}
}
And here's the test bed:
let json = """
[
{
"categoryName": "Trending",
"Trending": [
{
"category": "Trending",
"trailerPrice": "",
"isFavourit": null,
"isWatchlist": null
}
]
},
{
"categoryName": "Comedy",
"Comedy": [
{
"category": "Comedy",
"trailerPrice": "",
"isFavourit": null,
"isWatchlist": null
}
]
}
]
"""
let myjson = try! JSONDecoder().decode(
[ResponseData].self,
from: json.data(using: .utf8)!)
print(myjson)
And here's the output of the print statement, proving that we've populated our structs correctly:
[JustPlaying.ResponseData(
categoryName: "Trending",
unknown: [JustPlaying.ResponseData.Inner(
category: "Trending",
trailerPrice: "",
isFavourit: nil,
isWatchList: nil)]),
JustPlaying.ResponseData(
categoryName: "Comedy",
unknown: [JustPlaying.ResponseData.Inner(
category: "Comedy",
trailerPrice: "",
isFavourit: nil,
isWatchList: nil)])
]
Of course in real life we'd have some error-handling, no doubt!
EDIT Later I realized (in part thanks to CodeDifferent's answer) that I didn't need two containers; I can eliminate the CodingKeys enum, and my CK struct can do all the work! It is a general purpose key-maker:
init(from decoder: Decoder) throws {
let con = try! decoder.container(keyedBy: CK.self)
self.categoryName = try! con.decode(String.self, forKey:CK(stringValue:"categoryName")!)
let key = self.categoryName
self.unknown = try! con.decode([Inner].self, forKey: CK(stringValue:key)!)
}
Here's what I eventually came up for this json:
let json = """
{
"BTC_BCN":{
"last":"0.00000057",
"percentChange":"0.03636363",
"baseVolume":"47.08463318"
},
"BTC_BELA":{
"last":"0.00001281",
"percentChange":"0.07376362",
"baseVolume":"5.46595029"
}
}
""".data(using: .utf8)!
We make such a structure:
struct Pair {
let name: String
let details: Details
struct Details: Codable {
let last, percentChange, baseVolume: String
}
}
then decode:
if let pairsDictionary = try? JSONDecoder().decode([String: Pair.Details].self, from: json) {
var pairs: [Pair] = []
for (name, details) in pairsDictionary {
let pair = Pair(name: name, details: details)
pairs.append(pair)
}
print(pairs)
}
It is also possible to call not pair.details.baseVolume, but pair.baseVolume:
struct Pair {
......
var baseVolume: String { return details.baseVolume }
......
Or write custom init:
struct Pair {
.....
let baseVolume: String
init(name: String, details: Details) {
self.baseVolume = details.baseVolume
......

Realm swift list using Decodable

I am trying figure out how could I pars Realm list using new feature in Swift 4, Decodable protocol.
Here is a example JSON:
[{
"name": "Jack",
"lastName": "Sparrow",
"number": "1",
"address": [
{
"city": "New York",
"street": "av. test"
}
]
},
{
"name": "Cody",
"lastName": "Black",
"number": "2"
},
{
"name": "Name",
"lastName": "LastName",
"number": "4",
"address": [
{
"city": "Berlin",
"street": "av. test2"
},
{
"city": "Minsk",
"street": "av. test3"
}
]
}]
And Realm Models:
Person
public final class Person: Object, Decodable {
#objc dynamic var name = ""
#objc dynamic var lastName = ""
var address = List<Place>()
override public static func primaryKey() -> String? {
return "lastName"
}
private enum CodingKeys: String, CodingKey { case name, lastName, address}
convenience public init(from decoder: Decoder) throws {
self.init()
let container = try decoder.container(keyedBy: CodingKeys.self)
self.name = try container.decode(String.self, forKey: .name)
self.lastName = try container.decode(String.self, forKey: .lastName)
self.address = try container.decodeIfPresent(List<Place>.self, forKey: .address) ?? List()
}
}
Place
public final class Place: Object, Decodable {
#objc dynamic var city = ""
#objc dynamic var street = 0
override public static func primaryKey() -> String? {
return "street"
}
// We dont need to implement coding keys becouse there is nothing optional and the model is not expanded by extra properties.
}
And the result of parsing this JSON would be:
[Person {
name = Jack;
lastName = Sparrow;
number = 1;
address = List<Place> <0x6080002496c0> (
);
}, Person {
name = Cody;
lastName = Black;
number = 2;
address = List<Place> <0x6080002496c0> (
);
}, Person {
name = Name;
lastName = LastName;
number = 4;
address = List<Place> <0x6080002496c0> (
);
As we can see our list are always empty.
self.address = try container.decodeIfPresent(List<Place>.self, forKey: .address) ?? List()
will always be a nil.
Also I am extending List by :
extension List: Decodable {
public convenience init(from decoder: Decoder) throws {
self.init()
}
}
Any ideas what might be wrong ?
EDIT
struct LoginJSON: Decodable {
let token: String
let firstCustomArrayOfObjects: [FirstCustomArrayOfObjects]
let secondCustomArrayOfObjects: [SecondCustomArrayOfObjects]
let preferences: Preferences
let person: [Person]
}
Each property (instead of token) is a type of Realm Object and the last one is the one from above.
Thanks!
You cannot go directly from your JSON to a List. What's in the JSON is an array. So this line won't work:
self.address = try container.decodeIfPresent(List<Place>.self, forKey: .address) ?? List()
You have to start by fetching the array:
if let arr = try container.decodeIfPresent(Array<Place>.self, forKey: .address) {
// arr is now an array of Place
self.address = // make a List from `arr`, however one does that
} else {
self.address = nil
}