PowerShell and SQL Server – Exporting Data (JSON)

Sometimes it can be useful to export data from a database, so that it can be analysed, or, to import in to another computer system. JSON or JavaScript Object Notation files are an alternative file format to CSV, that can sometimes be used for both of these scenarios.

The example below exports data from a table called ‘person’, which contains five columns, ‘id’, ‘firstname’, ‘lastname’, ‘title’ and ‘dob’, from within an SQL Server database, to a JSON file in a specified location.

id firstname lastname title dob
1 Bob Smith Mr 1980-01-20
3 Fred Bloggs Mr 1975-05-07
4 Alan White Mr 1989-03-20
5 Fiona Bloggs Mrs 1985-05-19
6 Zoe Davis Miss 1979-07-11
7 Tom Ingram Mr 1971-10-04
8 Karen Thomas Mrs 1969-03-08
9 Samantha Yates Miss 1995-08-27

Firstly, variables are defined to facilitate the connection and authentication to the database. This is followed by a query string to extract, or select, the data from the ‘person’ table, as well as variables for the JSON file path and name. If the file path exists, the ‘Invoke-Sqlcmd’ cmdlet is used to extract the data. If data has been returned, a new JSON file is created and its contents is constructed.

A rolling seven day backup is also included. This makes a copy of the JSON file that has just been created, giving it a name that includes the index number for the day of the week, along with the day itself, for example, 'personexport-1-monday.json', for the backup on a Monday. Here, Sunday is classed as the first day of the week, with an index value of zero. Note that the backup is only done for the first time that this is run in a given day. Backups are then overwritten each week.

Feedback is provided as to the success or failure of the task.

# Clear the console window.
Clear-Host

# Database variables.
$server = "MSSQLSERVERDEMO"
$database = "Demo"
$username = "DemoUN"
$password = "DemoPW"

# SQL query to select all data in the person table.
$query = "SELECT id, firstname, lastname, title, format(dob, 'yyyy-MM-dd') as dob " + 
         "FROM [dbo].[person] ORDER BY [id]"

# Export path and file.
$exportPath = "C:\Demo\"
$exportJson = "personexport.json"

# Check to see if the file path exists.
if (Test-Path $exportPath)
{

    try
    {

        # Extract the data.
        $people = Invoke-Sqlcmd -ServerInstance $server -Database $database `
        -Username $username -Password $password -Query $query -ErrorAction Stop `

        # If data has been returned, do the export.
        if ($people.Count -gt 0)
        {

            # Export to JSON.
            [pscustomobject] @{ 
                person = @($people | Select-Object -Property id, firstname, lastname, title, dob)
            } | ConvertTo-Json | Out-File ($exportPath + $exportJson) -Encoding utf8

            # Today's date.
            $today = Get-Date

            # Construct the backup file name.
            $exportBackupJson = $exportJson.Substring(0, $exportJson.Length-5) + "-" + `
                                [int]$today.DayOfWeek + "-" + `
                                $today.DayOfWeek.ToString().ToLower() + ".json"

            # Check if the backup file does not exist, or if it does, check that
            # today's date is different from the last modified date.
            if (-not (Test-Path ($exportPath + $exportBackupJson)) -or `
               ((Test-Path ($exportPath + $exportBackupJson)) -and `
                ((Get-Item ($exportPath + $exportBackupJson)).LastWriteTime.date -ne $today.Date)))
            {

                # Copy the JSON export.
                Copy-Item ($exportPath + $exportJson) `
                -Destination ($exportPath + $exportBackupJson) -Force

            }

            # Message stating export successful.
            Write-Host "Data export successful."

        }
        else
        {

            # Message stating no data to export.
            Write-Host "There is no data to export."
          
        }



    }
    catch
    {

        # Message stating export unsuccessful.
        Write-Host "Data export unsuccessful."

    }

}
else
{

    # Message stating file path does not exist.
    Write-Host "File path does not exist."

}

The JSON file produced contains the following data.

{
  "person": [
    {
      "id": 1,
      "firstname": "Bob",
      "lastname": "Smith",
      "title": "Mr",
      "dob": "1980-01-20"
    },
    {
      "id": 3,
      "firstname": "Fred",
      "lastname": "Bloggs",
      "title": "Mr",
      "dob": "1975-05-07"
    },
    {
      "id": 4,
      "firstname": "Alan",
      "lastname": "White",
      "title": "Mr",
      "dob": "1989-03-20"
    },
    {
      "id": 5,
      "firstname": "Fiona",
      "lastname": "Bloggs",
      "title": "Mrs",
      "dob": "1985-05-19"
    },
    {
      "id": 6,
      "firstname": "Zoe",
      "lastname": "Davis",
      "title": "Miss",
      "dob": "1979-07-11"
    },
    {
      "id": 7,
      "firstname": "Tom",
      "lastname": "Ingram",
      "title": "Mr",
      "dob": "1971-10-04"
    },
    {
      "id": 8,
      "firstname": "Karen",
      "lastname": "Thomas",
      "title": "Mrs",
      "dob": "1969-03-08"
    },
    {
      "id": 9,
      "firstname": "Samantha",
      "lastname": "Yates",
      "title": "Miss",
      "dob": "1995-08-27"
    }
  ]
}