JSON for logging

Noah Aas 505 Reputation points
2024-11-21T18:55:41.65+00:00

Hello,

I have the REST interface and would like to log the data, request, response. To make it clearer, correct JSON format. I notice this behavior. What would be a good solution for logging?

"serial":"233124325000000043","batchNum":"G77777789",   --> looks ok

"serial\":\"233124325000000030\",\"batchNum\":\"G77777789\"   --> looks wrong

Why this character   \"

string dataObjectsAsString = Newtonsoft.Json.JsonConvert.SerializeObject(dataObjects, Newtonsoft.Json.Formatting.Indented);
Log.WriteLog($"[CUSTOM][MES_Step2_ExecuteGetSerialAssignments] response=\n{dataObjectsAsString}");

{
  "Result": "{\"error\":0,\"msg\":\"\",\"product_info\":{\"order\":\"DWO-0001\",\"partnum\":\"8888887777\",\"partrev\":\"A3\",\"bom_number\":\"8888887777\",\"bom_revision\":\"A3\",\"cust_name\":null,\"cust_partnum\":null,\"cust_partrev\":null,\"cust_bom_number\":null,\"cust_bom_revision\":null,\"cust_upc_code\":null,\"mrk_partnum\":null,\"mrk_partrev\":null},\"carrier_id\":\"233124325000000025\",\"serial_list\":[{\"serial\":\"233124325000000025\",\"lotnum\":\"G77777789\",\"datecode\":\"243\",\"cust_serial\":null,\"cust_datecode\":null,\"country_of_origin\":\"USA\"},{\"serial\":\"233124325000000026\",\"lotnum\":\"G77777789\",\"datecode\":\"243\",\"cust_serial\":null,\"cust_datecode\":null,\"country_of_origin\":\"USA\"},{\"serial\":\"233124325000000027\",\"lotnum\":\"G77777789\",\"datecode\":\"243\",\"cust_serial\":null,\"cust_datecode\":null,\"country_of_origin\":\"USA\"},{\"serial\":\"233124325000000028\",\"lotnum\":\"G77777789\",\"datecode\":\"243\",\"cust_serial\":null,\"cust_datecode\":null,\"country_of_origin\":\"USA\"},{\"serial\":\"233124325000000029\",\"lotnum\":\"G77777789\",\"datecode\":\"243\",\"cust_serial\":null,\"cust_datecode\":null,\"country_of_origin\":\"USA\"},{\"serial\":\"233124325000000030\",\"batchNum\":\"G77777789\",\"datecode\":\"243\",\"cust_serial\":null,\"cust_datecode\":null,\"country_of_origin\":\"USA\"}]}",
  "Id": 36,
  "Exception": null,
  "Status": 5,
  "IsCanceled": false,
  "IsCompleted": true,
  "CreationOptions": 0,
  "AsyncState": null,
  "IsFaulted": false
}
var dataObjects = response.Content.ReadAsStringAsync();
Log.Write($"[CUSTOM][GetSerialAssignments] response=\n{dataObjects.Result}");
response=
{"error":0,"msg":"","product_info":{"order":"DWO-0001","partnum":"8888887777","partrev":"A3","bom_number":"8888887777","bom_revision":"A3","cust_name":null,"cust_partnum":null,"cust_partrev":null,"cust_bom_number":null,"cust_bom_revision":null,"cust_upc_code":null,"mrk_partnum":null,"mrk_partrev":null},"carrier_id":"233124325000000040","serial_list":[{"serial":"233124325000000040","batchNum":"G77777789","datecode":"243","cust_serial":null,"cust_datecode":null,"country_of_origin":"USA"},{"serial":"233124325000000041","batchNum":"G77777789","datecode":"243","cust_serial":null,"cust_datecode":null,"country_of_origin":"USA"},{"serial":"233124325000000042","batchNum":"G77777789","datecode":"243","cust_serial":null,"cust_datecode":null,"country_of_origin":"USA"},{"serial":"233124325000000043","batchNum":"G77777789","datecode":"243","cust_serial":null,"cust_datecode":null,"country_of_origin":"USA"},{"serial":"233124325000000044","batchNum":"G77777789","datecode":"243","cust_serial":null,"cust_datecode":null,"country_of_origin":"USA"},{"serial":"233124325000000045","batchNum":"G77777789","datecode":"243","cust_serial":null,"cust_datecode":null,"country_of_origin":"USA"}]}
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,056 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Jiale Xue - MSFT 46,876 Reputation points Microsoft Vendor
    2024-11-22T02:43:24.2766667+00:00

    Hi @Noah Aas ,Welcome to Microsoft Q&A,

    In JSON format, \" is an escape character used to represent a double quote (") in a string. When you serialize a JSON object to a string, if it contains nested JSON objects or strings, double quotes are escaped to ensure that the resulting string conforms to the JSON specification.

    If you want to output readable JSON in the log instead of escaped strings: In addition to using the third-party library Serilog, use the Formatting.Indented option to generate more readable log output.

    var dataObjects = JsonConvert.DeserializeObject(response.Content.ReadAsStringAsync().Result);
    var formattedJson = JsonConvert.SerializeObject(dataObjects, Formatting.Indented);
    Log.Write($"[CUSTOM][GetSerialAssignments] response=\n{formattedJson}");
    

    Best Regards,

    Jiale


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". 

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.