json
  • neo4j
  • py2neo
  • 2016-07-25 1 views 0 likes 
    0

    J'ai essayé de chercher partout mais en vain. Voici ma requête Cypher dans py2neo:outputiing py2neo requête comme JSON

    graph = Graph() 
        In [6]: query = """Match (C:Customer)-[r:Customer_Send]->(Send:Customer) where C.Galactic_ID = '2000000000084001287' return Send.Galactic_ID """ 
    
    In [7]: graph.cypher.execute(query) 
    
    
    Out[7]: 
         | Send.Galactic_ID 
    ----+--------------------- 
         1 | 2000000000084114531 
         2 | 1000000000284949451 
         3 | 2000000000084114531 
         4 | 1000000000213446086 
    

    Je veux la sortie ci-dessus pour être JSON formaté.

    merci d'avance.

    Répondre

    0

    Voici la façon de le faire: Je suis sûr que ce sera d'un grand secours aux lecteurs ici:

    query = """Match (C:Customer)-[r:Customer_Send]->(Send:Customer) where C.Galactic_ID = '2000000000084001287' return Send.Galactic_ID as ID""" 
    
    records = graph.cypher.execute(query,Customer='ID') 
    returnObject = [] 
    
    In [11]: for record in records: 
    ....:  returnObject.append(
    . ...:  { 
        ....:  'Customer':record.ID 
        ....:  } 
         ....: ) 
    
    
    In [15]: returnObject 
    Out[15]: 
        [{'Customer': u'2000000000084114531'}, 
        {'Customer': u'1000000000284949451'}, 
        {'Customer': u'2000000000084114531'}, 
        {'Customer': u'1000000000213446086'}, 
        {'Customer': u'2000000000084114531'}, 
        {'Customer': u'2000000000127655859'}, 
        {'Customer': u'1000000000296804864'}, 
        {'Customer': u'2000000000084114531'}, 
        {'Customer': u'2000000000127655859'}, 
        {'Customer': u'2000000000127655859'}, 
        {'Customer': u'2000000000084114531'}, 
        {'Customer': u'1000000000213446086'}] 
    
        from flask import json 
    
    
        In [17]: x = json.dumps(returnObject) 
    
        In [18]: x 
        Out[18]: '[{"Customer": "2000000000084114531"}, {"Customer": "1000000000284949451"}, {"Customer": "2000000000084114531"}, {"Customer": "1000000000213446086"}, {"Customer": "2000000000084114531"}, {"Customer": "2000000000127655859"}, {"Customer": "1000000000296804864"}, {"Customer": "2000000000084114531"}, {"Customer": "2000000000127655859"}, {"Customer": "2000000000127655859"}, {"Customer": "2000000000084114531"}, {"Customer": "1000000000213446086"}]' 
    
    1

    C'est relativement simple avec juste la bibliothèque standard et la dernière version de py2neo:

    >>> from py2neo import Graph 
    >>> from json import dumps 
    >>> g = Graph(password="password") 
    >>> dumps(g.run("UNWIND range(1, 3) AS n RETURN n").data()) 
    '[{"n": 1}, {"n": 2}, {"n": 3}]' 
    

    http://py2neo.org/v3/database.html#py2neo.database.Cursor.data

    +0

    Salut Nigel. Cela aide vraiment. J'utilisais encore l'ancienne version jusqu'à maintenant. Merci pour le conseil. –

     Questions connexes

    • Aucun problème connexe^_^