Parsing responses neo4j-driver


I'm using the V4.4.2 version of the driver and trying to execute the query below but i'm having a hard time parsing the responses.


MATCH (n:Invoice), (n)-[:HAS_INVOICE_LINE]->(line), (n)-[:HAS_BILLING_ADDRESS]->(bill_addr), (n)-[:HAS_COMPANY_ADDRESS]->(comp_addr) WHERE n.deletion_status = false RETURN n, line, bill_addr, comp_addr

The query returns the results as expected in the browser (In this case it's one but could be multiple):

Without downstream nodes i usually use this snippet to get it:

async def list_all() -> list:
    driver = Database.get_driver()
    session = driver.session()
    result ="MATCH (n:Invoice), "
                               "(n)-[:HAS_INVOICE_LINE]->(line), "
                               "(n)-[:HAS_BILLING_ADDRESS]->(bill_addr), "
                               "(n)-[:HAS_COMPANY_ADDRESS]->(comp_addr) "
                               "WHERE n.deletion_status = false "
                               "RETURN n, line, bill_addr, comp_addr")
    values =
    ret = []
    for val in values:
    return ret

But with this query it returns it 3 times. This is due to the Invoiceline node being there 3 times i discovered using the debugger.

Is there a way to get this as a dict/json? or easily convert it to something like:

    "companyAddress": {},
    "billingAddress": {},
    "invoiceLines": [

Try this

MATCH (n:Invoice)
WHERE n.deletion_status = false
WITH n, collect(line{.*}) as lines
MATCH (n)-[:HAS_BILLING_ADDRESS]->(bill_addr)
MATCH (n)-[:HAS_COMPANY_ADDRESS]->(comp_addr) 
RETURN n{.*, lines: lines, bill_addr: bill_addr{.*}, comp_addr: comp_addr{.*}}

This uses cypher map projection. It is assumed that bill_addr and comp_addr exist and there is only one instance each. You can use optional match instead they may not exist.

1 Like

Thanks @glilienfield. I haven't thought about doing the heavy lifting in cypher instead of Python. It works perfectly.