Export data from Microsoft SQL Server table to JSON using PowerShell

In previous post I wrote about exporting data from Microsoft SQL server table to CSV file. Export to JSON format is similar:

1
2
3
4
Invoke-Sqlcmd -ServerInstance . -Database AdventureWorks -Query "SELECT TOP 3 AccountNumber, Name, CreditRating FROM Purchasing.Vendor" `
    | Select-Object AccountNumber, Name, CreditRating `
    | ConvertTo-Json `
    | Out-File -FilePath Vendor.json -Encoding utf8


Udemy course: Improve your productivity with PowerShell

Content of exported JSON file is:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
[
    {
        "AccountNumber""AUSTRALI0001",
        "Name""Australia Bike Retailer",
        "CreditRating":  1
    },
    {
        "AccountNumber""ALLENSON0001",
        "Name""Allenson Cycles",
        "CreditRating":  2
    },
    {
        "AccountNumber""ADVANCED0001",
        "Name""Advanced Bicycles",
        "CreditRating":  1
    }
]

If you are interested in PowerShell automation, take my Udemy course Improve your productivity with PowerShell.

Leave a Reply

Your email address will not be published. Required fields are marked *