Since I posted my last blog, but since I´m working in the university, have two user groups to manage, and a family, it has been sitting back in the queue to write new blogs. There was a request from a person in the PowerPlatform User Group in Iceland that requested to get more details on JSON and how to use Powershell with JSON. We will be using both a JSON file and an online website to invoke rest requests to get more feel.
To start with, we will take a look at how JSON structure is built up.
Here we see a simple way of creating data with apple; we start by opening JSON data with open and close brackets; we add the key and the quotation marks' value. To add new lines, we need to add a comma after each line but not the last line. This is a typical JSON format of data. But if you want to have many keys and values, we can have an array of fruits, as shown in the example below.
The only difference is that we set a parent object key name and the array as the key's value and surround that with an angle bracket. We can save this text of the fruits to a file named fruits.json within a folder on c:\fruits\fruits.JSON. From there, we can work with the file. Then we can read the contents of the file by typing in :
$fruits = Get-Content "c:\fruits\fruits.json" | ConvertFrom-Json
We can now see how the data is provided if we import the JSON data into a variable.
But to connect to a website API that contains JSON contents and read it from Powershell, we can use something called Invoke-RestMethod, and from there, we can call API path with a GET method to get data from the API.
(Invoke-RestMethod -Method Get -Uri "https://api.chucknorris.io/jokes/random").value