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

How JMESPath group by and summary data #70

Closed
shps951023 opened this issue Oct 8, 2020 · 2 comments
Closed

How JMESPath group by and summary data #70

shps951023 opened this issue Oct 8, 2020 · 2 comments

Comments

@shps951023
Copy link

How JMESPath group by and summary data

input data :

[
	{
		"value" : 15,
		"name" : "app1"
	},
	{
		"value" : 25,
		"name" : "app1"
	},
	{
		"value" : 30,
		"name" : "app2"
	},
	{
		"value" : 45,
		"name" : "app2"
	}	
]

expected result :

[
	{
		"name" : "app1",
		"summary_value" : 40
	},
	{
		"name" : "app2",
		"summary_value" : 75
	},	
]

what I've tried & thought :
It tried to below code but it not work and return Script error. message

const testData =
[
	{
		"value" : 15,
		"name" : "app1"
	},
	{
		"value" : 25,
		"name" : "app1"
	},
	{
		"value" : 30,
		"name" : "app2"
	},
	{
		"value" : 45,
		"name" : "app2"
	}	
];


console.log( jmespath.search(testData, '[].{name:name,summary_value:sum([?name = name].value)}') );

Demo - JSFiddle - Code Playground

PS : my env is ubuntu and support bash

@darrenmothersele
Copy link

Hey, the issue here is that once you are in the projection you can't get back to the parent level of the data. So the ?name == name conditional clause isn't going to do what you think it should do.

I don't know how you can acheive this with standard JMESPath, but with my fork I've added a decorate function so that you can add your own functions to JMESPath's internal function table. Here is a working example using your testData above that uses the groupBy function from lodash:

https://codesandbox.io/s/zealous-dew-lr9g3?file=/src/index.js

@shps951023
Copy link
Author

shps951023 commented Oct 9, 2020

@darrenmothersele thanks!
It looks like JMESPath need to custom function to do it.

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

2 participants