Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
256 views
in Technique[技术] by (71.8m points)

powershell - Not able to fetch the individual details from JSON data

"Ns": {
    "value": [
        {
            "Nname": "exa",
            "SR": [
                {
                    "name": "port1",
                    "properties": {
                        "description": "Allow port1",
                        "destinationPortRange": "1111",
                        "priority": 100
                    }
                },
                {
                    "name": "port1_0",
                    "properties": {
                        "description": "Allow port1",
                        "destinationPortRange": "1111",
                        "priority": 150
                    }
                },
                {
                    "name": "port2",
                    "properties": {
                        "description": "Allow  1115",
                        "destinationPortRange": "1115",
                        "priority": 100,
                    }
                }
            ]
        }
    ]
}

Want to assert the details of priority and name but was not able to do it.

Here is what I have implemented:

$Ndetails = templateProperties.parameters.Ns.value.SR
foreach ($Ndata in $Ndetails) {
    $Ndata .properties.destinationPortRange |
        Should -BeExactly @('1111','1111','1115')
}?

How to resolve the same using Pester in PowerShell?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You don't need to use foreach for this. You can just use Select-Object for this. Assuming your JSON is as @Mark Wragg linked in the comments:

$Json = @'
[{
    "Ns": {
        "value": [{
            "Nname": "exa",
            "SR": [{
                    "name": "port1",
                    "properties": {
                        "description": "Allow port1",
                        "destinationPortRange": "1111",
                        "priority": 100
                    }
                },
                {
                    "name": "port1_0",
                    "properties": {
                        "description": "Allow port1",
                        "destinationPortRange": "1111",
                        "priority": 150
                    }
                },
                {
                    "name": "port2",
                    "properties": {
                        "description": "Allow  1115",
                        "destinationPortRange": "1115",
                        "priority": 100
                    }
                }
            ]
        }]
    }
}]
'@
 
$t = $Json | ConvertFrom-Json

Your test file should look like this:

$result = $t.Ns.value.SR.properties.destinationPortRange
it 'destinationPortRange matches' {
  $result | Should -BeExactly @('1111','1111','1115')
}

Explanation

Your use of foreach was incorrect as you compared single element (also notice that I deleted unnecessary space)

$Ndata.properties.destinationPortRange

to the array

| Should -BeExactly @('1111','1111','1115')

What you have to do is to compare array to array as in my example.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...