Reputation: 1612
I have user input which is separated by commas and I am using split function to get different values. I have an API that returns some data in JSON. I want to filter data from API Json based on the user input
Powershell code
#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 }
I want the filtered data to return $json filtered by the userInput data. Thank you
Upvotes: 1
Views: 9166
Reputation: 16096
How about...
# 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
No need for this...
$userInputData = -split $GetIds
... since the split is on the Read
# Filter json with $userInputData
$Obj.results |
Where-Object id -in $GetIds
# Results
<#
id name
-- ----
1 John
2 Mark
#>
Upvotes: 0
Reputation: 437618
First, use the binary form of the -split
operator if you want to split by commas (,
) - the unary form splits by whitespace only.
# 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()
Next, it is $Obj
that you must filter with Where-Object
, i.e. the custom-object graph that ConvertFrom-Json
parsed your JSON text into, not the raw JSON:
$filteredData = $Obj.results | Where-Object id -in $userInputData
The -in
operator allows you to test the LHS for being part of the RHS array.
To put it all together:
Note: Your sample JSON is technically invalid, due to trailing commas after the last property in the .results
objects, which I've corrected below. In PowerShell [Core] v6+, ConvertFrom-Json
would accept even the invalid JSON, but not in 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
The above yields:
id name
-- ----
1 John
2 Mark
Upvotes: 3