Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am accepting a json object as a command line input & then trying to parse it in Powershell 3.0.

The code is as below -

# Accept the command line json parameter
param(
 [string]$json = $(throw '-json parameter is required')
)

$currentPath = Get-Location

# Load the Dot Net Json Library
[Reflection.Assembly]::LoadFile("$currentPath\Newtonsoft.Json.dll")

# Parsed result stored
$result = [Newtonsoft.Json.Linq.JObject]::Parse($json)

write-host "$result"

foreach ($singleResult in $result)
{    
    foreach ($units in $singleResult) 
    { 
      foreach ($unit in $units)
      {
         write-host "$unit" -foreground DarkYellow

         $TechnologyName = $unit.TechnologyName.ToString();
         write-host "$TechnologyName"


         $OutputValue = $unit.OutputValue.ToString();
         write-host "$OutputValue"               
      }  
    }     
}

The command line is as below -

PS C:\Users\aghosh\Desktop> .\test.ps1 -json '{"DevResults":[{"TechnologyName":"TFS","RuleName":"Alt CI ID for ESB","OutputValue":"ESClientCenter"},{"TechnologyName":"TFS","RuleName":"TFS Team Project Name","OutputValue":"ClientCenter"}],"QaResults":[{"TechnologyName":"TFS","RuleName":"Alt CI ID for ESB","OutputValue":"ESClientCenter"},{"TechnologyName":"TFS","RuleName":"TFS Team Project Name","OutputValue":"ClientCenter"}],"PreProdResults":[{"TechnologyName":"TFS","RuleName":"Alt CI ID for ESB","OutputValue":"ESClientCenter"},{"TechnologyName":"TFS","RuleName":"TFS Team Project Name","OutputValue":"ClientCenter"}],"ProdResults":[{"TechnologyName":"TFS","RuleName":"Alt CI ID for ESB","OutputValue":"ESClientCenter"},{"TechnologyName":"TFS","RuleName":"TFS Team Project Name","OutputValue":"ClientCenter"},{"TechnologyName":"TFS","RuleName":"Process Template","OutputValue":"Raymond James CMMI V3.5"}]}'

Now I want to get to the portion -

"DevResults": [
  {
    "TechnologyName": "TFS",
    "RuleName": "Alt CI ID for ESB",
    "OutputValue": "ESClientCenter"
  },
  {
    "TechnologyName": "TFS",
    "RuleName": "TFS Team Project Name",
    "OutputValue": "ClientCenter"
  }

Can you please give me suitable directions.Thanks.

share|improve this question
I got the desired result using - foreach($unit in $result["DevResults"]) {Write-Host $unit["TechnologyName"].ToString()}.Thanks – anir Jul 22 at 21:26
You can answer your own questions – Lars Truijens Jul 22 at 21:27
2  
Also PS v3 has build in Json support. $result = ConvertFrom-Json $json; $result.DevResults | Select-Object TechnologyName – Lars Truijens Jul 22 at 21:32
add comment (requires an account with 50 reputation)

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.