Skip to content

Cant do nested for loop unless used within new array context.  #358

@samer1977

Description

@samer1977

Hi,
Sometimes I find myself traversing different level arrays where Im interested at certain array level data. let's say we have the following json:


{
	"root": [
		{
			"batch_0": [
				{
					"end": "20240101",
					"start": "20240101",
					"id": "ABC10XX0092001"
				},
				{
					"end": "20230101",
					"start": "20230101",
					"id": "ABC10XX0092000"
				}
			],
			"batch_1": [
				{
					"end": "20220101",
					"start": "20220101",
					"id": "ABC10XX00920"
				}
			]
		}
	]
}

If I want to bucket all id values into an array in new json as follows:

{
  "ids" : [ "ABC10XX0092001", "ABC10XX0092000", "ABC10XX00920" ]
}

Obviously I have to do nested for loop here (unless Im missing something) but the only way I can do nested loop is by declaring an new array for each for and then do flattening of all nested arrays to become one:

{

  "ids":flatten([for(.root) [for(.) [for(.value) .id]]])
}

Why I cant do direct nested of for loop as follows:

{

  "ids":[for(.root) 
                  for(.)
                        for(.value) .id ]
}

I tried to use Pipe operator as well , and it did not work. It always complain about two consecutive for statement. Can this be an enchantment to save overhead , get better performance and make the code more readable or this can cause other issues ?

Thanks

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions