![]() |
VOOZH | about |
The CData Cmdlets for JSON are standard PowerShell cmdlets that make it easy to accomplish data cleansing, normalization, backup, and other integration tasks by enabling real-time and bidirectional access to JSON.
The Cmdlets are not only a PowerShell interface to JSON, but also an SQL interface; this tutorial shows how to use both to create, retrieve, update, and delete JSON services. We also show examples of the ADO.NET equivalent, which is possible with the CData ADO.NET Provider for JSON. To access JSON services from other .NET applications, like LINQPad, use the CData ADO.NET Provider for JSON.
Once you have acquired the necessary connection properties, accessing JSON services in PowerShell can be enabled in three steps.
See the Getting Started chapter in the data provider documentation to authenticate to your data source: The data provider models JSON APIs as bidirectional database tables and JSON files as read-only views (local files, files stored on popular cloud services, and FTP servers). The major authentication schemes are supported, including HTTP Basic, Digest, NTLM, OAuth, and FTP. See the Getting Started chapter in the data provider documentation for authentication guides.
After setting the and providing any authentication values, set to more closely match the data representation to the structure of your data.
The property is the controlling property over how your data is represented into tables and toggles the following basic configurations.
See the Modeling JSON Data chapter for more information on configuring the relational representation. You will also find the sample data used in the following examples. The data includes entries for people, the cars they own, and various maintenance services performed on those cars.
Install the module:
Install-Module JSONCmdlets
Connect:
$json = Connect-JSON -URI "$URI" -DataModel "$DataModel"
Search for and retrieve data:
$[ personal.name.last ] = "Roberts" $people = Select-JSON -Connection $json -Table "people" -Where "[ personal.name.last ] = `'$[ personal.name.last ]`'" $people
You can also use the Invoke-JSON cmdlet to execute SQL commands:
$people = Invoke-JSON -Connection $json -Query 'SELECT * FROM people WHERE [ personal.name.last ] = @[ personal.name.last ]' -Params @{'@[ personal.name.last ]'='Roberts'}
Load the provider's assembly:
[Reflection.Assembly]::LoadFile("C:\Program Files\CData\CData ADO.NET Provider for JSON\lib\System.Data.CData.JSON.dll")
Connect to JSON:
$conn= New-Object System.Data.CData.JSON.JSONConnection("URI=C:/people.json;DataModel=Relational;")
$conn.Open()
Instantiate the JSONDataAdapter, execute an SQL query, and output the results:
$sql="SELECT [ personal.name.first ], [ personal.name.last ] from people"
$da= New-Object System.Data.CData.JSON.JSONDataAdapter($sql, $conn)
$dt= New-Object System.Data.DataTable
$da.Fill($dt)
$dt.Rows | foreach {
Write-Host $_.[ personal.name.first ] $_.[ personal.name.last ]
}
Update-JSON -Connection $JSON -Columns @('[ personal.name.first ]','[ personal.name.last ]') -Values @('My[ personal.name.first ]', 'My[ personal.name.last ]') -Table people -Id "MyId"
$cmd = New-Object System.Data.CData.JSON.JSONCommand("UPDATE people SET [ personal.name.last ]='Roberts' WHERE Id = @myId", $conn)
$cmd.Parameters.Add((New-Object System.Data.CData.JSON.JSONParameter("@myId","10456255-0015501366")))
$cmd.ExecuteNonQuery()
Add-JSON -Connection $JSON -Table people -Columns @("[ personal.name.first ]", "[ personal.name.last ]") -Values @("My[ personal.name.first ]", "My[ personal.name.last ]")
$cmd = New-Object System.Data.CData.JSON.JSONCommand("INSERT INTO people ([ personal.name.last ]) VALUES (@my[ personal.name.last ])", $conn)
$cmd.Parameters.Add((New-Object System.Data.CData.JSON.JSONParameter("@my[ personal.name.last ]","Roberts")))
$cmd.ExecuteNonQuery()
Remove-JSON -Connection $JSON -Table "people" -Id "MyId"
$cmd = New-Object System.Data.CData.JSON.JSONCommand("DELETE FROM people WHERE Id=@myId", $conn)
$cmd.Parameters.Add((New-Object System.Data.CData.JSON.JSONParameter("@myId","001d000000YBRseAAH")))
$cmd.ExecuteNonQuery()
CodeProject
Download a free trial of the JSON Data Provider to get started:
Download NowLearn more:
👁 JSON IconRapidly create and deploy powerful .NET applications that integrate with JSON web services.