05-29-2024 09:57 PM
Hi All,
I've tried to find an answer to this one but haven't had success. I have the below JSON structure (with roughly 5,000 records) returned from an API.
{
"employees": [
{
"/work/employeeIdInCompany": {
"value": 654321
},
"work": {
"employeeIdInCompany": 654321
},
"id": "32184129999999999999"
},
{
"/work/employeeIdInCompany": {
"value": 123456
},
"work": {
"employeeIdInCompany": 123456
},
"id": "3218412688888888888"
},
...
]
}
What I need to do is pull the longer ID field where the employeeIdInCompany value matches the current search criteria. For example, where the employeeIdInCompany is 123456, I need to pull 3218412688888888888 from the above JSON.
I know how to use where and pluck where the search item is at or above the level of the field to be pulled, but at a bit of a loss where the search item is nested like this.
Thank You
Gareth
Solved! Go to Solution.
05-29-2024 11:20 PM
It's a bit of an eyesore, but this is about as elegant as Ruby can do it. Ugly or not, it works and it's a single action.
It assumes a few things:
If those three assumptions are okay, then this will probably work for you.
I put your sample payload in a logger message and accessed it from there. Assume "message" in the below is a data pill containing your sample data. If you already have a data pill for "employees", you can just use that instead of "message["employees"]"
message["employees"][message["employees"].pluck("work").pluck("employeeIdInCompany").find_index(123456)]["id"]
It should look like this:
Hope this helps!
05-29-2024 11:20 PM
It's a bit of an eyesore, but this is about as elegant as Ruby can do it. Ugly or not, it works and it's a single action.
It assumes a few things:
If those three assumptions are okay, then this will probably work for you.
I put your sample payload in a logger message and accessed it from there. Assume "message" in the below is a data pill containing your sample data. If you already have a data pill for "employees", you can just use that instead of "message["employees"]"
message["employees"][message["employees"].pluck("work").pluck("employeeIdInCompany").find_index(123456)]["id"]
It should look like this:
Hope this helps!
05-30-2024 09:52 PM
Awesome, that worked a treat, thank you!