How to Save JSON in Pretty Format in EspoCRM Text Field?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sapyantuk
    Senior Member
    • Oct 2020
    • 265

    How to Save JSON in Pretty Format in EspoCRM Text Field?

    I'm trying to save JSON data in a text field in EspoCRM, but it currently saves in a raw, single-line format. I’d like it to be saved in a "pretty" format with proper indentation for better readability.

    Is there a way to ensure that JSON data is stored in a formatted, indented way when saved in EspoCRM? Any suggestions or code examples on how to achieve this would be appreciated!
  • lazovic
    Super Moderator
    • Jan 2022
    • 809

    #2
    Hi sapyantuk,

    You can use the following formula script (you can test in the Administration > Formula Sandbox):
    Code:
    $origJsonString = '{"name":"John","age":30,"address":{"street":"123 Main St","city":"New York"},"phones":["+1234567890","2322232"],"email":"john.doe@example.com","isActive":true,"roles":["user","testing"],"profile":{"preferences":{"notifications":true},"settings":{"language":"en"}}}';
    
    $jsonString = string\replace($origJsonString, '{', '{\n');                
    $jsonString = string\replace($jsonString, '}', '\n}');                
    $jsonString = string\replace($jsonString, ':', ': ');                
    $jsonString = string\replace($jsonString, ',', ',\n');      
    $jsonString = string\replace($jsonString, '": ["', '": [\n"');
    $jsonString = string\replace($jsonString, '"],', '"\n],');
    
    
    output\printLine($origJsonString);
    output\printLine('\n');
    output\printLine($jsonString);​
    ​​​​Click image for larger version

Name:	image.png
Views:	44
Size:	52.8 KB
ID:	112192
    Last edited by lazovic; 11-11-2024, 03:01 PM.

    Comment

    Working...