Batch JSON Arrays with LINQ Chunk Method
By FoxLearn 1/9/2025 3:18:48 AM 32
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 fix 'Failure sending mail' in C#
- How to Parse a Comma-Separated String from App.config in C#
- How to convert a dictionary to a list in C#
- How to retrieve the Executable Path in C#
- How to validate an IP address in C#
- How to retrieve the Downloads Directory Path in C#
- C# Tutorial
- Dictionary with multiple values per key in C#
Categories
Popular Posts
Freedash bootstrap lite
11/13/2024
Plus Admin Dashboard Template
11/18/2024
Dash UI HTML5 Admin Dashboard Template
11/18/2024
Material Dashboard Admin Template
11/17/2024