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

JMESPATH (comparing aggregated nested data) #127

Open
jskler opened this issue Sep 4, 2024 · 1 comment
Open

JMESPATH (comparing aggregated nested data) #127

jskler opened this issue Sep 4, 2024 · 1 comment

Comments

@jskler
Copy link

jskler commented Sep 4, 2024

Hello!

I was wondering if someone can help me find a solution to compare some nested data.

I have this dataset below, and I need to return true, for case when type is 'X', get shareholder[]firstName+shareholder[]lastName and compare with logistic_name, if they are the same, to give back true. I have been trying to use join, but the return is NULL.

My code is

[(shareholders != null) &&
( length(shareholders[?type == 'shareholderperson' && join(' ', [firstName, lastName]) == transaction.receiverName]) > 0)

]

{
"id": 6731037,
"customerId": 665,
"shareholders": [ -- nested JSON --
{
"id": "1",
"type": "X,
"lastName": "one",
"firstName": "james",
},
{
"id": "2",
"type": "Z,
"lastName": "two",
"firstName": "james",
},
{
"id": "3",
"type": "Y,
"lastName": "three",
"firstName": "james",
}
],
"logistic_name": "james one", -- not nested--
}
}

Expected result will be TRUE, because there is shareholder type X where the firstName+lastName == logistic_name. ID 1

@EnCey
Copy link

EnCey commented Nov 7, 2024

Hi @jskler,

Given this json:

{
  "id": 6731037,
  "customerId": 665,
  "shareholders": [
    {
      "id": "1",
      "type": "X",
      "lastName": "one",
      "firstName": "james"
    },
    {
      "id": "2",
      "type": "Z",
      "lastName": "two",
      "firstName": "james"
    },
    {
      "id": "3",
      "type": "Y",
      "lastName": "three",
      "firstName": "james"
    }
  ],
"logistic_name": "james one"
}

A JmesPath transformation as you describe would look like this:

shareholders[?type=='X'].[join(' ', [firstName, lastName])][0][0]==$.logistic_name

Note that this requires use of the $ expression (or let alternatively), which is a community feature.
You can try it out here

As for whether you can use $ or let, that depends on your library / programming language.
I'm currently trying to figure out myself what the state is with these features.

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