展平嵌套的JSON并转换为CSV

时间:2020-06-18 13:52:58

标签: json powershell

我有一个嵌套的JSON,我想将其转换为CSV。下面是包含详细信息的JSON,我们希望使用csv格式将csv消费到其他应用程序中。

{
"Code": 1,
"Message": "Success",
"EmployeesCount": "2",
"Employees": [
    {
        "EmployeeCode": "XA0001",
        "EmployeeID": "1256",
        "EmployeeName": "Srihari T",
        "FirstName": "Srihari",
        "LastName": "T",
        "MiddleName": "",
        "Attributes": [
            {
                "AttributeTypeID": "64",
                "AttributeTypeDesc": "State",
                "AttributeTypeCode": "State",
                "AttributeTypeUnitID": "5367",
                "AttributeTypeUnitDesc": "Kerela",
                "AttributeTypeUnitCode": "Kelera"
            },
            {
                "AttributeTypeID": "68",
                "AttributeTypeDesc": "Department",
                "AttributeTypeCode": "Department",
                "AttributeTypeUnitID": "5428",
                "AttributeTypeUnitDesc": "Finance",
                "AttributeTypeUnitCode": "Finance"
            },
            {
                "AttributeTypeID": "67",
                "AttributeTypeDesc": "Designation",
                "AttributeTypeCode": "Designation",
                "AttributeTypeUnitID": "5409",
                "AttributeTypeUnitDesc": "Executive",
                "AttributeTypeUnitCode": "Executive"
            }
        ]
    },
    {
        "EmployeeCode": "XA0002",
        "EmployeeID": "1257",
        "EmployeeName": "Adam Varghese",
        "FirstName": "Adam",
        "LastName": "Varghese",
        "MiddleName": "",
        "Attributes": [
            {
                "AttributeTypeID": "64",
                "AttributeTypeDesc": "State",
                "AttributeTypeCode": "State",
                "AttributeTypeUnitID": "5367",
                "AttributeTypeUnitDesc": "Tamil Nadu",
                "AttributeTypeUnitCode": "Tamil Nadu"
            },
            {
                "AttributeTypeID": "68",
                "AttributeTypeDesc": "Department",
                "AttributeTypeCode": "Department",
                "AttributeTypeUnitID": "5428",
                "AttributeTypeUnitDesc": "CC",
                "AttributeTypeUnitCode": "CC"
            },
            {
                "AttributeTypeID": "67",
                "AttributeTypeDesc": "Designation",
                "AttributeTypeCode": "Designation",
                "AttributeTypeUnitID": "5409",
                "AttributeTypeUnitDesc": "TL",
                "AttributeTypeUnitCode": "TL"
            }
        ]
    }
]

}

我需要以以下格式输出到CSV

EmployeeCode    EmployeeID  EmployeeName    State       Department  Designation
XA0001          1256        Srihari T       Kerala      Finance     Executive
XA0002          1257        Adam Varghese   Tamil Nadu  CC          TL

遵循Powershell代码

$JSON = @"{
"Code": 1,
"Message": "Success",
"EmployeesCount": "2",
"Employees": [
    {
        "EmployeeCode": "XA0001",
        "EmployeeID": "1256",
        "EmployeeName": "Srihari T",
        "FirstName": "Srihari",
        "LastName": "T",
        "MiddleName": "",
        "Attributes": [
            {
                "AttributeTypeID": "64",
                "AttributeTypeDesc": "State",
                "AttributeTypeCode": "State",
                "AttributeTypeUnitID": "5367",
                "AttributeTypeUnitDesc": "Kerela",
                "AttributeTypeUnitCode": "Kelera"
            },
            {
                "AttributeTypeID": "68",
                "AttributeTypeDesc": "Department",
                "AttributeTypeCode": "Department",
                "AttributeTypeUnitID": "5428",
                "AttributeTypeUnitDesc": "Finance",
                "AttributeTypeUnitCode": "Finance"
            },
            {
                "AttributeTypeID": "67",
                "AttributeTypeDesc": "Designation",
                "AttributeTypeCode": "Designation",
                "AttributeTypeUnitID": "5409",
                "AttributeTypeUnitDesc": "Executive",
                "AttributeTypeUnitCode": "Executive"
            }
        ]
    },
    {
        "EmployeeCode": "XA0002",
        "EmployeeID": "1257",
        "EmployeeName": "Adam Varghese",
        "FirstName": "Adam",
        "LastName": "Varghese",
        "MiddleName": "",
        "Attributes": [
            {
                "AttributeTypeID": "64",
                "AttributeTypeDesc": "State",
                "AttributeTypeCode": "State",
                "AttributeTypeUnitID": "5367",
                "AttributeTypeUnitDesc": "Tamil Nadu",
                "AttributeTypeUnitCode": "Tamil Nadu"
            },
            {
                "AttributeTypeID": "68",
                "AttributeTypeDesc": "Department",
                "AttributeTypeCode": "Department",
                "AttributeTypeUnitID": "5428",
                "AttributeTypeUnitDesc": "CC",
                "AttributeTypeUnitCode": "CC"
            },
            {
                "AttributeTypeID": "67",
                "AttributeTypeDesc": "Designation",
                "AttributeTypeCode": "Designation",
                "AttributeTypeUnitID": "5409",
                "AttributeTypeUnitDesc": "TL",
                "AttributeTypeUnitCode": "TL"
            }
        ]
    }
]}"@ | ConvertFrom-Json 

