PoshCode Archive  Artifact [0e1f06b4a4]

Artifact 0e1f06b4a4fa17677188b7841ff2152e34f3ba1ac0370e16bd78253358812d0e:

  • File Read-JSON.ps1 — part of check-in [a97e59fc07] at 2018-06-10 13:47:07 on branch trunk — From gregs repository on github. (user: greg zakharov size: 929)

# encoding: ascii
# api: powershell
# title: Read-JSON
# description: From greg’s repository on github.
# version: 0.1
# type: function
# author: greg zakharov
# license: CC0
# function: Read-JSON
# x-poshcode-id: 4866
# x-archived: 2014-02-09T11:19:30
# x-published: 2014-02-03T07:54:00
#
#
#requires -version 2.0
function Read-JSON {
  <#
    .EXAMPLE
        PS C:\>Read-JSON data.json
    .NOTES
        Author: greg zakharov
  #>
  param(
    [Parameter(Mandatory=$true)]
    [ValidateScript({Test-Path $_})]
    [String]$FileName
  )
  
  begin {
    Add-Type -AssemblyName System.Web.Extensions
    $jss = New-Object Web.Script.Serialization.JavaScriptSerializer
  }
  process {
    try {
      $jss.DeserializeObject((cat (cvpa $FileName)))
    }
    catch [Management.Automation.MethodInvocationException] {
      Write-Host Invalid JSON primitive. -fo Red
    }
  }
  end {}
}