Skip to content

Instantly share code, notes, and snippets.

@matt40k
Last active September 16, 2017 22:47
Show Gist options
  • Save matt40k/fab6ca579c355a0862a0a2ffb8166330 to your computer and use it in GitHub Desktop.
Save matt40k/fab6ca579c355a0862a0a2ffb8166330 to your computer and use it in GitHub Desktop.
For some reason ConvertTo-Json messes up within a function - what am I missing? Note-Now fixed, this isn't c#, see commits to see fix. (https://gist.github.com/matt40k/fab6ca579c355a0862a0a2ffb8166330/revisions)
$v1
$v2
Function GetJson($value1, $value2)
{
$j = @{"test"=$value1;"test2"=$value2} | ConvertTo-Json
return $j
}
$v1 = "test123"
$v2 = "test321"
$result = GetJson $v1 $v2
Write-Host $result
#Returns (incorrect)
#{
# "test": [
# "test123",
# "test321"
# ],
# "test2": null
#}
$value1=$v1
$value2=$v2
$j2 = @{"test"=$value1;"test2"=$value2} | ConvertTo-Json
Write-Host $j2
#Returns (correct)
#{
# "test": "test123",
# "test2": "test321"
#}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment