-1

I am trying to send a json in an email and one of the biggest challenge is to render this json as a multi-line string in a HTML. Currently, the json is coming into the email as a single line json which is not what i want. Since, I am doing this in C#, I have an extension method like this:

public static string ApplyHTMLTempate(this string info, PayloadType payloadType, string error = null, string jsonData=null)
{
    string strRegex = @"(?<=[a-z])([A-Z])|(?<=[A-Z])([A-Z][a-z])";
    Regex myRegex = new Regex(strRegex, RegexOptions.None);
    string strReplace = @" $1$2";

    //string formattedData = string.IsNullOrWhiteSpace(data) ? "" : $"<pre>{WebUtility.HtmlEncode(data)}</pre>";

    return $@"<!DOCTYPE html>
    <html lang=""en"">
    <head>
        <meta charset=""UTF-8"">
        <meta name=""viewport"" content=""width=device-width, initial-scale=1.0"">
        <title>Error Message</title>
        <style>
            body {{font - family: Arial, sans-serif;
                        margin: 0;
                        padding: 20px;
                        background-color: #f7f7f7;
                    }}
                    .container {{max - width: 600px;
                        margin: 0 auto;
                        background-color: white;
                        padding: 20px;
                        border-radius: 5px;
                        box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
                    }}
                    pre {{background - color: #f2f2f2;
                        padding: 10px;
                        border-radius: 5px;
                        overflow: auto;
                    }}
        </style>
    </head>
    <body>
        <div class=""container"">
            <p><strong>{info}: {error}</strong></p>
            <p></p>
            <p>{myRegex.Replace(payloadType.ToString(), strReplace)} Payload:</p>
            <pre>{jsonData}</pre>
        </div>
    </body>
    </html>";
}

In the above code, you can see that jsonData is rendered as a single line. Here's an example of my json data:

