3

enter image description here

In the above example, I would like to get the value "Bug" from "System.WorkItemType"

using

$response = Invoke-RestMethod -Url $someUri -Method Get -ContentType "application/json" -Headers $headers
$wit = response.fields.System.WorkItemType

doesn't work since the dot / period messes it up.

In javascript there is this question and answer but did not work in Powershell.

Using regex to replace the dot with underscore seems too farfetched (and I didn't get it to work, but used -match -convertFrom-Json -replace -convertTo-Json so maybe did it wrong?

So my question is simply: How do I get the 'Bug' value from the 'System.WorkItemType' key? (bug may be other strings...)

mklement0
  • 382,024
  • 64
  • 607
  • 775
Pierre
  • 559
  • 9
  • 21
  • 3
    You can use quotes so that it's parsed literally, try `$wit = response.fields.'System.WorkItemType'` or `$wit = response.fields."System.WorkItemType"` (both should work). – Jacob Oct 12 '18 at 21:32
  • Thtat did the trick! Thank you so much! – Pierre Oct 12 '18 at 21:41

1 Answers1

10

If you put the property names in "" or '', you should be able to get what you are looking for:

$json= @'
 { "id" : 9983,
    "rev" : 17,
    "fields" :{
    "System.AreaPath":"Cloud\\Dev Blue Team",
    "System.TeamProject":"Cloud"
    }
}
'@

$j = $Json | convertfrom-json
$j.fields."System.AreaPath"
$J.fields.'System.TeamProject'

if you need to escape double quotes, use the ` grave accent, known as the backtick in PowerShell.

"area path = $($j.fields.`"System.AreaPath`")"
mklement0
  • 382,024
  • 64
  • 607
  • 775
Thom Schumacher
  • 1,469
  • 13
  • 24