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

Question:

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

The result is all the properties of the DataTable 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 through each column/ row and adding then into a custom object..etc.

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

Thanks

Yasir

http://www.sqlist.co.uk

Answer:

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

Source:

How to convert DataTable to JSON using ConvertTo-json in Powershell v3 by licensed under CC BY-SA | With most appropriate answer!

Leave a Reply