将JSON从FLAT转换为HIERARCHY JSON

时间:2018-09-21 15:46:38

标签: node.js

我正在以以下格式获取JSON:

[
        {
            "PatientId": 1,
            "FirstName": "Emma",
            "LastName": "Williams",
            "Sex": "Female",
            "DOB": "1982-06-17T18:30:00.000Z",
            "ProgramName": "UHC_ACO_1",
            "ScheduledDate": "2018-09-18T18:30:00.000Z",
            "vfirstname": "Mia",
            "vlastname": "Smith"
        },
        {
            "PatientId": 1,
            "FirstName": "Emma",
            "LastName": "Williams",
            "Sex": "Female",
            "DOB": "1982-06-17T18:30:00.000Z",
            "ProgramName": "UHC_ACO_2",
            "ScheduledDate": "2018-09-18T18:30:00.000Z",
            "vfirstname": "Mia",
            "vlastname": "Smith"
        },
        {
            "PatientId": 2,
            "FirstName": "James",
            "LastName": "Smith",
            "Sex": "Male",
            "DOB": "2009-06-19T18:30:00.000Z",
            "ProgramName": "UHC_ACO_1",
            "ScheduledDate": null,
            "vfirstname": null,
            "vlastname": null
        }
    ]

但是我真的想要以下格式

[
         {
            "PatientId": 1,
            "FirstName": "Emma",
            "LastName": "Williams",
            "Sex": "Female",
            "DOB": "1982-06-17T18:30:00.000Z",
            "ProgramId":[
                        {
                            "ProgramName": "UHC_ACO_1",
                            "ScheduledDate": "2018-09-18T18:30:00.000Z",
                            "vfirstname": "Mia",
                            "vlastname": "Smith"
                        },
                        {
                            "ProgramName": "UHC_ACO_2",
                            "ScheduledDate": "2018-09-18T18:30:00.000Z",
                            "vfirstname": "Mia",
                            "vlastname": "Smith"
                        }
                        ]
        },

        {
            "PatientId": 2,
            "FirstName": "James",
            "LastName": "Smith",
            "Sex": "Male",
            "DOB": "2009-06-19T18:30:00.000Z",
            "ProgramId":[
                         {
                            "ProgramName": "UHC_ACO_1",
                            "ScheduledDate": null,
                            "vfirstname": null,
                            "vlastname": null
                        }
                        ]
        }
       ]

我看到了很多例子,它们循环到父母,孩子身上,以获得树的结构。

但是我不希望任何东西被硬编码,因为json格式可能会改变。

我想要一个通用方法。

我正在使用nodejs,在代码中表达框架。

1 个答案:

答案 0 :(得分:-1)

Lodash解决了该问题,无需对任何事情进行硬编码