How to merge list of string json object into one list in c#

Viewed 57

I have a list of string JSON items and I have to make them into one list with multiple items. currently, I see the list count is 2 but the first Item, itself has two JSON objects

[0] = "[\n  {\n    \"inputData\": {\n      \"CustomerID\": 1364,\n      \"BookingID\": 63100,\n      \"Amount\": \"1163\",\n      \"BookingID\": \"03200\",\n      \"ProductNumber\": \"11600500\",\n      \"ProductCode\": \"\"\n    },\n    \"response\": [\n      \n    ]\n  },\n  {\n    \"inputData\": {\n      \"CustomerID\": 1364,\n      \"BookingID\": 6330,\n      \"Amount\": \"451\",\n      \"BookingID\": \"0320100\",\n      \"ProductNumber\": \"235160050\",\n      \"ProductCode\": \"\"\n    },\n    \"response\": [\n      \n    ]\n  }\n]"

[1]= "[\n  {\n    \"inputData\": {\n      \"CustomerID\": 6364,\n      \"BookingID\": 1300,\n      \"Amount\": \"1463\",\n      \"BookingID\": \"700100\",\n      \"ProductNumber\": \"5160050000\",\n      \"ProductCode\": \"\"\n    },\n    \"response\": [\n      \n    ]\n  }\n]"

MY expected output is I have to get one list of JSON strings with a count of 3

"[\n  {\n    \"inputData\": {\n      \"CustomerID\": 1364,\n      \"BookingID\": 63100,\n      \"Amount\": \"1163\",\n      \"BookingID\": \"03200\",\n      \"ProductNumber\": \"11600500\",\n      \"ProductCode\": \"\"\n    },\n    \"response\": [\n      \n    ]\n  },\n  {\n    \"inputData\": {\n      \"CustomerID\": 1364,\n      \"BookingID\": 6330,\n      \"Amount\": \"451\",\n      \"BookingID\": \"0320100\",\n      \"ProductNumber\": \"235160050\",\n      \"ProductCode\": \"\"\n    },\n    \"response\": [\n      \n    ]\n  },\n{\n    \"inputData\": {\n      \"CustomerID\": 6364,\n      \"BookingID\": 1300,\n      \"Amount\": \"1463\",\n      \"BookingID\": \"700100\",\n      \"ProductNumber\": \"5160050000\",\n      \"ProductCode\": \"\"\n    },\n    \"response\": [\n      \n    ]\n  }\n]"

I tried below but it's not working

var result = list.SelectMany(x=>x).ToList();

Could someone help me out with this? Thanks in advance.

1 Answers

Are you trying to achive something like this?

public class Program
 {
      public static void Main()
      {
         var firstString = JArray.Parse("[\n  {\n    \"inputData\": {\n      \"CustomerID\": 1364,\n      \"BookingID\": 63100,\n      \"Amount\": \"1163\",\n      \"BookingID\": \"03200\",\n      \"ProductNumber\": \"11600500\",\n      \"ProductCode\": \"\"\n    },\n    \"response\": [\n      \n    ]\n  },\n  {\n    \"inputData\": {\n      \"CustomerID\": 1364,\n      \"BookingID\": 6330,\n      \"Amount\": \"451\",\n      \"BookingID\": \"0320100\",\n      \"ProductNumber\": \"235160050\",\n      \"ProductCode\": \"\"\n    },\n    \"response\": [\n      \n    ]\n  }\n]");

         var secondString = JArray.Parse("[\n  {\n    \"inputData\": {\n      \"CustomerID\": 6364,\n      \"BookingID\": 1300,\n      \"Amount\": \"1463\",\n      \"BookingID\": \"700100\",\n      \"ProductNumber\": \"5160050000\",\n      \"ProductCode\": \"\"\n    },\n    \"response\": [\n      \n    ]\n  }\n]");

         var mergedString = new JArray(firstString.Union(secondString));

         Console.WriteLine(mergedString);
       }
}

Results

enter image description here

Related