Return the index number within a LIST that matches a specific STRING

I'm making a webapi call using apoc.load.json and returning data back.

The first collection (value[0]) is the "propery" names in a list. I'd like to write my code to be self-healing if a later version of the webapi changes the order that the properties are returned.

for example: The property I want is 'svc_state_age' which happens to be the 13th item returned.

CALL apoc.load.json(url) YIELD value
UNWIND value[1].result as svcstatus
WITH * where svcstatus[12] <> cmks.servicestateage

But if the vendor changes things and now svs_state_age is the 10th element, I'd have to fix my code. I'd like to be able to use a variable for [12] when the 'name' of the property in value[0] matches 'svc_state_age'

I'm not sure how to return the index number of a collection ie:
WITH collect(value[0].result) as apipropertylist
then return the "index" number of the list where the value matches a string.

Any ideas on how to accomplish this?


If you have APOC Procedures installed, you can use the apoc.coll.indexOf() function to find the index of a value in a list.

Thanks Andrew! That's what I get for breaking my own 1st rule of cypher, which is "Check if APOC solves your problem for you first".

I am taking the JSON results from apoc.load.json in which the first JSON group returned is the header names, and I use apoc.coll.indexOf using value.result[0] as the collection. The index for 2 properties are then stored as indexn1 and indexn2 variables, which I then use to get the values for those properties (from the 2nd JSON collection) in the WHERE clause and SET commands. value.result[1][indexn1]

If anyone else is trying to do this, I'll leave my code sample for context below:

MATCH (cs:Cmksite)--(cmkh:Cmkhost)--(cmks:Cmkservice)--(t:Ticket)-[tsr:TICKET_STATUS]-(ts:Ticketstatus) where not((cmks)--(:Servicestate)) and (t)--(:Crmcharge) and ts.statcode<900
WITH *,apoc.text.urlencode( as svcname0
WITH *,replace(svcname0,'+','%20') as svcname
WITH *,"""&service="+svcname+"&site=""&view_name=service&output_format=JSON&_username=myusername&_secret=mysupersecret" as url
CALL apoc.load.json(url) YIELD value
WITH *,apoc.coll.indexOf(value.result[0],'svc_state_age') as indexn1,apoc.coll.indexOf(value.result[0],'svc_plugin_output') as indexn2
WITH * WHERE value.result[1][indexn1] <> cmks.servicestateage
SET cmks.servicestateage=value.result[1][indexn1],cmks.output=value.result[1][indexn2]