Linked Questions

0 votes
0 answers
31 views

print an object without __dict__ as JSON in python [duplicate]

I'm getting data back from a library. How do I get both the keys and the data in JSON format? It appears to not have a dict property, so I don't know how to inspect it. vars(obj.rect), fails. I ...
Tac Tacelosky's user avatar
0 votes
0 answers
28 views

How can I make this complex object JSON serializable? [duplicate]

I've got: class Catalog(): Categories = [] class Category(): Subcategories = [] Courses = [] def __init__(self, title): self.Title = title It goes on, where subcategories and ...
Russ's user avatar
  • 51
0 votes
0 answers
26 views

How to convert list of object to json in Python? [duplicate]

I have a class called Employee and there is list which contains instance of Employee. I tried the below approach: class Employee: empId empName def __init__(self, empId, empName): ...
Saurabh's user avatar
  • 867
0 votes
0 answers
22 views

Convert any python object to JSON string and then using the JSON string construct the original python object [duplicate]

How do we construct a JSON string which contains all the necessary details which are part of any python objects (primitive data types, custom classes, enums, list of classes, dictionary of different ...
Jubin Mathew's user avatar
0 votes
0 answers
21 views

Serialize an object to JSON [duplicate]

How can I serialize an object that contains a list of objects to JSON The error I get : TypeError: Object of type person is not JSON serializable import json class person: def __init__(self, ...
Olivier Coujan's user avatar
0 votes
0 answers
20 views

Does json.dumps call a class dunder method? [duplicate]

When attempting to serialize a class to JSON in python you could try to call json.dumps(my_class_instance). However, this will raise TypeError: Object of type MyClass is not JSON serializable. There ...
jlhasson's user avatar
  • 1,916
0 votes
0 answers
18 views

is not JSON serializable [Ros Python] [duplicate]

I am trying to make a dictionary of Point, in which the key is an id (unique integer number) and value is the point itself. Please see the sample code below: from geometry_msgs.msg import Point ...
ravi's user avatar
  • 6,260
0 votes
0 answers
16 views

Python - loading a JSON file with non supported objects and types [duplicate]

I've got a log file as JSON that includes unsupported types, even though the intention was apparently to have something human editable and I wish this log file was never produced, what would be the ...
user1767754's user avatar
  • 24.3k
1943 votes
15 answers
2.2m views

How to prettyprint a JSON file?

How do I pretty-print a JSON file in Python?
Colleen's user avatar
  • 24.7k
507 votes
32 answers
813k views

How to convert JSON data into a Python object?

I want to convert JSON data into a Python object. I receive JSON data objects from the Facebook API, which I want to store in my database. My current View in Django (Python) (request.POST contains the ...
Sai Krishna's user avatar
  • 7,957
454 votes
16 answers
531k views

NumPy array is not JSON serializable

After creating a NumPy array, and saving it as a Django context variable, I receive the following error when loading the webpage: array([ 0, 239, 479, 717, 952, 1192, 1432, 1667], dtype=int64) ...
Karnivaurus's user avatar
  • 23.5k
193 votes
13 answers
186k views

Make the Python json encoder support Python's new dataclasses

Starting with Python 3.7, there is something called a dataclass: from dataclasses import dataclass @dataclass class Foo: x: str However, the following fails: >>> import json >>&...
miracle2k's user avatar
  • 30.8k
309 votes
2 answers
242k views

How do I change the string representation of a Python class? [duplicate]

In Java, I can override the toString() method of my class. Then Java's print function prints the string representation of the object defined by its toString(). Is there a Python equivalent to Java's ...
snakile's user avatar
  • 53.8k
60 votes
7 answers
78k views

Encoding nested python object in JSON

I want to encode objects in JSON. But, I can not figure out how to make the output without the string escaping. import json class Abc: def __init__(self): self.name="abc name" def ...
Fred Laurent's user avatar
  • 1,557
66 votes
6 answers
76k views

Making object JSON serializable with regular encoder

The regular way of JSON-serializing custom non-serializable objects is to subclass json.JSONEncoder and then pass a custom encoder to json.dumps(). It usually looks like this: class CustomEncoder(json....
leonsas's user avatar
  • 4,818

15 30 50 per page
1 2
3
4 5
11