import sqlite3
import json

# Reconnect to the SQLite database
conn = sqlite3.connect('example.db')
cur = conn.cursor()

# Execute the query
cur.execute("SELECT Name, Address FROM your_table")

# Fetch all rows from the executed query
rows = cur.fetchall()

# Get column names from the cursor description
column_names = [desc[0] for desc in cur.description]

# Convert the fetched rows to a list of dictionaries
result = [dict(zip(column_names, row)) for row in rows]

# Convert the list of dictionaries to a JSON object
json_result = json.dumps(result, indent=2)

print(json_result)

# Close the cursor and connection
cur.close()
conn.close()


try this one, because  i am hoping that your using sqlite3
On Tuesday, June 18, 2024 at 11:07:15 AM UTC+5:30 shriya...@gmail.com wrote:

> I want to convert my cursor.fetchall() response to JSON object.
>
> Like example: 
> [
> {Name: "XYZ-1", Address: "ABC-1"},
> {Name: "XYZ-2", Address: "ABC-2"}  
> ]
>
> currenty I am getting like below
> [
> ["XYZ-1","ABC-1"],
> ["XYZ-2","ABC-2"]
> ]
>
> Please help me how to parse in my custom models
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-rest-framework+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-rest-framework/0c42e75d-8a8a-4873-ae78-f879a72ddf69n%40googlegroups.com.

Reply via email to