Hey guys!
I have some issues with fetching data from JSON.
I've created an object of type Item, this is how it looks.
struct Item: Codable, Identifiable {
var id = UUID()
var products: Product
}
struct Product: Codable {
let barcodeNumber: String // barcode_number
var productName: String? // product_name
var category: String? // category
var brand: String? // brand
var images: [String]? // images
}
This is how the object looks like:
{
"products": [
{
"barcode_number": "0140157379",
"barcode_type": "ISBN",
"barcode_formats": "ISBN 0140157379, ISBN 9780140157376",
"mpn": "11712838",
"model": "",
"asin": "",
"product_name": "Haroun and the Sea of Stories",
"title": "Haroun and the Sea of Stories",
"category": "Media > Books > Print Books",
"manufacturer": "Penguin Books, Inc.",
"brand": "Penguin Books",
"label": "",
"author": "Rushdie, Salman",
"publisher": "Penguin Books",
"artist": "",
"actor": "",
"director": "",
"studio": "",
"genre": "",
"audience_rating": "Adult",
"ingredients": "",
"nutrition_facts": "",
"color": "",
"format": "Marketplace",
"package_quantity": "",
"size": "",
"length": "",
"width": "",
"height": "",
"weight": "0.4",
"release_date": "",
"description": "Excellent Marketplace listings for \"Haroun and the Sea of Stories\" by Salman Rushdie starting as low as $1.99!",
"features": [],
"images": [
"https://images.barcodelookup.com/134/1342375-1.jpg"
],
"stores": [
{
"store_name": "Textbooks.com",
"store_price": "1.99",
"product_url": "https://www.textbooks.com/Haroun-and-the-Sea-of-Stories-90-Edition/9780140157376/Salman-Rushdie.php&intsrc=CATF_2377",
"currency_code": "USD",
"currency_symbol": "$"
},
{
"store_nam
Object is quite long so I am uploading only the part of it here. But the data I am interested in, is in this part. During a catch where I decode data and put into the object, catch throws an error:
do {
let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase
let product = try decoder.decode([Item].self, from: data)
print(product)
completed(.success(product))
} catch {
completed(.failure(.chatchError))
}
I can only assume that the error appears cause my custom object is not right. Do you have any suggestions what am I doing wrong?
Many thanks!