У меня есть вложенный 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
Пожалуйста, помогите мне, как получить желаемый результат.