Creating a JSON file from SQL Server
In this recipe, we will create a JSON file from records that we retrieve from SQL Server.
Getting ready
For this recipe, choose a table with records that you want to export to a JSON file.
How to do it...
The following steps walk you through how to create a JSON file from SQL Server records using PowerShell:
- Open PowerShell ISE as an administrator.
- Import the
SQLPS
module as follows:#import SQL Server module Import-Module SQLPS -DisableNameChecking
- Add the following script and run it:
$VerbosePreference = "Continue" $instanceName = "localhost" $databaseName = "SampleDB" $fileName = "C:\DATA\Customers.json" Invoke-SqlCmd -Query "SELECT * FROM SampleText" -ServerInstance $instanceName -Database $databaseName | ConvertTo-Json ...
Get SQL Server 2014 with PowerShell v5 Cookbook now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.