0

I have following JSON object which is dynamically created and can contain special characters in the names e.g. O'Brian in Last Name and special characters in Notes.

$jsonData = @"
{
        "ReqId": 37,
        "First Name": "John",
        "Middle Name": "",
        "Last Name": "O'Brian",
        "Preferred Name": "John",
        "Date": "04/28/2021 05:00:00",
        "Email": "[email protected]",
        "Notes": "This is a note with Special characters"
}
"@ | ConvertTo-Json;

I want to use this JSON data and convert it to HTML table format. Then pass this HTML data as a value of the key in another JSON object which will then be sent to API.

$convData = ConvertTo-Html -InputObject ($jsonData);
$apiJson = @"
    {
        "Title" : "This is Test",
        "Description" : "This is Test Description.<br ><br /> $convData"
    }
    "@

$apiJson needs to be valid JSON so that API can parse it as a JSON object successfully.

I tried ConvertTo-HTML method but it is not working and gives empty table. Also I want just <table>...</table> part and need to remove other tags.

{
    "Test" : "This is Test",
    "description" : "This is Test.<br > <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//E
N"  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/x
html"> <head> <title>HTML TABLE</title> </head><body> <table> <colgroup><col/></colgroup> <tr>
<th>*</th></tr> <tr><td>539</td></tr> </table> </body></html>"
}

Need help. Thank you.

1 Answer 1

1
  1. Change ConvertTo-Json to ConvertFrom-Json - You want to convert the json to an object which can be converted into html table

  2. Add the -Fragment switch to the ConvertTo-Html in order to generate only the HTML table

_

$jsonData = @'
{
        "ReqId": 37,
        "First Name": "John",
        "Middle Name": "",
        "Last Name": "O'Brian",
        "Preferred Name": "John",
        "Date": "04/28/2021 05:00:00",
        "Email": "[email protected]",
        "Notes": "This is a note with Special characters"
}
'@ | ConvertFrom-Json

$convData = $jsonData | ConvertTo-Html -Fragment
$apiJson = @"
    {
        "Title" : "This is Test",
        "Description" : "This is Test Description.<br ><br /> $convData"
    }
"@
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.