Pretty format json?

lock
push_pin
done
Answered
4

Is it possible to pretty format a json string: Map m = Map(data); printLine(m.toJson());

Outputs: { "First": "Martin", "Last": "Andersen"}

I would like to have it like this:

{
  "First": "Martin",
  "Last": "Andersen"
}
 
Regards Martin

10 Jul 2026 | 10:54 AM

All Replies (4)

Not by default, but you can use the code shared by Sverre here: Simple JSON formatter in CRMScript

10 Jul 2026 | 11:05 AM

OK, that is good! Thank you!

10 Jul 2026 | 11:08 AM

Hi Martin, this should work:

#setLanguageLevel 4;

Map data;
data.insert("First", "Martin");
data.insert("Last", "Andersen");

printLine(parseJSON2(data.toJson()).toJSON(2));
20 Jul 2026 | 06:44 AM

Very nice, two options. One is a custom function which can be changed to suit your needs. The other is inbuilt and most likely much faster, but it needs to convert to XML and as such duplicate your data and use a lot of lists to make the xml manageable internally, and it will use a lot of memory. But if you have memory left, this will be faster and you don’t need to define a method.

Thank you both.

20 Jul 2026 | 07:55 AM

Add reply