cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

How to check a key exists in a Record returned by cypher?

lingvisa
Graph Fellow

I am using Python Driver Bolt api.

 cypher = MATCH + WHERE + RETURN + LIMIT
        
        records = tx.run(cypher)

        result_set = []
        for record in records:
            tuple = [record['nname'], record['nid']]
            if 'mname' in record and 'mid' in record:
                tuple.append(record['mname'])
                tuple.append(record['mid'])
            result_set.append(tuple)

My records optionally contain fields 'mname' and 'mid' in the result set. However, the test fails when these two fields actually contain values:
if 'mname' in record and 'mid' in record:`

It's not a standard dict in Python. How to check the presence in such a case?

1 ACCEPTED SOLUTION

Hello @lingvisa

This function will transform the BOLT result into list of dictionnaries:

def bolt_to_list(result):
    return [r.data() for r in result]

You can use like this:

records = bolt_to_list(tx.run(cypher))

You can use classic Python to check for keys

Regards,
Cobra

View solution in original post

2 REPLIES 2

Hello @lingvisa

This function will transform the BOLT result into list of dictionnaries:

def bolt_to_list(result):
    return [r.data() for r in result]

You can use like this:

records = bolt_to_list(tx.run(cypher))

You can use classic Python to check for keys

Regards,
Cobra

lingvisa
Graph Fellow

Good to know. Thanks.