Im trying to filter and get the first object of two objects that my array includes using Jsopath.
My JSON source is the following :
{
"parties": {
"applicants": [
{
"role": "USER",
"companyType": "LEGAL_ENTITY",
"contactDetails": {
"residenceAddress": {
"streetAddress": "testADDRESS",
"postalCode": "12323",
"city": "Athens",
"country": "GR"
},
"email": "testing@tester.com"
},
"companyDetails": {
"company": "COMPANY TESTING1",
"refNo": "123213"
},
"id": "d2ed5f80-e6c6-4463-86f6-4600654f899d",
"state": "CONFIRMED",
"sequenceNumber": 1
},
{
"role": "USER",
"companyType": "LEGAL_ENTITY",
"contactDetails": {
"residenceAddress": {
"streetAddress": "TESTADDRESS2",
"postalCode": "1231232",
"city": "Athens",
"country": "GR"
},
"email": "TESTINGtest"
},
"companyDetails": {
"company": "COMPANYTESTING2",
"refNo": "423423"
},
"id": "d2ed5f80-e6c6-4463-86f6-4600654f899e",
"state": "CONFIRMED",
"sequenceNumber": 2
}
]
}
}
Things ive tried :
$.parties.applicants[?(@.role == 'USER')].companyDetails.company
Works. Brings both two objects.
($.parties.applicants[?(@.role == 'USER')].companyDetails.company)[0]
trying to bring the first object. not working.
$.parties.applicants[?(@.role == 'USER')][0].companyDetails.company
not working.
Im not sure if the logic im trying to implement ([0]) works or is appropriate for what i want to accomplish. Ive been testing the JSON path on https://jsonpath.com/ evaluator.
Any help or ideas will be more than welcome.
Here's the solution :
$.parties.applicants[?(@.role == 'USER' && @.sequenceNumber == 1)].companyDetails.company
But if you don't have access to sequenceNumber or if sequenceNumber is not the counter, you will have to change your querying language and use JMESPATH or Jq.