{"id":6309,"date":"2023-03-11T11:13:34","date_gmt":"2023-03-11T05:43:34","guid":{"rendered":"https:\/\/www.techgropse.com\/blog\/?p=6309"},"modified":"2025-11-17T23:56:31","modified_gmt":"2025-11-17T18:26:31","slug":"how-to-return-sql-data-in-json-format-python","status":"publish","type":"post","link":"https:\/\/www.techgropse.com\/blog\/how-to-return-sql-data-in-json-format-python\/","title":{"rendered":"How To Return SQL Data In JSON Format Python?"},"content":{"rendered":"<p>To return SQL data in JSON format in Python, you can use the built-in <code>json<\/code> module and the <code>pymysql<\/code> package (assuming you are using MySQL as your database). Here&#8217;s an example of how to do it:<\/p>\n<div class=\"flex items-center relative text-gray-200 bg-gray-800 px-4 py-2 text-xs font-sans\">\n<p><strong>import mysql.connector<\/strong><br \/>\n<strong>import json<\/strong><\/p>\n<p><strong># Connect to the database<\/strong><br \/>\n<strong>conn = mysql.connector.connect(<\/strong><br \/>\n<strong>host=&#8221;localhost&#8221;,<\/strong><br \/>\n<strong>user=&#8221;username&#8221;,<\/strong><br \/>\n<strong>password=&#8221;password&#8221;,<\/strong><br \/>\n<strong>database=&#8221;database_name&#8221;<\/strong><br \/>\n<strong>)<\/strong><\/p>\n<p><strong># Create a cursor object<\/strong><br \/>\n<strong>cursor = conn.cursor()<\/strong><\/p>\n<p><strong># Execute the SQL query<\/strong><br \/>\n<strong>query = &#8220;SELECT * FROM table_name&#8221;<\/strong><br \/>\n<strong>cursor.execute(query)<\/strong><\/p>\n<p><strong># Fetch all rows and convert to a list of dictionaries<\/strong><br \/>\n<strong>rows = cursor.fetchall()<\/strong><br \/>\n<strong>result = []<\/strong><br \/>\n<strong>for row in rows:<\/strong><br \/>\n<strong>d = {}<\/strong><br \/>\n<strong>for i, col in enumerate(cursor.description):<\/strong><br \/>\n<strong>d[col[0]] = row[i]<\/strong><br \/>\n<strong>result.append(d)<\/strong><\/p>\n<p><strong># Convert the list of dictionaries to JSON and print it<\/strong><br \/>\n<strong>json_result = json.dumps(result)<\/strong><br \/>\n<strong>print(json_result)<\/strong><\/p>\n<\/div>\n<div class=\"group w-full text-gray-800 dark:text-gray-100 border-b border-black\/10 dark:border-gray-900\/50 bg-gray-50 dark:bg-[#444654]\">\n<div class=\"text-base gap-4 md:gap-6 md:max-w-2xl lg:max-w-2xl xl:max-w-3xl p-4 md:py-6 flex lg:px-0 m-auto\">\n<div class=\"relative flex w-[calc(100%-50px)] flex-col gap-1 md:gap-3 lg:w-[calc(100%-115px)]\">\n<div class=\"flex flex-grow flex-col gap-3\">\n<div class=\"min-h-[20px] flex flex-col items-start gap-4 whitespace-pre-wrap\">\n<div class=\"markdown prose w-full break-words dark:prose-invert light\">\n<p>In the above code, we first connect to the database using <code>pymysql<\/code>, then create a cursor object to execute the SQL query. We then fetch all rows from the query result and convert them to a list of dictionaries, where each dictionary represents a row in the table with column names as keys and their values as values. Finally, we convert the list of dictionaries to JSON format using the <code>json.dumps()<\/code> method and print it. This approach is commonly used in mobile app development to handle database interactions efficiently. This approach is commonly used in <a href=\"https:\/\/www.techgropse.com\/mobile-app-development-company-dubai-uae\"><b>mobile application development uae<\/b><\/a> to handle database interactions efficiently and ensure fast, reliable data loading across the region.<\/p>\n<p>Converting SQL data to JSON format in Python is a valuable skill for app development. If you\u2019re building a data-driven app, partnering with a skilled <a href=\"https:\/\/www.techgropse.com\/mobile-app-development-company-chicago\">mobile app development company in Chicago<\/a>,\u00a0<a href=\"https:\/\/www.techgropse.com\/mobile-app-development-company-atlanta\">Atlanta<\/a>, <a href=\"https:\/\/www.techgropse.com\/mobile-app-development-company-los-angeles\">Los Angeles<\/a>, <a href=\"https:\/\/www.techgropse.com\/app-development-dallas\">Dallas<\/a>, <a href=\"https:\/\/www.techgropse.com\/mobile-app-development-company-san-francisco\">San Francisco<\/a>, <a href=\"https:\/\/www.techgropse.com\/mobile-app-development-company-new-york\">New York<\/a>, or Texas can help you implement such features seamlessly into your project. Converting SQL data to JSON format in Python is a valuable skill for app development. If you&#8217;re building a data-driven app, partnering with the <a href=\"https:\/\/www.techgropse.com\/mobile-app-development-riyadh-saudi-arabia\"><b>best mobile app development company in Riyadh<\/b><\/a> can help you implement such seamless and scalable backend features into your project.<\/p>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"flex justify-between\">\n<div class=\"text-gray-400 flex self-end lg:self-center justify-center mt-2 gap-3 md:gap-4 lg:gap-1 lg:absolute lg:top-0 lg:translate-x-full lg:right-0 lg:mt-0 lg:pl-2 visible\"><\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>To return SQL data in JSON format in Python, you can use the built-in json module and the pymysql package (assuming you are using MySQL as your database). Here&#8217;s an example of how to do it: import mysql.connector import json # Connect to the database conn = mysql.connector.connect( host=&#8221;localhost&#8221;, user=&#8221;username&#8221;, password=&#8221;password&#8221;, database=&#8221;database_name&#8221; ) # Create [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":6311,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[11],"tags":[862,987,988,702],"table_tags":[],"country":[],"country_map":[],"class_list":["post-6309","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-news","tag-blogs","tag-how-to-return-sql-data-in-json-format-python","tag-return-sql-data-in-json-format-python","tag-tech-news"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.techgropse.com\/blog\/wp-json\/wp\/v2\/posts\/6309","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.techgropse.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.techgropse.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.techgropse.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.techgropse.com\/blog\/wp-json\/wp\/v2\/comments?post=6309"}],"version-history":[{"count":0,"href":"https:\/\/www.techgropse.com\/blog\/wp-json\/wp\/v2\/posts\/6309\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.techgropse.com\/blog\/wp-json\/wp\/v2\/media\/6311"}],"wp:attachment":[{"href":"https:\/\/www.techgropse.com\/blog\/wp-json\/wp\/v2\/media?parent=6309"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.techgropse.com\/blog\/wp-json\/wp\/v2\/categories?post=6309"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.techgropse.com\/blog\/wp-json\/wp\/v2\/tags?post=6309"},{"taxonomy":"table_tags","embeddable":true,"href":"https:\/\/www.techgropse.com\/blog\/wp-json\/wp\/v2\/table_tags?post=6309"},{"taxonomy":"country","embeddable":true,"href":"https:\/\/www.techgropse.com\/blog\/wp-json\/wp\/v2\/country?post=6309"},{"taxonomy":"country_map","embeddable":true,"href":"https:\/\/www.techgropse.com\/blog\/wp-json\/wp\/v2\/country_map?post=6309"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}