question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

`ConvertTo-Csv` and `Export-Csv` should unroll (embedded) arrays and lists

See original GitHub issue

Prerequisites

Steps to reproduce

Knowing that the -InputObjectSpecifies 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:closed
  • Created a year ago
  • Reactions:2
  • Comments:16 (8 by maintainers)

github_iconTop GitHub Comments

2reactions
jhoneillcommented, Sep 14, 2022

@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 towards Object

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 be

Process { 
  Do_stuff_with $InputObject
}

I typically have

Process {
    foreach ($i in  $InputObject ) {
      Do_stuff_with $i
    }
}

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.

2reactions
jhoneillcommented, Sep 14, 2022

@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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found