Json object oddities with New-UDAutocompleteOption

Product: PowerShell Universal
Version: 5.0.7

I have a function that fetches data, and places it in an autocomplete, but the value parameter of New-UDAutocompleteOption doesnt seem to accept json data.

The following doesn’t work as expected:

New-UDApp -Content { 
    Function Get-STBEntries {
        [CmdletBinding()]
        param()
    
        '[{
        "Status":  "Status1",
        "Serialnr":  "aaa111"
        },
        {
        "Status":  "Status2",
        "Serialnr":  "bbb222"
        }]' | ConvertFrom-Json
    }
    
    $STBEntries = Get-STBEntries
    
    New-UDCard -Title 'Select computer' -Content {
        $STCompOptions = $STBEntries | Foreach-Object {
            New-UDAutocompleteOption -Name $_.Computername -Value "$($_ | ConvertTo-Json -Compress)"
        }
    
        New-UDAutocomplete -Label 'Select' -Options $STCompOptions -OnChange {
            Show-UDToast $EventData -Duration 100000
        } -FullWidth
    }
}

When running this instead of a toast i get an error: Cannot bind argument to parameter Message because it is an empty string

However, as soon as i add anything in the same string as ConvertTo-Json and “destroy” the json object it works as expected.

New-UDApp -Content { 
    Function Get-STBEntries {
        [CmdletBinding()]
        param()
    
        '[{
        "Status":  "Status1",
        "Serialnr":  "aaa111"
        },
        {
        "Status":  "Status2",
        "Serialnr":  "bbb222"
        }]' | ConvertFrom-Json
    }
    
    $STBEntries = Get-STBEntries
    
    New-UDCard -Title 'Select computer' -Content {
        $STCompOptions = $STBEntries | Foreach-Object {
            New-UDAutocompleteOption -Name $_.Status -Value "$($_ | ConvertTo-Json -Compress)-" # <- Note the dash '-'
        }
    
        New-UDAutocomplete -Label 'Select' -Options $STCompOptions -OnChange {
            Show-UDToast $EventData -Duration 100000
        } -FullWidth
    }
}

I kind of feel like there is something I do wrong, but can’t figure out what… Any ideas?

PSUJson

Bump. Anyone have any idea what this could be? Updated to 5.0.9 and no difference. (And noticed I wrote one of the code blocks wrong above… $_.Computername should of course be $_.Status

PSU

Personally, all of my string parameters I use with ConvertTo/From-Json are made with the following string structure:

@{
  Parameter = "$(ConvertTo-Json $JsonString -Depth 10)"
}

This way it’s evaluated and then forced to typecast itself into a string. Can you attempt with this structure?

Thank you so much for the suggestion, but I am already doing this (not putting the parameters in a hashtable, but string interpolation, "$()").

I also tried putting the result in a variable but the result is exactly the same. This doesnt work, but adding anything to the $value string that breaks the json format makes it work.

New-UDApp -Content { 
    Function Get-STBEntries {
        [CmdletBinding()]
        param()
    
        '[{
        "Status":  "Status1",
        "Serialnr":  "aaa111"
        },
        {
        "Status":  "Status2",
        "Serialnr":  "bbb222"
        }]' | ConvertFrom-Json
    }
    
    $STBEntries = Get-STBEntries
    
    New-UDCard -Title 'Select computer' -Content {
        $STCompOptions = $STBEntries | Foreach-Object {
            [string]$Value = "$($_ | ConvertTo-Json -Compress)"
            New-UDAutocompleteOption -Name $_.Status -Value $Value
        }
    
        New-UDAutocomplete -Label 'Select' -Options $STCompOptions -OnChange {
            Show-UDToast $EventData -Duration 100000
        } -FullWidth
    }
}

This may seem irrelevant, but can you try bypassing the pipeline and directly passing the InputObject? I’m wondering if it’s having an issue with the evaluation.

Another thing to try, though it won’t work in the structure you’re anticipating, is to transform your prefilled JSON string into a traditional pscustomobject/hashtable to see if it may be an issue with the evaluation on ConvertFrom-Json… this is an interesting one. Let me know if either of those have any effect; I’m going to test this on an app in my environment in the meantime to see if I can’t recreate.

Well, converting your $EventData to JSON works for the UDToast - but for whatever reason, something with the -OnChange trigger is causing this. You could also access the Value with $EventData.Value, predictably.

New-UDAutocomplete -Label 'Select' -Options $STCompOptions -OnChange {
  Show-UDToast (ConvertTo-Json $EventData) -Duration 100000
} -FullWidth

Bypassing the pipeline, ConvertTo-Json -Compress -InputObject $_, does nothing. Same error.
Also tried the ConvertTo-JsonEX from the Universal module, but no difference.

Using tha original hashtable works, but like mentioned, the value isn’t what I need in the end. Show-UDToast Is really just a part of an entire script, so I would prefer to have the JSON string.

It is only an issue with New-UDAutocomplete though. Using New-UDSelect works just as expected.

New-UDSelect -Label 'Select' -Option {
            $STBEntries | Foreach-Object {
                New-UDSelectOption -Name $_.Status -Value "$($_ | ConvertTo-Json -Compress)"
            }
        } -OnChange {
            Show-UDToast $EventData -Duration 100000
        } -FullWidth

I also tried to change the command order, not that I actually thought it would matter… And it didnt, same issue.

        New-UDAutocomplete -Label 'Select' -Options (
            $STBEntries | Foreach-Object {
                New-UDAutocompleteOption -Name $_.Status -Value "$($_ | ConvertTo-Json -Compress)"
            }
        ) -OnChange {
            Show-UDToast $EventData -Duration 100000
        } -FullWidth

I am starting to think a bug report to @adam is in place?