Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Object type of Node to json
#1
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
Reply
#2
please post some code that can be tried out.
Reply
#3
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.
Reply
#4
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
Reply
#5
Solved:
I have to use dict(o.items)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  declaring object parameters with type JonWayn 2 856 Dec-13-2022, 07:46 PM
Last Post: JonWayn
  Python Split json into separate json based on node value CzarR 1 5,471 Jul-08-2022, 07:55 PM
Last Post: Larz60+
  Deserialize Complex Json to object using Marshmallow tlopezdh 2 2,085 Dec-09-2021, 06:44 PM
Last Post: tlopezdh
  AttributeError: 'Node' object has no attribute 'insert' Anldra12 4 6,379 May-11-2021, 10:12 AM
Last Post: Anldra12
Star Type Error: 'in' object is not callable nman52 3 3,328 May-01-2021, 11:03 PM
Last Post: nman52
  finding and deleting json object GrahamL 1 4,801 Dec-10-2020, 04:11 PM
Last Post: bowlofred
  TypeError: 'type' object is not subscriptable Stef 1 4,440 Aug-28-2020, 03:01 PM
Last Post: Gribouillis
  isinstance() always return true for object type check Yoki91 2 2,498 Jul-22-2020, 06:52 PM
Last Post: Yoki91
  set a new object node in a linked list via reference oloap 2 2,055 Mar-13-2020, 09:45 PM
Last Post: oloap
  AttributeError: type object 'FunctionNode' has no attribute '_TestValidateFuncLabel__ binhduonggttn 0 2,219 Feb-19-2020, 11:29 AM
Last Post: binhduonggttn

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020