How to convert DataTable to JSON using ConvertTo-json in Powershell v3

I have a DataTable $ dt with the same data, I would like to transfer JSON data using the ConvertTo-JSON cmdlet in Powershell v3

$ds.Tables["mytable"] | ConvertTo-Json 

As a result, all DataTable properties are returned, but I only need the records in the data table.

I am wondering if there is a way to do this without looking at each column / row and then adding to the user object..etc.

This is what I get when I run the above line;

 [ { "RowError": "", "RowState": 2, "Table": { "CaseSensitive": false, "IsInitialized": true, "RemotingFormat": 0, "ChildRelations": "", "Columns": "Id Name IsActive", "Constraints": "", "DataSet": "System.Data.DataSet", "DefaultView": "System.Data.DataRowView System.Data.DataRowView", "DisplayExpression": "", "ExtendedProperties": "System.D....... 

thanks

Yasser

http://www.sqlist.co.uk

+6
source share
2 answers

After playing with the datatable sample a bit, I ended up with this:

 ($ds.Tables["mytable"] | select $ds.Tables["mytable"].Columns.ColumnName ) | ConvertTo-Json 
+13
source

You can also exclude properties with Select-Object, removing noise.

 $dt | Select-Object * -ExcludeProperty ItemArray, Table, RowError, RowState, HasErrors | ConvertTo-Json 
0
source

All Articles