1

I am trying to append a string to the JSON content extracted from a blob. The JSON content is similar to below.

    {
      "Subject": "Details",
      "FromAddress": "[email protected]",
      "ToAddress": "[email protected]",
      "Body": "My name is {name} and I faced this {exception} error"
    }

In this, I have to append a name (e.g. Arvind) and an exception (e.g. SystemException) and send the formatted data to another method to send mail

string data = EmailBlob.DownloadTextAsync().Result;
EmailData emailData = JsonConvert.DeserializeObject<EmailData>(data);

The variable data holds the following JSON

{
  "Subject": "Details",
  "FromAddress": "[email protected]",
  "ToAddress": "[email protected]",
  "Body": "My name is {name} and I faced this {exception} error"
}

The variable emailData holds the below.

  Subject: "Details",
  FromAddress: "[email protected]",
  ToAddress: "[email protected]",
  Body: "My name is {name} and I faced this {exception} error"

Now I have to update the name 'Arvind' and exception 'SystemException' and send the emaildata variable to another method as below.

  Subject: "Details",
  FromAddress: "[email protected]",
  ToAddress: "[email protected]",
  Body: "My name is Arvind and I faced this SystemException error"

How could I achieve it using C#.NET?

3
  • 3
    why not just use Replace method like Body.Replace("{name}","Arvind") Commented Mar 5, 2020 at 10:10
  • Possibly a duplicate of this stackoverflow.com/questions/43288857/… Commented Mar 5, 2020 at 10:18
  • string data = EmailBlob.DownloadTextAsync().Result; should be awaiting this Commented Mar 6, 2020 at 18:49

3 Answers 3

3
             EmailData.Body = EmailData.Body
                             .Replace("{name}", "Arvind")
                             .Replace("{exception}", "SystemException ");
Sign up to request clarification or add additional context in comments.

1 Comment

Please explain code rather than just dumping code here.
2

you can simply do it by String.Replace("old string","new string"); for your reference : https://learn.microsoft.com/en-us/dotnet/api/system.string.replace?view=netframework-4.8

    static void ReplaceExample()
    {
        //Method 1 - you can replace the value by data itself and you can deserialize to object
        string beforeDeserialize = EmailBlob.DownloadTextAsync().Result;
        beforeDeserialize = beforeDeserialize.Replace("{name}", "Arvind").Replace("{exception}", "SystemException ");
        EmailData emailDataM1 = JsonConvert.DeserializeObject<EmailData>(beforeDeserialize);
        //Method 2 - you can replace the valueafter deserialize
        string afterDeserialize = EmailBlob.DownloadTextAsync().Result;
        EmailData emailDataM2 = JsonConvert.DeserializeObject<EmailData>(afterDeserialize);
        emailDataM2.Body = emailDataM2.Body.Replace("{name}", "Arvind").Replace("{exception}", "SystemException ");
    }

Comments

0

1.

string name = "testName", exception "testException"

emailData.Body = emailData.Body.Replace("{name}", name);
emailData.Body = emailData.Body.Replace("{exception}", exception);

======================================

2.

  using System.Text.RegularExpressions;

Create variable and replace

string name = "testName", exception "testException"

emailData.Body = Regex.Replace(emailData.Body, "{name}", name);
emailData.Body = Regex.Replace(emailData.Body, "{exception}", exception );

=> "My name is testName and I faced this testException error"

1 Comment

Why are you using a RegEx replacement here? There are no regular expressions here.

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.