1

I have an Array with multiple json objects that I want to break them into multiple Arrays based on the number of json objects. Each Array should not contain more than 10 objects as an example.

[{object-1}, {object-2},....,{object-n}]
Array-1 = [{object-1}, ....,{object-10}]
Array-2 = [{object-11}, ....,{object-21}]
Array-3 = [{object-22}, {object-22}]

Looking into the actual Data note the EventsSelectors Array below. What I am looking for is to extract all the objects in the EventSelectors and breaks them into multiple arrays of EventsSelectors for later processing. Max number of Records should not exceed 20 records.

{
    "EventSelectors": [
        {
            "IncludeManagementEvents": true,
            "DataResources": [],
            "ReadWriteType": "All"
        },
        {
            "IncludeManagementEvents": true,
            "DataResources": [],
            "ReadWriteType": "All"
        },
        {
            "IncludeManagementEvents": true,
            "DataResources": [],
            "ReadWriteType": "All"
        }

    ],
    "TrailARN": "arn:aws:cloudtrail:us-east-2:123456789012:trail/TrailName"
}

Thanks

Moe
  • 1,427
  • 4
  • 34
  • 54
  • share the actual data (a subset only), explain how the output should look like. share your current effort as well (python code) – balderman Sep 03 '21 at 20:46

1 Answers1

0

See below

def slicer(lst, n):
    for i in range(0, len(lst), n):
        yield lst[i:i + n]


N = 3
data = [{6}, {7}, {8}, {8}, {1}, {2}, {5}, {77}, {88}, {654}]
sliced = {idx: data for idx, data in enumerate(slicer(data, N))}
print(sliced)

output

{0: [{6}, {7}, {8}], 1: [{8}, {1}, {2}], 2: [{5}, {77}, {88}], 3: [{654}]}
balderman
  • 22,927
  • 7
  • 34
  • 52
  • can you add some explanation to your solution – Moe Sep 04 '21 at 00:21
  • The 'slicer' is a generator that return (yield) a slice each time is is called. The external loop creates a dict where the key is the index and the value is the slice. – balderman Sep 04 '21 at 06:28
  • how can I iterate over the sliced big array. for i in sliced print i something like that. – Moe Sep 04 '21 at 07:37
  • The result is a dict. See how can you iterate over a dict ( like a java Map ) – balderman Sep 04 '21 at 08:20