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:

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:

[
    {
        "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 *