Python Forum

Full Version: Object type of Node to json
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have Object type of Node like:

<Node id=1 labels={'First'} properties={'foo': 'foo', 'bar': 'bar'}>

I have tried to use like:

Response(dumps(res), mimetype="application/json")

But it throws an error:

TypeError: Object of type Node is not JSON serializable
please post some code that can be tried out.
That seems fairly self-explanatory to me, you have some class which the json module doesn't know how to serialize. So you need to come up with some way to bridge that gap, either by converting your object into a dictionary or by figuring out how the json module serializes stuff and implement whatever interface there is for it.
Thanks everyone!

I have found a way to get dict list like:
for o in res:
  print(o.keys())
  # dict_keys(['foo', 'bar'])
  print(o.values())
  # dict_values(['foo','bar'])
Now, please help me building a list and to convert it to json.

Thanks.

I'm not able to edit the reply - not sure how to do it.

I wanted to state that, I can grab as well:
print o.items()
# dict_items([('foo', 'bar'), ('foo', 'bar')])

My final expectation is to get like:

{
"id": 1,
"foo": "foo",
"bar": "bar"
}

o.id will get the id
Solved:
I have to use dict(o.items)