Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python 3: Is not JSON serializable

TypeError: b'Pizza is a flatbread generally topped with tomato sauce and cheese and baked in an oven. It is commonly topped with a selection of meats, vegetables and condiments. The term was first recorded in the 10th century, in a Latin manuscript from Gaeta in Central Italy. The modern pizza was invented in Naples, Italy, and the dish and its variants have since become popular in many areas of the world.\nIn 2009, upon Italy\'s request, Neapolitan pizza was safeguarded in the European Union as a Traditional Speciality Guaranteed dish. The Associazione Verace Pizza Napoletana (the True Neapolitan Pizza Association) is a non-profit organization founded in 1984 with headquarters in Naples. It promotes and protects the "true Neapolitan pizza".\nPizza is sold fresh, frozen or in portions, and is a common fast food item in North America and the United Kingdom. Various types of ovens are used to cook them and many varieties exist. Several similar dishes are prepared from ingredients commonly used in pizza preparation, such as calzone and stromboli.' is not JSON serializable 

I have a program that adds this into a JSON string, which works fine for most text strings - but not this one apparently. Can you tell why not, or how to fix it?

like image 923
njha Avatar asked Mar 25 '16 01:03

njha


People also ask

Is not JSON serializable Python error?

The Python "TypeError: Object of type dict_items is not JSON serializable" occurs when we try to convert a dict_items object to JSON. To solve the error, convert the dict to JSON or pass the dict_items object to the list() class before the conversion to JSON.

How do I make my Python object JSON serializable?

Use toJSON() Method to make class JSON serializable So we don't need to write custom JSONEncoder. This new toJSON() serializer method will return the JSON representation of the Object. i.e., It will convert custom Python Object to JSON string.

Is Python set JSON serializable?

If you try to convert the Set to json, you will get this error: TypeError: Object of type set is not JSON serializable. This is because the inbuilt Python json module can only handle primitives data types with a direct JSON equivalent and not complex data types like Set.

What makes an object not JSON serializable?

The Python "TypeError: Object of type set is not JSON serializable" occurs when we try to convert a set object to a JSON string. To solve the error, convert the set to a list before serializing it to JSON, e.g. json. dumps(list(my_set)) . Here is an example of how the error occurs.


1 Answers

This is not a string, but a byte sequence. JSON knows only how to handle Unicode strings, not byte sequences. Either transform into Unicode (json.dumps(x.decode("utf-8"))), or into an integer array (json.dumps(list(x))).

like image 182
Amadan Avatar answered Sep 29 '22 09:09

Amadan