Batch JSON Arrays with LINQ Chunk Method
By FoxLearn 1/9/2025 3:18:48 AM 105
By utilizing the LINQ Chunk method, you can transform a one-dimensional array into a two-dimensional array (batches).
This method demonstrates how to split a JSON array of objects into smaller batches.
For example:
[ { "id": "101" }, { "id": "102" }, { "id": "103" }, { "id": "104" }, { "id": "105" }, { "id": "106" }, { "id": "107" } ]
Converted into batches of 2
[ [ { "id": "101" }, { "id": "102" } ], [ { "id": "103" }, { "id": "104" } ], [ { "id": "105" }, { "id": "106" } ], [ { "id": "107" } ] ]
You can use the following code.
using System; using System.Collections.Generic; using System.Linq; // Example JSON input as a string string jsonInputString = "[ { \"id\": \"101\" }, { \"id\": \"102\" }, { \"id\": \"103\" }, { \"id\": \"104\" }, { \"id\": \"105\" }, { \"id\": \"106\" }, { \"id\": \"107\" } ]"; // Deserialize the JSON string into a list of objects List<object> jsonInput = System.Text.Json.JsonSerializer.Deserialize<List<object>>(jsonInputString); // Create a new list to store batches List<object> batch = new List<object>(); // Use the Chunk method to split the input array into batches of size 2 foreach (var chunk in jsonInput.Chunk(2)) { batch.Add(chunk); } // Serialize the batches into a JSON string for output string jsonOutputString = System.Text.Json.JsonSerializer.Serialize(batch); // Output the final result Console.WriteLine(jsonOutputString);
The Chunk
method in C# efficiently divides a large array into smaller arrays, each containing a maximum of the specified number of items (in this case, 2). This allows you to easily manage and process the data in smaller parts, which is especially useful for batch processing or API requests.
- How to use JsonConverterFactory in C#
- How to serialize non-public properties using System.Text.Json
- The JSON value could not be converted to System.DateTime
- Try/finally with no catch block in C#
- Parsing a DateTime from a string in C#
- Async/Await with a Func delegate in C#
- How to batch read with Threading.ChannelReader in C#
- How to ignore JSON deserialization errors in C#
Categories
Popular Posts
Freedash bootstrap lite
11/13/2024
Admin BSB Free Bootstrap Admin Dashboard
11/14/2024
K-WD Tailwind CSS Admin Dashboard Template
11/17/2024
Spica Admin Dashboard Template
11/18/2024