Batch JSON Arrays with LINQ Chunk Method
By FoxLearn 1/9/2025 3:18:48 AM 82
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.
- Using the OrderBy and OrderByDescending in LINQ
- Querying with LINQ
- Optimizing Performance with Compiled Queries in LINQ
- MinBy() and MaxBy() Extension Methods in .NET
- SortBy, FilterBy, and CombineBy in NET 9
- Exploring Hybrid Caching in .NET 9.0
- Using Entity Framework with IDbContext in .NET 9.0
- Primitive types in C#