问题
I got a question about How to use Powershell flatten a nested JSON and covert to CSV. Below is my JSON, which is a mail message log get from Office 365 with many users messages, I need to filter the columns, flatten and convert to CSV :
createdDateTime,
receivedDateTime,
from_name
from_adress
To_name_1
To_adress_2
To_name_2
To_adress_2
...
The "from" field has only one data. But the "toRecipients" is a array.
{
...
"createdDateTime": "xxxx-xx-xx",
"receivedDateTime": "xxxx-xx-xx",
"isRead": true,
"from": {
"emailAddress": {
"name": "John",
"adress": "john@onmicrosoftware.com"
}
},
"toRecipients": [
{
"emailAddress": {
"name": "Amy",
"adress": "Amy@onmicrosoftware.com"
}
},
{
"emailAddress": {
"name": "Amy",
"adress": "Amy@onmicrosoftware.com"
}
}
]
}
回答1:
Here is a complete runnable example. It will create a file "C:\test.csv".
There is no "automatic" way of flattening a nested object to a flat object. But you can manually create assign properties to a flat object.
First I parse the JSON text into a powershell object
$obj = @"
{
"createdDateTime": "xxxx-xx-xx",
"receivedDateTime": "xxxx-xx-xx",
"isRead": true,
"from": {
"emailAddress": {
"name": "John",
"adress": "john@onmicrosoftware.com"
}
},
"toRecipients": [
{
"emailAddress": {
"name": "Amy",
"adress": "Amy@onmicrosoftware.com"
}
},
{
"emailAddress": {
"name": "Amy",
"adress": "Amy@onmicrosoftware.com"
}
}
]
}
"@ | ConvertFrom-Json
Now take the Powershell object (or list of objects, this will work even if you have many of these entries) and pipe it to ForEach-Object. Inside the loop map the different properties to a flat object.
$flattened = $obj | ForEach-Object {
return [PSCustomObject]@{
createdDateTime = $_.createdDateTime
receivedDateTime = $_.receivedDateTime
from_name = $_.from.emailAddress.name
from_adress = $_.from.emailAddress.adress
to_name_1 = $_.toRecipients[0].emailAddress.name
to_adress_1 = $_.toRecipients[0].emailAddress.adress
to_name_2 = $_.toRecipients[1].emailAddress.name
to_adress_2 = $_.toRecipients[1].emailAddress.adress
}
}
Now you can export the entire thing as a CSV
$flattened | Export-Csv C:\test.csv -Delimiter ";" -Encoding UTF8
This assumes that there will always be 2 toRecipients. It would be possible to dynamically add to_name_3, to_name_4, and so on if more are encountered, but that's quite a bit more complicated.
来源:https://stackoverflow.com/questions/62340113/flatten-a-nested-json-with-array-and-filter-to-csv