{"id":122486,"date":"2018-04-24T12:33:17","date_gmt":"2018-04-24T12:33:17","guid":{"rendered":"https:\/\/webkul.com\/blog\/?p=122486"},"modified":"2018-04-24T12:33:17","modified_gmt":"2018-04-24T12:33:17","slug":"serialize-python-object-into-json-format","status":"publish","type":"post","link":"https:\/\/webkul.com\/blog\/serialize-python-object-into-json-format\/","title":{"rendered":"Serialize Python object into JSON format"},"content":{"rendered":"<p>Sometimes we have to parse\/serialize the python object data in JSON for sending the data to the server.<br \/>\nThere are may way to do this, one of such way is to serialize the object inside the <strong>__repr__<\/strong> method.<\/p>\n<p>&nbsp;<\/p>\n<p>In this post, I am going to explain to you that how to create a JSON type record in python.Below is the example of a python class which implements the JSON serialization.<\/p>\n<p>&nbsp;<\/p>\n<pre class=\"brush:py\">import json\r\nfrom collections import OrderedDict\r\n\r\ndef jsonDefault(OrderedDict):\r\n    return OrderedDict.__dict__\r\n    \r\nclass SampleClass(object):\r\n    def __init__(self,*args,**kwargs):pass\r\n    \r\n    def __repr__(self):\r\n        \r\n    return json.dumps(self, default=jsonDefault, indent=4)\r\n\r\n    def add_record_as_data(self,_record):\r\n        self.__dict__.update(_record.__dict__)\r\n    def add_record_as_attr(self,_record):\r\n        self.record = _record\r\n        \r\nobj = SampleClass()\r\nobj.name = 'name'\r\nprint (obj)\r\n{\r\n \"name\": \"name\"\r\n}\r\n\r\n\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>In the above example we have create a\u00a0\u00a0 <strong>jsonDefault<\/strong> data handler which\u00a0 used inside the<strong> __repr__ <\/strong>method of <strong>SampleClass<\/strong> class as : json.dumps(self, <strong>default=jsonDefault<\/strong>, indent=4)<\/p>\n<p>That\u2019s all for today. I hope this blog will help you. I\u2019d be very grateful if you\u2019d write your opinions, comments, and suggestions to keep the page updated and interesting.<\/p>\n<p>You may also like our post on <a href=\"https:\/\/webkul.com\/blog\/python-imaging-librarypil-examples\/\">Python Imaging Library(PIL) Examples<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Sometimes we have to parse\/serialize the python object data in JSON for sending the data to the server. There are may way to do this, one of such way is to serialize the object inside the __repr__ method. &nbsp; In this post, I am going to explain to you that how to create a JSON <a href=\"https:\/\/webkul.com\/blog\/serialize-python-object-into-json-format\/\">[&#8230;]<\/a><\/p>\n","protected":false},"author":86,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2383],"tags":[236,3324,6581],"class_list":["post-122486","post","type-post","status-publish","format-standard","hentry","category-blog","tag-json","tag-python","tag-serialization"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Serialize Python object into JSON format - Webkul Blog<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/webkul.com\/blog\/serialize-python-object-into-json-format\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Serialize Python object into JSON format - Webkul Blog\" \/>\n<meta property=\"og:description\" content=\"Sometimes we have to parse\/serialize the python object data in JSON for sending the data to the server. There are may way to do this, one of such way is to serialize the object inside the __repr__ method. &nbsp; In this post, I am going to explain to you that how to create a JSON [...]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/webkul.com\/blog\/serialize-python-object-into-json-format\/\" \/>\n<meta property=\"og:site_name\" content=\"Webkul Blog\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/webkul\/\" \/>\n<meta property=\"article:published_time\" content=\"2018-04-24T12:33:17+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-og.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"630\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Prakash Kumar\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@webkul\" \/>\n<meta name=\"twitter:site\" content=\"@webkul\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Prakash Kumar\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/webkul.com\/blog\/serialize-python-object-into-json-format\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/webkul.com\/blog\/serialize-python-object-into-json-format\/\"},\"author\":{\"name\":\"Prakash Kumar\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/8b41d806aa1c7ec81f958437fac62e5b\"},\"headline\":\"Serialize Python object into JSON format\",\"datePublished\":\"2018-04-24T12:33:17+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/webkul.com\/blog\/serialize-python-object-into-json-format\/\"},\"wordCount\":153,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/webkul.com\/blog\/#organization\"},\"keywords\":[\"json\",\"python\",\"serialization\"],\"articleSection\":[\"blog\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/webkul.com\/blog\/serialize-python-object-into-json-format\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/webkul.com\/blog\/serialize-python-object-into-json-format\/\",\"url\":\"https:\/\/webkul.com\/blog\/serialize-python-object-into-json-format\/\",\"name\":\"Serialize Python object into JSON format - Webkul Blog\",\"isPartOf\":{\"@id\":\"https:\/\/webkul.com\/blog\/#website\"},\"datePublished\":\"2018-04-24T12:33:17+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/webkul.com\/blog\/serialize-python-object-into-json-format\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/webkul.com\/blog\/serialize-python-object-into-json-format\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/webkul.com\/blog\/serialize-python-object-into-json-format\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/webkul.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Serialize Python object into JSON format\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/webkul.com\/blog\/#website\",\"url\":\"https:\/\/webkul.com\/blog\/\",\"name\":\"Webkul Blog\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/webkul.com\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/webkul.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/webkul.com\/blog\/#organization\",\"name\":\"WebKul Software Private Limited\",\"url\":\"https:\/\/webkul.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png\",\"contentUrl\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png\",\"width\":380,\"height\":380,\"caption\":\"WebKul Software Private Limited\"},\"image\":{\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/webkul\/\",\"https:\/\/x.com\/webkul\",\"https:\/\/www.instagram.com\/webkul\/\",\"https:\/\/www.linkedin.com\/company\/webkul\",\"https:\/\/www.youtube.com\/user\/webkul\/\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/8b41d806aa1c7ec81f958437fac62e5b\",\"name\":\"Prakash Kumar\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/0423ba8e78c94fb466768b44982d402aec0a1bb0ea274e7907672f740c41d776?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/0423ba8e78c94fb466768b44982d402aec0a1bb0ea274e7907672f740c41d776?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g\",\"caption\":\"Prakash Kumar\"},\"url\":\"https:\/\/webkul.com\/blog\/author\/prakash-kumar163\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Serialize Python object into JSON format - Webkul Blog","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/webkul.com\/blog\/serialize-python-object-into-json-format\/","og_locale":"en_US","og_type":"article","og_title":"Serialize Python object into JSON format - Webkul Blog","og_description":"Sometimes we have to parse\/serialize the python object data in JSON for sending the data to the server. There are may way to do this, one of such way is to serialize the object inside the __repr__ method. &nbsp; In this post, I am going to explain to you that how to create a JSON [...]","og_url":"https:\/\/webkul.com\/blog\/serialize-python-object-into-json-format\/","og_site_name":"Webkul Blog","article_publisher":"https:\/\/www.facebook.com\/webkul\/","article_published_time":"2018-04-24T12:33:17+00:00","og_image":[{"width":1200,"height":630,"url":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-og.png","type":"image\/png"}],"author":"Prakash Kumar","twitter_card":"summary_large_image","twitter_creator":"@webkul","twitter_site":"@webkul","twitter_misc":{"Written by":"Prakash Kumar","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/webkul.com\/blog\/serialize-python-object-into-json-format\/#article","isPartOf":{"@id":"https:\/\/webkul.com\/blog\/serialize-python-object-into-json-format\/"},"author":{"name":"Prakash Kumar","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/8b41d806aa1c7ec81f958437fac62e5b"},"headline":"Serialize Python object into JSON format","datePublished":"2018-04-24T12:33:17+00:00","mainEntityOfPage":{"@id":"https:\/\/webkul.com\/blog\/serialize-python-object-into-json-format\/"},"wordCount":153,"commentCount":0,"publisher":{"@id":"https:\/\/webkul.com\/blog\/#organization"},"keywords":["json","python","serialization"],"articleSection":["blog"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/webkul.com\/blog\/serialize-python-object-into-json-format\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/webkul.com\/blog\/serialize-python-object-into-json-format\/","url":"https:\/\/webkul.com\/blog\/serialize-python-object-into-json-format\/","name":"Serialize Python object into JSON format - Webkul Blog","isPartOf":{"@id":"https:\/\/webkul.com\/blog\/#website"},"datePublished":"2018-04-24T12:33:17+00:00","breadcrumb":{"@id":"https:\/\/webkul.com\/blog\/serialize-python-object-into-json-format\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/webkul.com\/blog\/serialize-python-object-into-json-format\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/webkul.com\/blog\/serialize-python-object-into-json-format\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/webkul.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Serialize Python object into JSON format"}]},{"@type":"WebSite","@id":"https:\/\/webkul.com\/blog\/#website","url":"https:\/\/webkul.com\/blog\/","name":"Webkul Blog","description":"","publisher":{"@id":"https:\/\/webkul.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/webkul.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/webkul.com\/blog\/#organization","name":"WebKul Software Private Limited","url":"https:\/\/webkul.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png","contentUrl":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png","width":380,"height":380,"caption":"WebKul Software Private Limited"},"image":{"@id":"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/webkul\/","https:\/\/x.com\/webkul","https:\/\/www.instagram.com\/webkul\/","https:\/\/www.linkedin.com\/company\/webkul","https:\/\/www.youtube.com\/user\/webkul\/"]},{"@type":"Person","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/8b41d806aa1c7ec81f958437fac62e5b","name":"Prakash Kumar","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/0423ba8e78c94fb466768b44982d402aec0a1bb0ea274e7907672f740c41d776?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/0423ba8e78c94fb466768b44982d402aec0a1bb0ea274e7907672f740c41d776?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g","caption":"Prakash Kumar"},"url":"https:\/\/webkul.com\/blog\/author\/prakash-kumar163\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/122486","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/users\/86"}],"replies":[{"embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/comments?post=122486"}],"version-history":[{"count":4,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/122486\/revisions"}],"predecessor-version":[{"id":122499,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/122486\/revisions\/122499"}],"wp:attachment":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/media?parent=122486"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/categories?post=122486"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/tags?post=122486"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}