Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9110725
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T03:20:15+00:00 2026-06-17T03:20:15+00:00

Im trying to create a JSON result with Pymongo , Mongodb and Django. I

  • 0

Im trying to create a JSON result with Pymongo , Mongodb and Django. I have created a Django view that contains a pymongo query and want to return the results in json. However, the results produced arent valid json (according to jsonlint)

Here is my django view.

from django.http import HttpResponse
import pymongo
from datetime import datetime, timedelta
import json
from bson import json_util

#setup database connection
try:
    conn = pymongo.Connection()
    db = conn.mydatabase
except:
    print('Error: Unable to connect to database.')
    conn = None

def querypeople(request):
    result = db.people.find({}).sort('name')
    json_docs = []
    for doc in result:
        json_doc = json.dumps(doc, default=json_util.default, sort_keys=True, indent=4)
        json_docs.append(json_doc)
    return HttpResponse(json_docs, content_type='application/json')

Which produces this output. (Notice commas missing between each document and [ ] should be enclosing the entire result. This makes it invalid JSON.) What am i doing wrong?

“_id”: {
“$oid”: “50c596ab2b9afbbc85ed202a”
},
“date_added”: {
“$date”: 1355126443473
},
“name”: “Al Landon”
}{
“_id”: {
“$oid”: “50c5b9d92b9afbc3f1e7c90c”
},
“company”: “Corrs”,
“date_added”: {
“$date”: 1355135449179
},
“name”: “Andrew Lumsden”,
“title”: “A Partner”

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T03:20:17+00:00Added an answer on June 17, 2026 at 3:20 am

    As I understood, you return with response list, not json string. Try:

    json_docs = json.dumps(list(result), default=json_util.default, sort_keys=True, indent=4)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to create a query that will result in a list of
I am trying to add string values in JsonArray and after that create Json
trying to create a dependant select function that fetch json value from database, but
I am trying to create a JSONMessage class that can parse a json string
I am trying to create a .csv file with data that I have stored
I am trying to create controller actions which will return either JSON or partial
so i have an already working jquery ajax application that i'm trying to create
I'm trying to create an MVC view that will display some data in a
I am trying to create a json file from a rake task using rabl.
Trying to use Jbuilder to create some JSON views for my app, but the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.