如何在Marshmallow中创建嵌套的json架构?

时间:2018-02-06 05:30:44

标签: python json marshmallow

我正在尝试从SQL Server中的表中获取数据并将其转换为JSON格式。我使用Marshmallow实现了对基本JSON格式的转换。我没有广泛使用JSON。要求是将其转换为分层JSON格式,如下所示:

必需的JSON格式:

 {
        "Year" : [{
            "2016" : [{
                "Q1" : [{
                    "Jan" : [{
                        "Week1" : [{
                            "Sunday" : [1,2,3],
                            "Monday" : [1,2,3],
                            "Tuesday" : [1,2,3],
                            "Wednesday" : [1,2,3],
                            "Thursday" : [1,2,3],
                            "Friday" : [1,2,3],
                            "Saturday" : [1,2,3]
                        }],
                        "Week2" : [{
                            "Sunday" : [1,2,3],
                            "Monday" : [1,2,3],
                            "Tuesday" : [1,2,3],
                            "Wednesday" : [1,2,3],
                            "Thursday" : [1,2,3],
                            "Friday" : [1,2,3],
                            "Saturday" : [1,2,3]
                        }],
                        "Week3" : [{
                            "Sunday" : [1,2,3],
                            "Monday" : [1,2,3],
                            "Tuesday" : [1,2,3],
                            "Wednesday" : [1,2,3],
                            "Thursday" : [1,2,3],
                            "Friday" : [1,2,3],
                            "Saturday" : [1,2,3]
                        }],
                        "Week4" : [{
                            "Sunday" : [1,2,3],
                            "Monday" : [1,2,3],
                            "Tuesday" : [1,2,3],
                            "Wednesday" : [1,2,3],
                            "Thursday" : [1,2,3],
                            "Friday" : [1,2,3],
                            "Saturday" : [1,2,3]
                        }],
                    }],
                }],
            }],
        }],
    }

我已经从表中读取数据,创建了一些函数并且已经实现了这种格式:

示例实现格式:

[{"sent_year": 2017, "sent_quarter": 4, "sent_month": "December", "sent_week": "Week5", "sent_day": "Sunday", "application_name": "App Two", "sent_score": "3"},
 {"sent_year": 2017, "sent_quarter": 4, "sent_month": "December", "sent_week": "Week5", "sent_date": "Monday", "application_name": "App One", "sent_score": "1"}, 
 {"sent_year": 2017, "sent_quarter": 4, "sent_month": "December", "sent_week": "Week5", "sent_date": "Tuesday", "application_name": "App Two", "sent_score": "3"},
 {"sent_year": 2016, "sent_quarter": 4, "sent_month": "December", "sent_week": "Week5", "sent_date": "Wednesday", "application_name": "App One", "sent_score": "2"}, 
 {"sent_year": 2016, "sent_quarter": 4, "sent_month": "December", "sent_week": "Week5", "sent_date": "Thursday", "application_name": "App Two", "sent_score": "1"}, 
 {"sent_year": 2016, "sent_quarter": 4, "sent_month": "December", "sent_week": "Week5", "sent_date": "Friday", "application_name": "App One", "sent_score": "4"}]

由于这涉及基于价值的数据分组,我无法弄清楚如何继续。请帮忙。

0 个答案:

没有答案