1. Overview In this tutorial, You will be learning exactly how to solve the difficulty in python " TypeError: thing of kind 'int64' i...
You are watching: Object of type int64 is not json serializable
1. OverviewIn this tutorial, You will certainly be learning how to settle the trouble in python "TypeError: object of kind "int64" is no JSON serializable" and also "TypeError: (Integer) is not JSON serializable". These 2 errors come right into existence as soon as we working with JSON Serialization. Solutions shown in this write-up will job-related for apart from the int or int64 type such as float, array and also etc.Below is the scenario wherein my friend confronted the problem.
2. Problem 1: declare (Scenario): TypeError: object of type "int64" is no JSON serializableI have actually a Dataframe the stores keep name and also daily sales count. Ns am trying to insert this come Salesforce using the listed below Python script. I, however, acquire an error TypeError: object of kind "int64" is not JSON serializableGiven below is the watch of the Dataframe
Storename,CountStore A,10Store B, 12Store C, 5I use the listed below code come insert it come Salesforceupdate_list = <>for ns in range((len(store))): update_data = "name" : store<"entity_name">.iloc, "count__c": store<"count">.iloc update_list.append(update_data)sf_data_cursor = sf_datapull.salesforce_login()sf_data_cursor.bulk.Account.update(update_list)Get the error the critical line over gets executed.Error:TypeError: object of type "int64" is not JSON serializableYou can try this difficulty solving without seeing the answer first.
3. Solution 1 for TypeError: thing of kind "int64" is not JSON serializableJSON walk not acknowledge NumPy data types. Convert the number come a Python int before serializing the object. Just add int typecasting come the store(..) value.
4. Systems 2 to TypeError: thing of kind "int64" is no JSON serializableimport numpy package and create your own decoder for addressing the problem of any kind of data type.import jsonimport numpy as npclass NpEncoder(json.JSONEncoder): def default(self, obj): if isinstance(obj, np.integer): return int(obj) elif isinstance(obj, np.floating): return float(obj) elif isinstance(obj, np.ndarray): return obj.tolist() else: return super(NpEncoder, self).default(obj)# her codes .... Json.dumps(data, cls=NpEncoder)
5. Solution 3 through typecasting to String all values in CSV fileAnother choice is that once you produce the data frame that treats all values as string from CSV file. For that, we should use "dtype=str" which specifies the type for each field as String. So that String is serialized by default.For example, if you loaded the keep from a CSV file:import pandas as pdstore = pd.read_csv("store.csv", dtype=str)Then whatever has a kind of str that have the right to be serialized to JSON.
6. Trouble 2: maintain int64 values when parsing JSON in Go
7. Solution 1: To maintain the int64 kind in unmarshallingYou deserve to use a Decoder and UseNumber come decode your numbers without loss.The Number form is defined like this :
8. Systems 2: To keep the int64 kind in unmarshallingYou can likewise decode into a details structure tailored as per your needs.
See more: Watch Wisdom Of The Crowd Season 1 Episode 4 2F, Wisdom Of The Crowd User Bias (Tv Episode 2017)
9. ConclusionIn this article, we have seen the remedies for the troubles that we challenge in data types int, int64, float and array serialization. We have displayed the troubles here because that int type. However the exact same solutions will work for float and remaining varieties as well.Just offer a try. Store posting your questions in the comment ar for answers.