0
from flask import Flask, jsonify, render_template
from py2neo import Graph,authenticate

app = Flask(__name__)
authenticate("localhost:7474","neo4j", "neo4j")
graph = Graph("http://localhost:7474/db/data")

def buildNodes(nodeRecord):
    print("............................")
    data = {"id": str(nodeRecord.n._id), "label": next(iter(nodeRecord.n.labels))}
    data.update(nodeRecord.n.properties)
    print(data)
    return {"data": data}

def buildEdges(relationRecord):
    data = {"source": str(relationRecord.r.start_node._id),
            "target": str(relationRecord.r.end_node._id),
            "relationship": relationRecord.r.rel.type}

    return {"data": data}

@app.route('/')
def index():
    print("index")
    return render_template('index.html')

@app.route('/graph')
def get_graph():
    # print(graph.cypher.execute('MATCH (n) RETURN n').columns)
    nodes = map(buildNodes, graph.cypher.execute('MATCH (n) RETURN n'))
    print(nodes)
    edges = map(buildEdges, graph.cypher.execute('MATCH ()-[r]->() RETURN r'))
    print(edges)
    # json_2={"nodes":nodes,"edges":edges}
    # return json.dumps(json_2, cls=UserEncoder)
    elements = {"nodes": nodes, "edges": edges}
    print(dict(elements))
    return jsonify(elements)
    return jsonify(elements)

if __name__ == '__main__':
    app.run(debug = False)

When I use Python to connect the graph database(neo4j), I have the problem 'map object at 0x03D25C50' is not JSON serializable, but map object at 0x03D25C50 is the result of method of map(). I don't know how to resolve the problem.

Is there anything obvious I'm dong wrong here?

martineau
  • 119,623
  • 25
  • 170
  • 301
  • I'm not sure if the `map` object has this implemented, but many objects implement the `__str__` method so that you can get a string representation of the data. Sometimes it's just the class name, but for instance the `dict` object will do this: `>>> str({'b':2, 'c':3}) "{'c': 3, 'b': 2}"` – Allie Fitter Mar 14 '17 at 16:44
  • thanks a lot,I have changed the code,but the problem shows below,I‘am looking forward your answer. Thanks a ton! – kenneth Mar 15 '17 at 08:55

2 Answers2

0

The quick answer to what the error in your image is saying is that you aren't returning a response from your except block. And thus because the try is failing, your view isn't returning anything. But that doesn't answer the real question. I haven't used map in python. for loops and list comprehension typically get the job done pretty well. I'm also not familiar at all with neo4j. That being said, I believe the answer is that you're using the __next__ method. Is there any reason why you're doing that explicitly? According to the docs this used when iterating over an iterable to get the next set of data. In other words it's implicitly called when you loop over an iterable--with a for loop for instance. What may be happening is that if there is only one set of data, when you call __next__ the first time, you get that first and only data set. But when you call it the second time it doesn't have data to return, so you get a map object. This may not be the 100% correct answer, but try removing the __next__ calls, and see if that helps. Try to encode simply str(edges) and str(nodes) instead of calling __next__.

Allie Fitter
  • 1,689
  • 14
  • 18
0

In Python 2, the map built-in returned a list. In Python 3, it returns an iterator instead of a list, which the json module can't serialize. If you need a list, and not an iterator, you should use a list comprehension: [buildNodes(row) for row in graph.cypher.execute(...)]. You can also force-type it by doing list(map(...)), but that's not as clear as a listcomp.

You should be aware that you can't call __next__ (or, preferably, next(...)) on a list, however, as that is an iterator method. You can print a whole list without consuming it, though, so unless you're explicitly trying to lazy load, a list is a better option here anyway.

You can read about some of the difference between lists/other sequence types and iterators in the standard docs here and about the next built-in function and why it is preferable to __next__ here.

Tore Eschliman
  • 2,477
  • 10
  • 16