6

I'm currently producing a JSON file from a PowerShell script but it is outputting Unicode instead of special characters such as '<' I need HTML in the LinkText but not sure how to change the encoding.

This is the output I'm getting:

[
    {
        "Id":  "187303",
        "LinkText":  "\u003cb style =color:#d11717;\u0027\u003eAnnual General Meeting (MEET)"
    },
    {
        "Id":  "187305",
        "LinkText":  "\u003cb style =color:#d11717;\u0027\u003eAnnual General Meeting (MEET)"
    }
]

This is the code that I'm using:

$(foreach ($row in $DataSet.Tables[0].Rows){  
    $stockShortName = $row[0].ToString().Trim()
    $id = $row[0].ToString().Trim()
    $linkText = "<b style =color:`#d11717;'>$event_description" 

    (New-Object PSObject |
     Add-Member -PassThru NoteProperty Id $id |
     Add-Member -PassThru NoteProperty LinkText $linkText 
    )
}) | ConvertTo-JSON | Out-File $OutputFile -Encoding "default"
nickhar
  • 19,981
  • 12
  • 60
  • 73
steven
  • 249
  • 2
  • 8
  • 16

2 Answers2

4

I don't see a built-in parameter to prevent that conversion from happening. Here's a workaround that converts back unicode characters:

[regex]::replace($json,'\\u[a-fA-F0-9]{4}',{[char]::ConvertFromUtf32(($args[0].Value -replace '\\u','0x'))})
Shay Levy
  • 121,444
  • 32
  • 184
  • 206
  • You can also use [regex]::Unescape() as explained here: http://stackoverflow.com/questions/29306439/powershell-convertto-json-problen-containing-special-characters. – Cary Apr 19 '17 at 21:25
0

Out-File is not a culprit here, it writes exactly what ConverTo-Json produces. However, if you pipe your output into ConvertFrom-Json, it works just fine. Are you sure it's a problem?

Serjx86
  • 520
  • 3
  • 12