Skip to content

ConvertTo-Json and URL encoding #2632

Closed

Description

Steps to reproduce

When you use Convertto-Json cmdlet and have an url in one of the properties of your hashtable then the output will show that there took some url encoding place during the conversion.

Expected behavior

I don't want the url encoding to happen. Or at least I want to be able to configure this if needed.

Actual behavior

url encoding is happening.

Environment data

Name                           Value
----                           -----
PSVersion                      5.1.14959.1000
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.14959.1000
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1

Example for testing:

$uri = 'https://www.stranger.nl/thisisstrange?date=2015-04-05&value=foo&this#&%*'

$body = @{

    'url' = $uri

}


$body | convertto-json

[Reflection.Assembly]::LoadFile("C:\Program Files\WindowsPowerShell\Modules\newtonsoft.json\1.0.1.2\libs\Newtonsoft.Json.dll") | out-null

[Newtonsoft.Json.JsonConvert]::SerializeObject($body)

When using the Newtonsoft json class the output is being show as expected.

Output:
{
    "url":  "https://www.stranger.nl/thisisstrange?date=2015-04-05\u0026value=fo
o\u0026this#\u0026%*"
}
{"url":"https://www.stranger.nl/thisisstrange?date=2015-04-05&value=foo&this#&%*
"}

Activity

  1. added this to the milestone on Jan 23, 2017
  2. modified the milestones: 6.0.0-beta1, on Mar 16, 2017
  3. changed the title [-]Convertto-Json and url encoding[/-] [+]ConvertTo-Json and URL encoding[/+] on Mar 21, 2017
  4. modified the milestones: , on May 15, 2017
  5. SteveL-MSFT commented on May 16, 2017

    @SteveL-MSFT
    Member

    This doesn't repro with PSCore6 since we are using Newtonsoft Json.net

    PS C:\users\slee\repos\PowerShell> $uri = 'https://www.stranger.nl/thisisstrange?date=2015-04-05&value=foo&this#&%*'
    PS C:\users\slee\repos\PowerShell>
    PS C:\users\slee\repos\PowerShell> $body = @{
    >>
    >>     'url' = $uri
    >>
    >> }
    PS C:\users\slee\repos\PowerShell>
    PS C:\users\slee\repos\PowerShell>
    PS C:\users\slee\repos\PowerShell> $body | convertto-json
    {
      "url": "https://www.stranger.nl/thisisstrange?date=2015-04-05&value=foo&this#&%*"
    }
  6. epernst commented on Sep 2, 2020

    @epernst

    I'm also using Powershell 5.1 and I'm still getting this problem. ConvertTo-Json creates an useable url.
    Is your answer to this in my world very critical error really to use PSCore6?

  7. vexx32 commented on Sep 2, 2020

    @vexx32
    Collaborator

    Yes. PowerShell 5.1 is not being actively maintained. How would you expect to get new features/bugfixes without using a new version of the product?

    There may be other PowerShell modules available from the PSGallery which use newer JSON libraries as well that you could utilize.

  8. xander-petty commented on Apr 12, 2021

    @xander-petty

    I'm also using Powershell 5.1 and I'm still getting this problem. ConvertTo-Json creates an useable url.
    Is your answer to this in my world very critical error really to use PSCore6?
    $json.Replace("\u0026", "&")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions