When I use the log module to log something, the log message prints escape characters instead of interpreting it. For instance, consider the following code.
import ballerina/log;
public function main() {
json jsonObject = {
"name": "John Doe",
"age": 30,
"city": "New York"
};
log:printInfo("Hello this is your json object: " + jsonObject.toJsonString());
}
When I run the above code, it logs the following.
time = 2023-11-09T15:15:10.935+05:30 level = INFO module = org/test message = "Hello this is your json object: {\"name\":\"John Doe\", \"age\":30, \"city\":\"New York\"}"
How can I make the program log the line without escape characters?
Since the JSON object is passed to the
messageparameter, it is converted to a JSON string, as themessageparameter expects to print a string as expected by the user. Since the use case is to print the JSON as it is (i.e., print the JSON as key-value pairs), the JSON parameter should be provided as apayloadparameter, as shown below.The above code generates the following output: