{"id":4631,"date":"2019-05-16T10:48:52","date_gmt":"2019-05-16T05:18:52","guid":{"rendered":"https:\/\/pheonixsolutions.com\/blog\/?p=4631"},"modified":"2019-05-23T10:55:34","modified_gmt":"2019-05-23T05:25:34","slug":"python-json-encode-and-dcode","status":"publish","type":"post","link":"https:\/\/pheonixsolutions.com\/blog\/python-json-encode-and-dcode\/","title":{"rendered":"PYTHON JSON: ENCODE AND DCODE"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\">PYTHON JSON: ENCODE AND DCODE<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Date posted :16\/05\/2019<\/strong><\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>What is json:<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">     <strong>JSON&nbsp;<\/strong>stands for<strong>&nbsp;J<\/strong>ava<strong>S<\/strong>cript&nbsp;<strong>O<\/strong>bject&nbsp;<strong>N<\/strong>otation. It may be a string or text format. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">     JSON is similar to python dictionary. Python has a inbuilt library for JSON.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">syntax of JSON:<\/h3>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"> {\n     \n      \u201ckey\u201d : \u201cvalue\u201d,\n      \u201ckey\u201d : \u201cvalue\u201d,\n\n\n    }\n<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Encoding:<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"> Converting python data into JSON is called encoding. dump() method used to encode the python data.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"> import json\n\n x = {\n   \"name\": \"YYY\",\n  \"age\": 45,\n   \"married\": True,\n   \"children\": (\"Alice\",\"AAA\"),\n   \"pets\": ['Dog'],\n   \"cars\": [\n     {\"model\": \"ROLS\", \"mpg\": 15.1},\n     {\"model\": \"ZZZ\", \"mpg\": 18.1}\n   ]\n }\n# sorting result in asscending order by keys:\n sorted_string = json.dumps(x, indent=4, sort_keys=True)\n print(sorted_string)\n<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Output:<\/h3>\n\n\n\n<figure class=\"wp-block-image\"><img fetchpriority=\"high\" decoding=\"async\" width=\"572\" height=\"320\" src=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2019\/05\/PYTHON.png\" alt=\"\" class=\"wp-image-4632\" srcset=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2019\/05\/PYTHON.png 572w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2019\/05\/PYTHON-300x168.png 300w\" sizes=\"(max-width: 572px) 100vw, 572px\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Decoding:<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"> Decoding is done with the help of load() and loads() methods.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import json  # json library imported\n# json data string\nperson_data = '{  \"value\":  { \"name\":  \"YYY\",  \"sex\":  \"male\",  \"age\":  28}}'\n# Decoding or converting JSON format in dictionary using loads()\ndict_obj = json.loads(person_data)\nprint(dict_obj)\n# check type of dict_obj\nprint(\"Type of dict_obj\", type(dict_obj))\n# get human object details\nprint(\"VALUES\",  dict_obj.get('value'))\n<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Output:<\/h3>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" width=\"614\" height=\"85\" src=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2019\/05\/python1.png\" alt=\"\" class=\"wp-image-4633\" srcset=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2019\/05\/python1.png 614w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2019\/05\/python1-300x42.png 300w\" sizes=\"(max-width: 614px) 100vw, 614px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Thanks for using pheonixsolutions.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You find this tutorial helpful? Share with your friends to keep it alive.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>PYTHON JSON: ENCODE AND DCODE Date posted :16\/05\/2019 What is json: JSON&nbsp;stands for&nbsp;JavaScript&nbsp;Object&nbsp;Notation. It may be a string or text [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[1022],"tags":[654,653,652,292],"class_list":["post-4631","post","type-post","status-publish","format-standard","hentry","category-web-architecture","tag-decode","tag-encode","tag-json","tag-python","psol-cat-web-architecture"],"jetpack_publicize_connections":[],"jetpack_shortlink":"https:\/\/wp.me\/phn2x7-1cH","jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/4631","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/comments?post=4631"}],"version-history":[{"count":0,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/4631\/revisions"}],"wp:attachment":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/media?parent=4631"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/categories?post=4631"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/tags?post=4631"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}