0

We have an array element in our json that we want to save off into its own table. However, we are struggling with saving off the data to the table without needing to add a column.

Example of our json structure

{ fruits: [
{
fruitID: "12345"
    name: "apple",
    color: "red",
    recipes: [
        {
            title: "Apple Pie",
            description: "asbjfnksdkm",
            videoTutorial: "foo.com"
        },
        {
            title: "Apple Cinnamon Roll",
            description: "Cinnamon",
            videoTutorial: "foo2.com"
        }
    ]
},{
fruitID: "43214"
    name: "banana",
    color: "yellow",
    recipes: [
        {
            title: "Banana Split",
            description: "Ice Cream",
            videoTutorial: "weee.com"
        },
        {
            title: "Banana Foster",
            description: "Cinnamon",
            videoTutorial: "wooo.com"
        }
    ]
}]

}

How do we extract "recipes" from "fruits" without needing a column?

Our response comes in an immediate gets translated in a data codable that contains a fruits codable. Here's our codable structure:

 public struct DataClass: Codable {
 public let fruits: [Fruit]
    
    enum CodingKeys: String, CodingKey {
       case fruits
    }

    public class Fruit: Codable {
    public let fruits: [Fruit]
    
    enum CodingKeys: String, CodingKey {
        case fruits
    }
}

public class Fruit: Codable {
    public let name: String
    public let color: String
    public let recipes: [Recipes]
    
    enum CodingKeys: String, CodingKey {
        case name
        case color
        case recipes
    }
}

public class Recipes: Codable {
    public let title: String
    public let description: String
    public let videoTutorial: String
    
    enum CodingKeys: String, CodingKey {
        case title
        case description
        case videoTutorial
    }
}

In order to obtain the recipes data from the fruit, we added coding keys and a decode if present to get our array of Recipes. However, GRDB seems to want a column for recipes to be present when recipes is part of Coding Keys. Is there a way to get around this so that I only store recipes in its table without needing the column?

Currently, I have both and it is redundant so we're trying to get rid of the column as a table is preferable since recipes contains different elements that would be better stored in their own respective columns in the recipe table as opposed to a single column.

mgrene
  • 1
  • 1
    “… GRDB seems to want a column for recipes”, what kind of column? Are you talking about a foreign key because that is always needed when you have a relationship between two tables. – Joakim Danielson Aug 06 '22 at 07:05
  • I believe you will find this helpful: https://stackoverflow.com/questions/44549310/how-to-decode-a-nested-json-struct-with-swift-decodable-protocol – Rob C Aug 06 '22 at 09:52
  • @JoakimDanielson A column for recipes since it is a coding key – mgrene Aug 08 '22 at 13:53
  • You have already mentioned that in the question. – Joakim Danielson Aug 08 '22 at 16:46

0 Answers0