Answered by:
Concatenate into json

Question
-
User-1852975005 posted
Hi,
I have code that uses the above line. To send a request in json (in this format).string POSTStringCon = "{\r\n \"action\": \"read\",\r\n \"header\": {\r\n\t\t\"requestor\": \"WorkOrderCreateService\",\r\n\t\t\"dateTime\": \"DD/MM/YYYY HH:MM:SS\",\r\n\t\t\"messagePriority\": \"1\",\r\n\t\t\"correlationId\": \"\"\r\n\t},\r\n \"payload\": {\r\n\t \"workOrder\": {\r\n \t\"workOrder\": \"02006523\"\r\n \r\n\t\t}\r\n }\r\n}";
I now need to concatenate the below variable values into the string.
string saction = "read"; i.e. replace read with saction
string sworkOrder = "02006523"; i.e. replace 02006523 with sworkOrder
Tuesday, May 5, 2020 11:54 PM
Answers
-
User-1330468790 posted
Hi NewToDotyNet,
The below code still does not manipulate the string?I am not sure what you mean "manipulate the string"?
You cannot directly manipulate the string anyway. What you could do is to assign a new string to the original string variable.
In your case, I think the codes are correct and should be working as expected.
You could manipulate the JObject instance "jObject" and assign its value to "POSTString" as below codes.
////Modify the value using JObject JObject jObject = JObject.Parse(POSTStringCon); JToken workOrder = jObject.SelectToken("payload.workOrder.workOrder"); workOrder.Replace("01809200"); // This PostString will be changed as jObject's content string POSTString = jObject.ToString();
Best regards,
Sean
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, May 8, 2020 7:00 AM
All replies
-
User415553908 posted
I am not entirely sure whether you want to add more properties or to replace existing values, but when using JSON.Net library, both are quite easy to achieve:
string POSTStringCon = "{\r\n \"action\": \"read\",\r\n \"header\": {\r\n\t\t\"requestor\": \"WorkOrderCreateService\",\r\n\t\t\"dateTime\": \"DD/MM/YYYY HH:MM:SS\",\r\n\t\t\"messagePriority\": \"1\",\r\n\t\t\"correlationId\": \"\"\r\n\t},\r\n \"payload\": {\r\n\t \"workOrder\": {\r\n \t\"workOrder\": \"02006523\"\r\n \r\n\t\t}\r\n }\r\n}"; JObject obj = JsonConvert.DeserializeObject(POSTStringCon) as JObject; // add more keys to your object obj.Add("saction", new JValue("read")); obj.Add("sworkOrder", new JValue("02006523")); // replace existing values: obj.SelectToken("action").Replace(new JValue("read!")); obj.SelectToken("payload.workOrder.workOrder").Replace(new JValue("00000")); //serialise back var result = JsonConvert.SerializeObject(obj); Console.WriteLine(result);
Wednesday, May 6, 2020 3:40 AM -
User-1330468790 posted
Hi NewToDotyNet,
Could you please confirm if you want to handle this using c#?
If so, I suggest you use Newtonsoft.Json package to edit the JSON string via JObject.
More details, you could refer to below Console App codes.
static void Main(string[] args) { //Original JSON string string POSTStringCon = "{\r\n \"action\": \"read\",\r\n \"header\": {\r\n\t\t\"requestor\": \"WorkOrderCreateService\",\r\n\t\t\"dateTime\": \"DD/MM/YYYY HH:MM:SS\",\r\n\t\t\"messagePriority\": \"1\",\r\n\t\t\"correlationId\": \"\"\r\n\t},\r\n \"payload\": {\r\n\t \"workOrder\": {\r\n \t\"workOrder\": \"02006523\"\r\n \r\n\t\t}\r\n }\r\n}"; Console.WriteLine(POSTStringCon); //Modify the value using JObject JObject jObject = JObject.Parse(POSTStringCon); string saction = "read1"; // Replace action string sworkOrder = "0200XXXX"; // Replace workOrder jObject["action"] = saction; JToken workOrder = jObject.SelectToken("payload.workOrder.workOrder"); workOrder.Replace(sworkOrder); Console.WriteLine(jObject.ToString()); Console.ReadLine(); }
Result:
Hope this can help you.
Best regards,
Sean
Wednesday, May 6, 2020 3:46 AM -
User-1852975005 posted
Hi Sean,
I think your example works. But when I try to implement it in the real project, it doesn't make any changes to the string.
string POSTStringCon = "{\r\n \"action\": \"read\",\r\n \"header\": {\r\n\t\t\"requestor\": \"WorkOrderCreateService\",\r\n\t\t\"dateTime\": \"DD/MM/YYYY HH:MM:SS\",\r\n\t\t\"messagePriority\": \"1\",\r\n\t\t\"correlationId\": \"\"\r\n\t},\r\n \"payload\": {\r\n\t \"workOrder\": {\r\n \t\"workOrder\": \"02006523\"\r\n \r\n\t\t}\r\n }\r\n}"; //Modify the value using JObject JObject jObject = JObject.Parse(POSTStringCon); jObject["action"] = saction; JToken requestor = jObject.SelectToken("payload.workOrder.workOrder"); requestor.Replace(srequestor); JToken dateTime = jObject.SelectToken("payload.workOrder.workOrder"); dateTime.Replace(sdateTime); JToken messagePrioty = jObject.SelectToken("payload.workOrder.workOrder"); messagePrioty.Replace(smessagePriority); JToken workOrder = jObject.SelectToken("payload.workOrder.workOrder"); workOrder.Replace(sworkOrder); string POSTString = POSTStringCon;
Wednesday, May 6, 2020 10:13 PM -
User415553908 posted
string POSTString = POSTStringCon;
you are assigning it back to original string.
try serialising the jObject:
string POSTString = JsonConvert.SerializeObject(jObject);
Thursday, May 7, 2020 1:21 AM -
User-1852975005 posted
Hi,
To try and make things clearer.
The code works. Only it reads WO - 02006523.
The code does not seem able to manipulate the work order to another number, of the string - PostStringCon.
Thursday, May 7, 2020 4:21 AM -
User-1330468790 posted
Hi NewToDotyNet,
The code works. Only it reads WO - 02006523.This is because you did assignment four times for the same field "payload.workOrder.workOrder" and only the last one will be stored in this field.
JToken requestor = jObject.SelectToken("payload.workOrder.workOrder"); requestor.Replace(srequestor); JToken dateTime = jObject.SelectToken("payload.workOrder.workOrder"); dateTime.Replace(sdateTime); JToken messagePrioty = jObject.SelectToken("payload.workOrder.workOrder"); messagePrioty.Replace(smessagePriority); JToken workOrder = jObject.SelectToken("payload.workOrder.workOrder"); workOrder.Replace(sworkOrder);
If you want to change more fields, you should change the path for the method "SelectToken" like below codes.
static void Main(string[] args) { //Original JSON string string POSTStringCon = "{\r\n \"action\": \"read\",\r\n \"header\": {\r\n\t\t\"requestor\": \"WorkOrderCreateService\",\r\n\t\t\"dateTime\": \"DD/MM/YYYY HH:MM:SS\",\r\n\t\t\"messagePriority\": \"1\",\r\n\t\t\"correlationId\": \"\"\r\n\t},\r\n \"payload\": {\r\n\t \"workOrder\": {\r\n \t\"workOrder\": \"02006523\"\r\n \r\n\t\t}\r\n }\r\n}"; Console.WriteLine(POSTStringCon); //Modify the value using JObject JObject jObject = JObject.Parse(POSTStringCon); string saction = "read1"; // Replace action string srequestor = "WorkOrderCreateService1";//Replace requestor string sdateTime = new DateTime(2020, 1, 1).ToString("dd/MM/yyyy HH:mm:ss"); //Replace date time string smessagePriority = "2"; //Replace messagePriority string sworkOrder = "0200XXXX"; // Replace workOrder //Directly modify the value with [index] jObject["action"] = saction; //Modify the value using JToken JToken requestor = jObject.SelectToken("header.requestor"); requestor.Replace(srequestor); JToken dateTime = jObject.SelectToken("header.dateTime"); dateTime.Replace(sdateTime); JToken messagePrioty = jObject.SelectToken("header.messagePriority"); messagePrioty.Replace(smessagePriority); JToken workOrder = jObject.SelectToken("payload.workOrder.workOrder"); workOrder.Replace(sworkOrder); Console.WriteLine(jObject.ToString()); Console.ReadLine(); }
Result:
If you want add nodes for the JSON object, you could refer to below link:
Hope this can help you.
Best regards,
Sean
Thursday, May 7, 2020 7:14 AM -
User-1852975005 posted
Hi Sean,
apologies for the typos.
The below code still does not manipulate the string?
////Modify the value using JObject JObject jObject = JObject.Parse(POSTStringCon); //jObject["action"] = saction; //JToken requestor = jObject.SelectToken("payload.workOrder.requestor"); //requestor.Replace(srequestor); //JToken dateTime = jObject.SelectToken("payload.workOrder.dateTime"); //dateTime.Replace(sdateTime); //JToken messagePriority = jObject.SelectToken("payload.workOrder.messagePriority"); //messagePriority.Replace(smessagePriority); JToken workOrder = jObject.SelectToken("payload.workOrder.workOrder"); //workOrder.Replace(sworkOrder); workOrder.Replace("01809200");
Thursday, May 7, 2020 9:58 PM -
User-1330468790 posted
Hi NewToDotyNet,
The below code still does not manipulate the string?I am not sure what you mean "manipulate the string"?
You cannot directly manipulate the string anyway. What you could do is to assign a new string to the original string variable.
In your case, I think the codes are correct and should be working as expected.
You could manipulate the JObject instance "jObject" and assign its value to "POSTString" as below codes.
////Modify the value using JObject JObject jObject = JObject.Parse(POSTStringCon); JToken workOrder = jObject.SelectToken("payload.workOrder.workOrder"); workOrder.Replace("01809200"); // This PostString will be changed as jObject's content string POSTString = jObject.ToString();
Best regards,
Sean
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, May 8, 2020 7:00 AM