Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
samer1977 opened this issue May 29, 2024 · 0 comments
Open

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

samer1977 opened this issue May 29, 2024 · 0 comments

Comments

@samer1977
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant