Introduction
Reading JSON file can be done using "ConvertFrom-Json" method which simply converts a JSON content to an object or a hash table. I would like to explain the reading / parsing concept by using below JSON format string. JSON can be downloaded from site or can also be get from file from local machine.
JSON content example:
'{"Chair":[ { "color":"white", "price":"50" }, { "height":"10", "width":"5" }]}'
Reading JOSN using site URL
$JSONRequest = 'http://<JSON SITE URL>'
$JSONContent = Invoke-WebRequest $JSONRequest | ConvertFrom-Json
$JSONContent | select -expand Chair | Select color
$JSONContent | select -expand Chair | Select price
$JSONContent | select -expand Chair | Select height
$JSONContent | select -expand Chair | Select width
$JSONContent = Invoke-WebRequest $JSONRequest | ConvertFrom-Json
$JSONContent | select -expand Chair | Select color
$JSONContent | select -expand Chair | Select price
$JSONContent | select -expand Chair | Select height
$JSONContent | select -expand Chair | Select width
Reading JSON file from local machine
$JSONContent = Get-Content -Path "C:\filename.json" | ConvertFrom-JSON
$JSONContent | select -expand Chair
$JSONContent | select -expand Chair | Select color
$JSONContent | select -expand Chair | Select price
$JSONContent | select -expand Chair | Select height
$JSONContent | select -expand Chair | Select width
$JSONContent | select -expand Chair
$JSONContent | select -expand Chair | Select color
$JSONContent | select -expand Chair | Select price
$JSONContent | select -expand Chair | Select height
$JSONContent | select -expand Chair | Select width
Some time, you may get the error message below while running this cInvoke-WebRequest command. Invoke-WebRequest : The underlying connection was closed: An unexpected error occurred on a send. This error be resolved by using below link of code in start of PowerShell script
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
No comments:
Post a Comment