如何将NodeJs响应转换为嵌套的json响应

时间:2018-02-19 15:02:40

标签: mysql arrays json node.js express

我正在使用Nodejs(ExpressJS)和my-SQL来创建一个rest API。我希望我的响应像嵌套的json对象一样,在我的前端很容易阅读。我尝试使用for循环,但它没有给我正确的结果,是否有任何插件可用于格式化此响应或是否有任何简单的方法来执行此嵌套json响应。

我的SQL查询是

SELECT category.id as category_id, sub_category.id as subcategory_id, product.id as product_id, 
       category.name as category_name, sub_category.name as subcategory_name, 
       product.name as product_name 
FROM product 
JOIN sub_category ON product.sub_category_id = sub_category.id 
JOIN category ON sub_category.category_id = category.id 
ORDER BY category.id;

我得到的答案就像是

[
{
"category_id": 1,
"subcategory_id": 1,
"product_id": 11,
"category_name": "category 1",
"subcategory_name": "sub category 1",
"product_name": "New Product new"
},
{
"category_id": 1,
"subcategory_id": 2,
"product_id": 3,
"category_name": "category 1",
"subcategory_name": "sub category 2",
"product_name": "product 3"
},
{
"category_id": 1,
"subcategory_id": 1,
"product_id": 1,
"category_name": "category 1",
"subcategory_name": "sub category 1",
"product_name": "product 1"
},
{
"category_id": 1,
"subcategory_id": 1,
"product_id": 2,
"category_name": "category 1",
"subcategory_name": "sub category 1",
"product_name": "product 2"
},
{
"category_id": 1,
"subcategory_id": 1,
"product_id": 4,
"category_name": "category 1",
"subcategory_name": "sub category 1",
"product_name": "New Product updated"
},
{
"category_id": 1,
"subcategory_id": 1,
"product_id": 6,
"category_name": "category 1",
"subcategory_name": "sub category 1",
"product_name": "New Product"
},
{
"category_id": 1,
"subcategory_id": 1,
"product_id": 8,
"category_name": "category 1",
"subcategory_name": "sub category 1",
"product_name": "New Product new"
},
{
"category_id": 1,
"subcategory_id": 1,
"product_id": 9,
"category_name": "category 1",
"subcategory_name": "sub category 1",
"product_name": "New Product new"
},
{
"category_id": 1,
"subcategory_id": 1,
"product_id": 10,
"category_name": "category 1",
"subcategory_name": "sub category 1",
"product_name": "New Product new"
},
{
"category_id": 2,
"subcategory_id": 3,
"product_id": 5,
"category_name": "category 2",
"subcategory_name": "sub category 3",
"product_name": "product 5"
}
]

我需要像

[
    {
        "category_id" : 1,
        "category_name" : "product 1",
        "data":[
                {
                    "sub_category_id" : 1,
                    "sub_category_name" : "product 1",
                    "data":[
                        {
                            "product_id" : 1,
                            "product_name" : "product 1"  
                        },
                        {
                            "product_id" : 2,
                            "product_name" : "product 2" 
                        }
                    ]
                },
                {
                    "sub_category_id" : 2,
                    "sub_category_name" : "subcategory 2",
                    "data":[
                        {
                            "product_id" : 3,
                            "product_name" : "product 3"  
                        },
                        {
                            "product_id" : 4,
                            "product_name" : "product 4" 
                        }
                    ]    
                }
            ]
    },
    {
        "category_id" : 2 ,
        "category_name" : "category 2",
        "data":[
            {
                "sub_category_id" : 3,
                "sub_category_name" : "sub category 3",
                "data":[
                    {
                        "product_id" : 5,
                        "product_name" : "product 5"  
                    },
                    {
                        "product_id" : 6,
                        "product_name" : "product 6" 
                    }
                ]    
            }
        ]
    }
]

是否有任何插件,或任何人都可以帮助我。谢谢

2 个答案:

答案 0 :(得分:0)

" Flat优于嵌套"根据Python PEP20 :)人们宁愿反对相反的任务 - 他们希望将嵌套的JSON数据转换为平面结构,以便能够轻松地操纵数据。 Normalizr 是AFAIK最常用的此任务的库。

如果你确实需要它嵌套,那应该是编写正确的reduce函数的问题,我建议采用这种方式。

无论如何 - 如果您想使用第三方软件,这可能会有所帮助:https://www.npmjs.com/package/flat-to-nested

答案 1 :(得分:0)

检查下面的repl链接,用嵌套的forEach实现,但你也可以使用递归。

https://repl.it/@debasish_pothal/nested-json