Json Encoding HTML string

I am currently creating a JSON file from a PowerShell script, but it displays Unicode instead of special characters such as '<' I need HTML code in LinkText, but not sure how to change the encoding.

This is the result I get:

[
    {
        "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 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"
+5
source share
2 answers

I do not see a built-in parameter to prevent this conversion. Here is a workaround that converts Unicode characters:

[regex]::replace($json,'\\u[a-fA-F0-9]{4}',{[char]::ConvertFromUtf32(($args[0].Value -replace '\\u','0x'))})
+2
source

Out-File is not a criminal here, he writes exactly what ConverTo-Json produces. However, if you pass your output to ConvertFrom-Json, it works fine. Are you sure this is a problem?

0
source

All Articles