Home > Software design >  & sign is converted into \u0026 through powershell
& sign is converted into \u0026 through powershell

Time:09-29

I have below code:

$getvalue= 'true&replicaSet=users-shard-0&authSource=adsfsdfin&readPreference=neasrest&maxPoolSize=50&minPoolSize=10&maxIdleTimeMS=60'
$getvalue = $getvalue -replace '&','&'
$pathToJson = 'C:\1\test.json'
$a = Get-content -Path $pathToJson | ConvertFrom-Json
$a.connectionStrings.serverstring=$getvalue
$a | ConvertTo-Json | Set-content $pathToJson -ErrorAction SilentlyContinue

I got below result:

true\u0026replicaSet=users-shard-0\u0026authSource=adsfsdfin\u0026readPreference=neasrest\u0026maxPoolSize=50\u0026minPoolSize=10\u0026maxIdleTimeMS=60

There & sign converted into \u0026. How to prevent covert value. You can take reference from this question

I need & sign in json file instead of \u0026

CodePudding user response:

Windows PowerShell's ConvertTo-Json unexpectedly serializes & to its equivalent Unicode escape sequence (\u0026); ditto for ', < and > (fortunately, this no longer happens in PowerShell (Core) 7 ) - while unexpected and hindering readability - this isn't a problem for programmatic processing, since JSON parsers, including ConvertFrom-Json do recognize such escape sequences:

($json = 'a & b' | ConvertTo-Json) # -> `"a \u0026 b"` (WinPS)
ConvertFrom-Json $json # -> verbatim `a & b`, i.e. successful roundtrip

If you do want to convert such escape sequences to the verbatim character they represent:

  • This answer to the linked question shows a robust, general string-substitution approach.

  • However, in your case - given that you know the specific and only Unicode sequence to replace and there seems to be no risk of false positives - you can simply use another -replace operation:

$getvalue= 'true&amp;replicaSet=users-shard-0&amp;authSource=adsfsdfin&amp;readPreference=neasrest&amp;maxPoolSize=50&amp;minPoolSize=10&amp;maxIdleTimeMS=60'
$getvalue = $getvalue -replace '&amp;','&'

# Simulate reading an object from a JSON
# and update one of its properties with the string of interest.
$a = [pscustomobject] @{
  connectionStrings = [pscustomobject] @{
    serverstring = $getValue
  }
} 

# Convert the object back to JSON and translate '\\u0026' into '&'.
# ... | Set-Content omitted for brevity.
($a | ConvertTo-Json) -replace '\\u0026', '&'

Output (note how the \u0026 instance were replaced with &):

{
  "connectionStrings": {
    "serverstring": "true&replicaSet=users-shard-0&authSource=adsfsdfin&readPreference=neasrest&maxPoolSize=50&minPoolSize=10&maxIdleTimeMS=60"
  }
}

You can cover all problematic characters - & ', < and > - with multiple -replace operations:

  • However, if you need to rule out false positives (e.g., \\u0026), the more sophisticated solution from the aforementioned answer is required.
# Note: Use only if false positives aren't a concern.

# Sample input string that serializes to:
#   "I\u0027m \u003cfine\u003e \u0026 dandy."
($json = "I'm <fine> & dandy." | ConvertTo-Json)

# Transform the Unicode escape sequences for chars. & ' < >
# back into those chars.
$json -replace '\\u0026', '&' -replace '\\u0027', "'" -replace '\\u003c', '<' -replace '\\u003e', '>'
  • Related