Свести вложенный JSON и преобразовать в CSV - PullRequest
0 голосов
/ 18 июня 2020

У меня есть вложенный 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 голосов
/ 18 июня 2020

С этими базовыми данными, преобразованными из 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 в файл, используйте Export-Csv с флагом Unicode вместо ConvertTo-Csv.

... | Export-Csv -NoTypeInformation -Path ... -Encoding UTF8
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...