Tagged: JSON

PowerShell: working with JSON files

With introduction of Office 365 and SharePoint online, Microsoft increasingly using JSON file format to transfer data to and from the new platform. For example, Bulk UPA Custom Profile Properties Update API for SharePoint Online. For SharePoint administrator is natural to fall back to PowerShell to perform those tasks and automate the integration between the on-Premises environment and SharePoint Online.  Windows PowerShell 3.0 higher includes couple of commandlets (ConvertFrom-Json and ConvertTo-Json) to convert PowerShell objects to JSON format and back. One of the used cases is a fast and easy way to interact with web services.

####################################################################################################
#
#  Author.......: David Shvartsman
#  Date.........: 05/21/2018
#  Description..: Using PowerShell with JSON
#
#################################################################################################### 
#Save PowerShell object to the file in JSON format
$users | Where {$_name -like 'test'} | Select-Object $selectObject -skip 10 | 
            Select-Object -First 5 | ConvertTo-Json > C:\temp\users.json


#Read File in JSON format into PowerShell object
$users = Get-Content 'C:\temp\users.json' | Out-String | 
            ConvertFrom-Json | SELECT -expand Value | SELECT UserPrincipalName 

#Read Web Request response:
$WebRequestURL = "http://www.nactem.ac.uk/software/acromine/dictionary.py?sf=BMI"
Invoke-WebRequest $WebRequestURL | ConvertFrom-Json  | select *


#################################################################################################### 

That was another page in the Chronicles of SharePoint Bits, happy scripting!