{"UserEmail":"string","CRMId":"string","CRMCustomerId":"string","Transaction":{"EffectiveDate":"string","Id":"string","BillMethodTransaction":"string","Description":"string","EnteredDate":"string","PaymentPlanId":"string","PremiumOnEffectivDate":0,"ReasonTransaction":"string","Source":"string","SourceDescription":"string","TransactionType":"string","EstRevenuePercent":0},"LineOfBusiness":{"EffectiveDate":"string","LineOfBusinessId":"string","Id":"string","ApplicationCreatedDate":"string","Category":0,"Description":"string","ExpirationDate":"string","LineOfBusiness":"string","LineOfBusinessName":"string","SortNumber":0,"UICodeLineOfBusinessSetup":"string","WritingCompanyCode":"string","ElfFormVersionId":"string","CompanyPlanType":"string","StatePlanType":"string","Application":"string","ApplicationVersion":"string","EntityLink":"string"},"":{"Id":"string","AgencyBusinessClassification":"string","AgencyNotationId":"string","BillAccountingNumber":"string","BillMethod":"string","BrokerCode":"string","BusOriginCode":"string","CompanyCode":"string","CompanyCustomerNumber":"string","CompanyName":"string","CompanyType":"string","CompanyUrl":"string","CsrCode":"string","CustomerId":"string","Description":"string","EffectiveDate":"string","ExecCode":"string","ExpiryDate":"string","FirstWrittenDate":"string","FlatAmount":0,"FullTermPremium":0,"GLBranchCode":"string","GLDepartmentCode":"string","GLDivisionCode":"string","GLGroupCode":"string","IsContinuous":true,"IsExclDelete":true,"IsFinanced":true,"IsMultiEntity":true,"IsNewBusiness":true,"IsWritingCompanyLogoAvailable":true,"MasterAgencyCode":"string","Method":"string","MultiEntityARFlag":"string","NationalProducerCode":0,"NegotiatedCommissionsValidDate":"string","Notation":"string","ParentCompany":"string","PaymentPlanId":"string","Percentage":0,"Number":"string","SubType":"string","Type":"string","TypeLOB":"string","PremiumAdjustment":"string","Prior":"string","PriorId":"string","RenewalReportFlag":"string","SourceId":"string","SubAgentCode":"string","TotalCost":0,"TypeOfBusiness":0,"Underwriter":"string","WritingCompanyCode":"string","WritingCompanyName":"string","Status":"string","ChangedBy":"string","LineOfBusinessIDs":"string","MaxEffectiveDateTransaction":"string","TransactNowButton":true,"TypeOfBusinessDisplay":"string","UILineOfBusinessCodes":"string"},"Customer":{"CustomerId":"string","AccountExecCode":"string","AccountExecCodeDisplay":"string","AccountRepCode":"string","AccountRepCodeDisplay":"string","AddressLine1":"string","AddressLine2":"string","AutoApplyPay":"string","BillAddress1":"string","BillAddress2":"string","BillCity":"string","BillCounty":"string","BillName":"string","BillState":"string","BillZipCode":"string","BrokerCode":"string","BrokerCodeDisplay":"string","BusinessAreaCode":"string","BusinessEntity":"string","BusinessExtension":"string","BusinessPhone":"string","CellAreaCode":"string","CellExtension":"string","CellPhone":"string","City":"string","CollectionLetter":true,"County":"string","CustomerNotation":"string","CustomerNotationId":"string","CustomerNumber":0,"CustomerType":"string","CustomerWebAddress":"string","DateCustomerAdded":"string","DateOfBirth":"string","DoingBusinessAs":"string","DriversLicense":"string","DUNSNo":"string","EMail":"string","EMail2":"string","FaxAreaCode":"string","FaxExtension":"string","FaxPhone":"string","FederalIDNo":"string","FirmName":"string","FirstName":"string","FormalSalutation":"string","GLBranchCode":"string","GLBranchName":"string","GLCode":"string","GLDepartmentCode":"string","GLDepartmentName":"string","GLDivisionCode":"string","GLDivisionName":"string","GLGroupCode":"string","GLGroupName":"string","GroupingOption":"string","HomeAreaCode":"string","HomeExtension":"string","HomePhone":"string","InBusinessSince":"string","InformalSalutation":"string","IsActive":true,"IsBenefitCustomer":true,"IsBillAddressSameAsCustomer":true,"IsBillNameSameAsCustomer":true,"IsBrokerCustomer":true,"IsCommercialCustomer":true,"IsDeriveAttrFlagsCust":true,"IsExclDelete":true,"IsFinancialCustomer":true,"IsHealthCustomer":true,"IsLifeCustomer":true,"IsNonPropertyAndCasualtyCustomer":true,"IsPersonalCust":true,"IsPrintAgencyBill":true,"IsPrintDirectBill":true,"IsSecured":true,"KnownSince":"string","Last":"string","LateCharge":true,"Latitude":0,"Longitude":0,"MaritalStatus":"string","MarketingFlag":true,"MasterCustomerId":"string","MasterFullName":"string","MasterSubExists":true,"MasterSubTrack":"string","MasterSubType":"string","MiddleName":"string","NAICS":"string","Occupation":"string","OtherAreaCode":"string","OtherExtension":"string","OtherPhone":"string","PagerAreaCode":"string","PagerExtension":"string","PagerPhone":"string","PremiumOption":"string","PrintCustomerStatement":true,"ReportOption":"string","SIC":"string","SSN":"string","State":"string","StatePrintGroup":"string","TypeName":"string","YearEmployed":"string","ZipCode":"string","ChangedBy":"string","AccountBalance":0,"MethodOfDistribution":"string","BusinessOriginCode":"string","ModifiedDate":"string","Sex":"string"}}

What is the best way to render this jsonData as multiple lines in an email?

Fast Chip
  • 425
  • 1
  • 4
  • 16
  • What does jsonData look like? Is it indented with line breaks? – dbc Aug 31 '23 at 04:24
  • If your JSON is unformatted and you would like to pretty-print it, assuming you are using .NET Core 3.1 or later see [How to pretty print using System.Text.Json for unknown object](https://stackoverflow.com/q/65620631). – dbc Aug 31 '23 at 04:27
  • That link really helped me a lot. I greatly appreciate your help. Thanks – Fast Chip Aug 31 '23 at 04:42

0 Answers0