$Flattened =$JSON | ForEach-Object {
                Return [PSCustomObject]@{
                EmployeeCode = $_.Employees.EmployeeCode
                EmployeeID = $_.Employees.EmployeeID
                EmployeeName = $_.Employees.EmployeeName
                State = $_.Employees.Attributes[0].AttributeTypeUnitDesc
                Department = $_.Employees.Attributes[1].AttributeTypeUnitDesc
                Designation = $_.Employees.Attributes[2].AttributeTypeUnitDesc
                }
                } $Flattened

输出是

EmployeeCode : {XA0001, XA0002}
EmployeeID   : {1256, 1257}
EmployeeName : {Srihari T, Adam Varghese}
State        : Kerela
Department   : Finance
Designation  : Executive

请帮助我如何获得所需的输出。

1 个答案:

答案 0 :(得分:0)

使用此基本数据从JSON转换:

$data = ConvertFrom-Json '{
"Code": 1,
"Message": "Success",
"EmployeesCount": "2",
"Employees": [
    {
        "EmployeeCode": "XA0001",
        "EmployeeID": "1256",
        "EmployeeName": "Srihari T",
        "FirstName": "Srihari",
        "LastName": "T",
        "MiddleName": "",
        "Attributes": [
            {
                "AttributeTypeID": "64",
                "AttributeTypeDesc": "State",
                "AttributeTypeCode": "State",
                "AttributeTypeUnitID": "5367",
                "AttributeTypeUnitDesc": "Kerela",
                "AttributeTypeUnitCode": "Kelera"
            },
            {
                "AttributeTypeID": "68",
                "AttributeTypeDesc": "Department",
                "AttributeTypeCode": "Department",
                "AttributeTypeUnitID": "5428",
                "AttributeTypeUnitDesc": "Finance",
                "AttributeTypeUnitCode": "Finance"
            },
            {
                "AttributeTypeID": "67",
                "AttributeTypeDesc": "Designation",
                "AttributeTypeCode": "Designation",
                "AttributeTypeUnitID": "5409",
                "AttributeTypeUnitDesc": "Executive",
                "AttributeTypeUnitCode": "Executive"
            }
        ]
    },
    {
        "EmployeeCode": "XA0002",
        "EmployeeID": "1257",
        "EmployeeName": "Adam Varghese",
        "FirstName": "Adam",
        "LastName": "Varghese",
        "MiddleName": "",
        "Attributes": [
            {
                "AttributeTypeID": "64",
                "AttributeTypeDesc": "State",
                "AttributeTypeCode": "State",
                "AttributeTypeUnitID": "5367",
                "AttributeTypeUnitDesc": "Tamil Nadu",
                "AttributeTypeUnitCode": "Tamil Nadu"
            },
            {
                "AttributeTypeID": "68",
                "AttributeTypeDesc": "Department",
                "AttributeTypeCode": "Department",
                "AttributeTypeUnitID": "5428",
                "AttributeTypeUnitDesc": "CC",
                "AttributeTypeUnitCode": "CC"
            },
            {
                "AttributeTypeID": "67",
                "AttributeTypeDesc": "Designation",
                "AttributeTypeCode": "Designation",
                "AttributeTypeUnitID": "5409",
                "AttributeTypeUnitDesc": "TL",
                "AttributeTypeUnitCode": "TL"
            }
        ]
    }
]}'

和此PowerShell代码(请注意,我们要迭代Employees!):

$Flattened = $data.Employees | ForEach-Object {
    [PSCustomObject]@{
        EmployeeCode = $_.EmployeeCode
        EmployeeID = $_.EmployeeID
        EmployeeName = $_.EmployeeName
        State = ($_.Attributes | Where AttributeTypeCode -eq "State").AttributeTypeUnitDesc
        Department = ($_.Attributes | Where AttributeTypeCode -eq "Department").AttributeTypeUnitDesc
        Designation = ($_.Attributes | Where AttributeTypeCode -eq "Designation").AttributeTypeUnitDesc
    }
}

$Flattened | ConvertTo-Csv -NoTypeInformation

此输出在控制台上产生

"EmployeeCode","EmployeeID","EmployeeName","State","Department","Designation"
"XA0001","1256","Srihari T","Kerela","Finance","Executive"
"XA0002","1257","Adam Varghese","Tamil Nadu","CC","TL"

这应该足够接近您想要的内容。我很确定您不需要固定宽度的列。

要将CSV保存到文件中,请使用带有Unicode标志的Export-Csv而不是ConvertTo-Csv

... | Export-Csv -NoTypeInformation -Path ... -Encoding UTF8