`ConvertTo-Csv` and `Export-Csv` should unroll (embedded) arrays and lists
See original GitHub issuePrerequisites
- Write a descriptive title.
- Make sure you are able to repro it on the latest released version
- Search the existing issues.
- Refer to the FAQ.
- Refer to Differences between Windows PowerShell 5.1 and PowerShell.
Steps to reproduce
Knowing that the -InputObject
“Specifies the objects that are converted to CSV strings”, I would expect that any embedded array or list to unroll into objects (see also: Suggestion: Add a chunking (partitioning, batching) mechanism to Select-Object, analogous to Get-Content -ReadCount)
$List = 1..5 |ForEach-Object {
[pscustomobject]@{ id = $_; name = "name$_" }
}
,$List |ConvertTo-Csv
Expected behavior
"id","name"
"1","name1"
"2","name2"
"3","name3"
"4","name4"
"5","name5"
Similar to:
$List |Convert-Csv # Without an unary comma
and to what Add-Content
and Set-Content
does for:
,(1..5) |Set-Content .\MyFile.txt
Or simply as it is being output:
,$List
id name
-- ----
1 name1
2 name2
3 name3
4 name4
5 name5
Actual behavior
"Length","LongLength","Rank","SyncRoot","IsReadOnly","IsFixedSize","IsSynchronized","Count"
"5","5","1","System.Object[]","False","True","False","5"
Error details
No Error
Environment data
Name Value
---- -----
PSVersion 7.2.6
PSEdition Core
GitCommitId 7.2.6
OS Microsoft Windows 10.0.22000
Platform Win32NT
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
WSManStackVersion 3.0
Visuals
No response
Issue Analytics
- State:
- Created a year ago
- Reactions:2
- Comments:16 (8 by maintainers)
Top Results From Across the Web
Flatten a nested JSON with array and filter to CSV
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...
Read more >Avoiding System.Object[] (or Similar Output) when using ...
One approach to this is to use the –Join operator on those properties which will have a collection of items in it. [pscustomobject]@{...
Read more >Convert Json With Nested Arrays to CSV
We are trying to get data that is exported through an API converted to a CSV. Each "columns" array is the data we...
Read more >ConvertTo-FlatObject - Power$nippets
This cmdlet might help to resolve this as it recursively flattens every property of any embedded array, hash table or object to property...
Read more >ConvertTo-Csv (Microsoft.PowerShell.Utility)
The ConvertTo-CSV cmdlet returns a series of character-separated value (CSV) strings ... You can use the Export-Csv cmdlet to convert objects to CSV...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@mklement0 Yes, I was just reading the comment the 18070, and if for anything which looks at the InputObject parameter generally, the object vs Object[] question should be on the radar.
I wonder, without having checked, if the commands which unroll arrays tend towards having
Object[]
and the ones which don’t towardsObject
When I’m coding in PowerShell I leave
InputObject
as object (which is implied so I don’t need to have it in the param block), and where a process block would beI typically have
I don’t spend enough time in c# to know if that is equally easy to do there - PowerShell is very tolerant of
foreach ($i in $ScalarVariable)
but this simple change means if the object is an array it unrolls (one level, not nested arrays) and if its scalar it processes as normal.@mklement0 I’m looking at your category C items with a view to taking them to the cmdlet working group
There might be some quibbles about what is truly in category C. With
Where-object
, someone somewhere is bound to want to use the count of members or even-is [Array]
in a where condition. But the list is a helpful place to start from.