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!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.