PowerShell – Read JSON files

February 9, 2015

As we have seen Powershell can produce JSON output.

But what if we have to read a JSON file as input.

Here we go :

CLS
[System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") | Out-Null

$dlg = New-Object System.Windows.Forms.OpenFileDialog

$dlg.initialDirectory = """ + initialDir + """
$dlg.filter = "JSON files|*.JSON |Text Documents|*.txt | All Files|*.*"
$dlg.FilterIndex = 3
$dlg.Title = 'Select a file'
$dlg.ShowHelp = $True
$dlg.ShowDialog() | Out-Null
$dlg.ToFront

$dlg.FileName


$j = (get-content -Raw -path $dlg.FileName) | ConvertFrom-JSON 

$j.value

rv dlg,j

First of all we need to get Northwind Database to test.

The content looks like this :

image

Once downloaded you can run the PS Script.

It will bring up a File Selector Window

image

Select the Northwind.json file and look at the results.

image

Enjoy!

Advertisements

SharePoint – REST Interface, JSON output

July 1, 2014

REST and JSON are hot these days, prior to SOAP and XML.

Luckily for PowerShell scripters, SharePoint has an REST interface.

And PowerShell has a JSON class.

Here is an example on how to combine both.

CLS

$creds = Get-Credential # get and save credentials

$url = "http://YourSite/_vti_bin/listdata.svc/YourList"

# Once you have the URI set,
# you can use the Invoke-RestMethod cmdlet to get the required information back:
# -UseDefaultCredentials or -Credential $cred

$ListService = Invoke-RestMethod -Uri $URL -Credential $cred

echo $ListService | select ID, Name | ConvertTo-Json

If you have more examples let’s please share.