Forum Discussion

sdelano's avatar
sdelano
New Contributor II
9 years ago

Find Array index from object value in the array object getindex?

I have a json file with an array designed like below. I don’t have control over the number of or order of address entries. I have tried $.Address.findIndex(x => x == ‘Home’) and $.Address ? (value.AddrType = Home) among other expression language statements.

I would like to query the array by AddrType and get the index of the matching array entry record. Has anyone done something similar. We are attempting to normalize<<>>denormalize records based on types to denormalized fields that correspond to the types.

{fnam:“Joe”,Lname:“Long”,
Address:[
{
“AddrType”: “Mailing”,
“StreetAddr1”: “123 Prospect Street”,
“City”: “Waltham”,
“State”: “MA”,
“Zipcode”: “”
},
{
“AddrType”: “Main”,
“StreetAddr1”: “123 Baystate Street”,
“City”: “”,
“State”: “”,
“Zipcode”: “”,
}
]

2 Replies

  • tstack's avatar
    tstack
    Former Employee

    Is this the exact expression that you tried?

    If so, it won’t work because you missed the ‘AddrType’ field reference. Something like this should work:

    $.Address.findIndex(x => x.AddrType == "Home")
    
    • sdelano's avatar
      sdelano
      New Contributor II

      Works perfectly thank you.