根据用户输入的 powershell 筛选 JSON

Filter JSON based on the user input powershell

我有用逗号分隔的用户输入,我正在使用拆分函数来获取不同的值。我有一个 API return 在 JSON 中有一些数据。我想根据用户输入

从 API Json 过滤数据

Powershell 代码

#Get Input data
$GetIds = Read-Host -Prompt 'Enter Ids:'

#Example 1,2
#If they enter 1,2, I want results data of John and Mark
#API Call Data
$json = @'
{  
    "results": [
        {
            "id": "1", 
             "name": "John",           
        }, 
        {
             "id": "2", 
             "name": "Mark",  
        },
        {
             "id": "3", 
             "name": "Rachel",  
        }
    ]
}
'@

$Obj = ConvertFrom-Json $json

#Split by comma

$userInputData = -split $GetIds 

#Filter json with $userInputData 
$FilteredData = $json | Where-Object { $_.id -eq #loop through $userInputData }

我希望将过滤后的数据 return $json 由 userInput 数据过滤。谢谢

首先,如果要用逗号分隔 (,),请使用 -split operatorbinary 形式 - unary 表格仅按 空格 拆分。

# Sample user input
$GetIds = '1, 2'

# Split by ",", remove surrounding whitespace, convert to integers.
# For brevity, there's no error handling her, 
# so an empty / blank input wouldn't be interpreted as id 0, 
# and input such as `'1 2'` (no comma) would break.
[int[]] $userInputData = ($GetIds -split ',').Trim()

接下来,您必须使用 Where-Object 过滤 $Obj,即 ConvertFrom-Json 将您的 JSON 文本解析成的 custom-object 图,而不是原始 JSON:

$filteredData = $Obj.results | Where-Object id -in $userInputData

-in operator 允许您测试 LHS 是否是 RHS 阵列的一部分。


总而言之:

注意:您的示例 JSON 在技术上无效,因为 .results 对象中最后一个 属性 后面有逗号,我已在下面更正。在 PowerShell [Core] v6+ 中,ConvertFrom-Json 甚至会接受无效的 JSON,但在 Windows PowerShell 中则不会。

# Sample user input, in lieu of the Read-Host call.
$GetIds = '1, 2'

# Split by ",", remove surrounding whitespace, convert to integers.
# For brevity, there's no error handling her, 
# so an empty / blank input wouldn't be interpreted as id 0, 
# and input such as `'1 2'` (no comma) would break.
[int[]] $userInputData = ($GetIds -split ',').Trim()

$Obj = ConvertFrom-Json @'
{  
    "results": [
        {
            "id": "1", 
             "name": "John"
        }, 
        {
             "id": "2", 
             "name": "Mark"
        },
        {
             "id": "3", 
             "name": "Rachel"
        }
    ]
}
'@

$filteredData = $Obj.results | Where-Object id -in $userInputData

# Output the matching objects
$filteredData

以上结果:

id name
-- ----
1  John
2  Mark

怎么样...

# You can split on the read
$GetIds = (Read-Host -Prompt 'Enter Ids') -split (',')
# Results
<#
1
2
#>

# Your JSON string was not valid
<#
Error: Parse error on line 4:
...     "name": "John",     },      {           "id": "2",
----------------------^
Expecting 'STRING', got '}'
#>

# Corrected, notice the removed comma after the names 
$json = @'
{  
    "results": [
        {
            "id": "1", 
             "name": "John"           
        }, 
        {
             "id": "2", 
             "name": "Mark"  
        },
        {
             "id": "3", 
             "name": "Rachel"  
        }
    ]
}
'@

$Obj = $json | ConvertFrom-Json

不需要这个...

$userInputData = -split $GetIds 

...因为拆分是在读取

# Filter json with $userInputData 
$Obj.results | 
Where-Object id -in $GetIds
# Results
<#
id name
-- ----
1  John
2  Mark
#>