Translate

New Live Courses

🚀 *Gen AI Engineer Telugu (Production Focused)*
🗓️ *Date:* 30th Sept 2026, 07:00AM IST
📝 *Register Now!:*
https://www.vlrt.in/gr
👥 *Join WA Community:*
https://www.vlrt.in/gw
💡*Course Content:*
https://www.vlrt.in/ai
▶️ *Demo Videos:*
https://www.vlrt.in/gv

🚀 *Service now Admin/ Development (ITSM)FREE Demo in Telugu*
🗓️ *Date:* 30th Sept 2026, 08:00AM IST
📝 *Register Now!:*
https://www.vlrt.in/7r
👥 *Join WA Community:*
https://www.vlrt.in/7w
💡*Course Content:*
https://www.vlrt.in/7c
▶️ *Demo Videos:*
https://www.vlrt.in/7v

🚀 *Vulnerability Management Training Demo*
🗓️ *Date:* 30th Sept 2026, 09:00 AM IST
📝*Register Now!:*
https://www.vlrt.in/vr
👥 *Join Community:*
https://www.vlrt.in/cw
💡 *Course Content:*
https://www.vlrt.in/vc
▶️ *Demo Videos:*
https://www.vlrt.in/Vm

Thursday, 14 March 2024

How to remove the indentation from json output ? in MuleSoft 131

  How to remove the indentation from json output ? in MuleSoft


Here are two primary methods to remove indentation from the JSON output in MuleSoft 4:

Method 1: Using DataWeave:

DataWeave provides a built-in way to control the output formatting, allowing you to remove indentation.



%dw 2.0
---
// Sample JSON object
var data = {
  "name": "John",
  "age": 30,
  "city": "New York"
};

// Set the output format to compact (no indentation)
output application/json compact

---
write(data, application/json);

Explanation:

  1. Define the JSON object (data).

  2. Set the output format using the output directive:

  • application/json: Specifies the content type as JSON.

  • compact: Instructs DataWeave to produce a compact JSON output without indentation.

  1. The write function writes the data object to the output stream in the specified format (JSON without indentation).

Method 2: Using MEL (Message Expression Language):

While MEL doesn't directly offer formatting options, you can achieve this by leveraging a transformer component. Here's an example:


XML


<transformer ref="JSONTransformer">
  <input doc:name="Input Payload" />
  <output doc:name="Output Payload">#[payload.toString().replaceAll('\n', '')]</output>
</transformer>

Explanation:

  1. A JSON Transformer component is used to modify the payload.

  2. The input property references the incoming payload.

  3. The output property defines the transformed payload:

  • #[payload.toString()]: Converts the payload to a string representation.

  • .replaceAll('\n', ''): Removes all newline characters (\n) from the string, effectively removing indentation.

Choosing the Right Approach:

  • DataWeave: This is the preferred method as it offers a more declarative and concise approach to control the output formatting directly within your DataWeave script.

  • MEL: While functional, the MEL approach requires an additional transformer component and might be less readable for complex formatting requirements.

Additional Considerations:

  • Ensure the payload is a valid JSON object before applying these methods.

  • Consider using a consistent formatting approach throughout your Mule application for better maintainability.

Here are some helpful resources for further exploration:

Remember that using DataWeave provides a more streamlined and efficient way to manage JSON output formatting within your MuleSoft 4 applications.


No comments:

Post a Comment

Note: only a member of this blog may post a comment.