PowerShell : retrieve JSON object by field value

前端 未结 6 2143
说谎
说谎 2020-12-07 15:15

Consider JSON in this format :

\"Stuffs\": [
    {
        \"Name\": \"Darts\",
        \"Type\": \"Fun Stuff\"
    },
    {
        \"Name\": \"Clean Toilet         


        
6条回答
  •  醉梦人生
    2020-12-07 16:12

    Hows about this:

    $json=Get-Content -Raw -Path 'my.json' | Out-String | ConvertFrom-Json
    $foo="TheVariableYourUsingToSelectSomething"
    $json.SomePathYouKnow.psobject.properties.Where({$_.name -eq $foo}).value
    

    which would select from json structured

    {"SomePathYouKnow":{"TheVariableYourUsingToSelectSomething": "Tada!"}
    

    This is based on this accessing values in powershell SO question . Isn't powershell fabulous!

提交回复
热议问题