JSON

JavaScript Object Notation (JSON) is a data format that is easily read by humans and quickly parsed by machines. It has become very popular in combination with REST and for web services, but it is also used for storing configuration data. The following examples show how to work with the two cmdlets called ConvertTo-Json and ConvertFrom-Json:

#Exporting Services to JSON
$ServicesAsJSON = Get-Service | Select-Object -Property DisplayName, ServiceName, Status, StartType | ConvertTo-Json

#Converting exported JSON object to hashtable
$importedServices = $ServicesAsJSON | ConvertFrom-Json

#Show
$importedServices | Out-GridView

#Different types
(Get-Service)[0].GetType() #ServiceController
$importedServices[0].GetType() #PSCustomObject
$importedServices.GetType() #HashTable -- Array

#Loading some JSON data into variable
$someJSONdata = '{"people":[
{ "firstName":"John", "lastName":"Doe" },
{ "firstName":"Jane", "lastName":"Dane" }
]}'

#Converting exported JSON object to hashtable
$ConvertedDataFromJSON = ConvertFrom-Json -InputObject $someJSONdata

#Show
$ConvertedDataFromJSON.people | Out-GridView

Their use is very straightforward and self-explanatory, and works exactly the same as the previously described converting cmdlets.

If you want to create or modify JSON files yourself, you can take a look at https://www.json.org/.

The next example shows how to retrieve data with REST and work with it:

#Using TLS 1.2
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12

#Invoking web request to GitHub on the PowerShell repo and converting them to JSON
$gatheredPowerShellIssues = Invoke-WebRequest 'https://api.github.com/repos/PowerShell/PowerShell/issues' | ConvertFrom-Json

#Show
$gatheredPowerShellIssues | Out-GridView

The Invoke-WebRequest cmdlet gathers all the PowerShell issues as JSON objects. Therefore, we can pipe them easily to ConvertFrom-Json to retrieve a hash table to work with.

In addition, the settings in Visual Studio Code are maintained as JSON files. Try to press Ctrl + Shift + P and search for configure to find the preferences for user snippets:

Opening it up will show the underlying configuration file for the snippets. You can now easily open up all the configurations of VS Code and edit the specific JSON files as you like.

Once you have been working with Visual Studio Code for a while, you can customize it completely to your preferences.
You can find some of the most used settings on the following tutorial sites:
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset