Skip to main content

Notifications

Community site session details

Community site session details

Session Id :
Power Automate - Building Flows
Suggested answer

How to send JSON in email body with nice readable formatting?

Like (0) ShareShare
ReportReport
Posted on 11 Dec 2024 10:33:30 by 4
hi
 
I would like to format JSON object in emails so it is easily read.
 
The email sends out information in case an error happens and looks like that.
 
 
 
 
 
 
Is there any easy way how I can send JSON - note that the structure can change - in a nice readable format?
 
 
 
I actually created a child flow that sends out this email in case any parent flow fails. We have like 20 flows with different data structure.
 
 
 
Thank you
 
Radek
  • Suggested answer
    RŠ-11121030-0 Profile Picture
    4 on 13 Dec 2024 at 21:06:26
    How to send JSON in email body with nice readable formatting?
    @Ninjasabi
    {
    "Key1": "Value1",
    "Key2": "Value2",
    "Key3": {
      "SubKey1": "SubValue1",
      "SubKey2": "SubValue2"
      }
    }
     
     
    could I have it formatted - see above - instead of
    {
    "Key1": "Value1",
    "Key2": "Value2",
    "Key3": {
    "SubKey1": "SubValue1",
    "SubKey2": "SubValue2"
    }
    }
    this way? So the nesting is more "visible"?
     
    Thank you
    Radek
  • Suggested answer
    Ninjasabi Profile Picture
    120 on 13 Dec 2024 at 12:44:38
    How to send JSON in email body with nice readable formatting?

    To send a JSON object in an email with readable formatting, you can use Power Automate's HTML capabilities to structure the JSON in a way that is easy to read. Here's how you can achieve it:


    Step-by-Step Solution

    1. Convert JSON to a Readable Format

    • Use the "Compose" action to pretty-print the JSON.
    • Use the json() and string() functions:
      json(string(<Your JSON Object>))
      This ensures the JSON is formatted correctly, even if the structure changes.

    2. Format JSON with Line Breaks for Readability

    • Add the "Compose" action to insert a prettified version of the JSON object:
      • Expression to format JSON:
        replace(string(<Your JSON Object>), ',', ',<br>')
      • This adds line breaks after each key-value pair.

    3. Build the Email Body

    Use the "Send an email" action to create a well-structured email body.

    1. Subject: Add a relevant subject line.
    2. Body: Use dynamic content or the Compose output to embed the JSON.
      • Example:
        <p>An error occurred in one of the flows. Below is the JSON object:</p>
        <pre style="background-color:#f4f4f4;padding:10px;border:1px solid #ddd;border-radius:5px;">
        @{outputs('Compose')}
        </pre>

    Automating for Multiple Flows

    Since you’re using a child flow for error handling, ensure that:

    1. Pass the JSON Object to the Child Flow: Add the JSON object as an input parameter to the child flow.
    2. Dynamic Email Body in the Child Flow:
      • Use the replace expression (as shown earlier) to format the JSON dynamically.
      • Embed the formatted JSON in the email template.

    Example Output in the Email

    The JSON will appear nicely formatted and easy to read:

    {
    "Key1": "Value1",
    "Key2": "Value2",
    "Key3": {
    "SubKey1": "SubValue1",
    "SubKey2": "SubValue2"
    }
    }

    Advanced Option: HTML Table Conversion

    If you want a cleaner look, convert the JSON into an HTML table for emails:

    1. Use the "Create HTML Table" action:

      • Input: Your JSON object.
      • Output: A formatted table that you can embed directly into the email.
    2. Embed the table in the email body:

      <p>An error occurred in the flow. Below is the JSON data:</p>
      @{outputs('Create_HTML_table')}

    Final Tips

    • For unpredictable JSON structures, stick to prettified formatting with line breaks (<pre> tags work well).
    • Use a consistent design with child flows to handle multiple parent flows efficiently.

    Let me know if you’d like a detailed walkthrough of any part! 😊

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Understanding Microsoft Agents - Introductory Session

Confused about how agents work across the Microsoft ecosystem? Register today!

Warren Belz – Community Spotlight

We are honored to recognize Warren Belz as our May 2025 Community…

Congratulations to the April Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard > Power Automate - Building Flows

#1
stampcoin Profile Picture

stampcoin 79

#2
Michael E. Gernaey Profile Picture

Michael E. Gernaey 70 Super User 2025 Season 1

#3
David_MA Profile Picture

David_MA 48 Super User 2025 Season 1

Overall leaderboard