Error occur while passing json string

Sudheer961
Tera Contributor

When we create change request and when we keep the below content in "implementation plan" field. We are receiving the below error as part of sending the payload via rest message. This is a bidirectional integration. We are using business rule for this.

{"errorMessage":"Invalid payload","errorDetails":"'0x09' is invalid within a JSON string. The string should be correctly escaped. Path: $.u_implementation_plan | LineNumber: 18 | BytePositionInLine: 32."}

-> In business rule, we are using below method for those type of fields:

  function jsonEncode(str) {
        str = new JSON().encode(str);
        return str.substring(1, str.length - 1);
    }
 r.setStringParameterNoEscape('implementation_plan', jsonEncode(current.getDisplayValue('implementation_plan')));

 

Sudheer961_0-1718120496265.png

 

2 REPLIES 2

Community Alums
Not applicable

Hi @Sudheer961 ,

 

The issue is caused by an invalid character within the JSON string. Specifically, the error message indicates that the 0x09 character which is a horizontal tab.

 

Please update the function with the below code-

function jsonEncode(str) {
    // Replace tab characters with \t
    str = str.replace(/\t/g, '\\t');
    // Replace newline characters with \n
    str = str.replace(/\n/g, '\\n');
    // Replace carriage return characters with \r
    str = str.replace(/\r/g, '\\r');
    // Encode the string as JSON
    str = new JSON().encode(str);
    // Remove the surrounding quotes
    return str.substring(1, str.length - 1);
}

This will ensure removing invalid characters and replacing with appropriate characters.

 

If my response has resolved your query, please consider giving it a thumbs up ‌‌ and marking it as the correct answer‌‌!

 

Thanks & Regards,

Sanjay Kumar

 

Community Alums
Not applicable

Hi @Sudheer961 ,

 

Did the above solution resolve your query??

 

If my response has resolved your query, please consider giving it a thumbs up ‌‌ and marking it as the correct answer‌‌!

 

Thanks & Regards,

Sanjay Kumar