Можно попробовать
let str = """
[
{
"category" : {
"id" : 1,
"text" : "cat1"
},
"id" : 1,
"title" : "book1"
},{
"category" : {
"id" : 2,
"text" : "cat2"
},
"id" : 2,
"title" : "book2"
},{
"category" : {
"id" : 1,
"text" : "cat1"
},
"id" : 3,
"title" : "book3"
}
]
"""
do {
let res = try JSONDecoder().decode([Root].self, from: Data(str.utf8))
print(res)
let dic = Dictionary(grouping: res, by: { $0.category.text})
print(dic) // this dictionary is your new data source key is title of section value is sections rows
}
catch {
print(error)
}
struct Root: Codable {
let category: Category
let id: Int
let title: String
}
struct Category: Codable {
let id: Int
let text: String
}