0
0
Djangoframework~20 mins

Returning JSON with JsonResponse in Django - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
JsonResponse Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this Django view using JsonResponse?
Consider this Django view function. What JSON response will the client receive?
Django
from django.http import JsonResponse

def sample_view(request):
    data = {'name': 'Alice', 'age': 30}
    return JsonResponse(data)
A{"name": "Alice", "age": 30}
B"{'name': 'Alice', 'age': 30}"
C{"name": "Alice", "age": "30"}
D{"name": "Alice"}
Attempts:
2 left
💡 Hint
JsonResponse converts the dictionary to JSON with correct types.
📝 Syntax
intermediate
2:00remaining
Which option will cause a TypeError when returning JsonResponse?
Which of these Django view return statements will raise a TypeError?
Django
from django.http import JsonResponse

def view(request):
    # return statement here
Areturn JsonResponse('hello world')
Breturn JsonResponse({'fruits': ['apple', 'banana']})
Creturn JsonResponse({'count': 5})
Dreturn JsonResponse({'valid': True})
Attempts:
2 left
💡 Hint
JsonResponse expects a dictionary or list, not a plain string.
state_output
advanced
2:00remaining
What is the Content-Type header of this JsonResponse?
Given this Django view, what is the Content-Type header sent in the HTTP response?
Django
from django.http import JsonResponse

def view(request):
    return JsonResponse({'status': 'ok'})
Atext/html
Bapplication/json
Capplication/javascript
Dtext/plain
Attempts:
2 left
💡 Hint
JsonResponse sets the content type for JSON data.
🔧 Debug
advanced
2:00remaining
Why does this JsonResponse return an empty JSON object?
This Django view returns an empty JSON object {} instead of the expected data. Why?
Django
from django.http import JsonResponse

def view(request):
    data = {'count': 10}
    return JsonResponse(data, safe=False)
Asafe=False allows non-dict data, but data is a dict, so it returns empty
Bsafe=False disables validation, but data is dict so it returns correctly
Csafe=False is required only for non-dict data; here it has no effect
Dsafe=False causes JsonResponse to ignore data and return empty object
Attempts:
2 left
💡 Hint
safe=False is for allowing non-dict data like lists.
🧠 Conceptual
expert
3:00remaining
Which option correctly returns a JSON list with JsonResponse?
You want to return a JSON list ['red', 'green', 'blue'] from a Django view using JsonResponse. Which return statement is correct?
Django
from django.http import JsonResponse

def colors_view(request):
    colors = ['red', 'green', 'blue']
    # return statement here
Areturn JsonResponse(colors)
Breturn JsonResponse(colors, safe=True)
Creturn JsonResponse({'colors': colors})
Dreturn JsonResponse(colors, safe=False)
Attempts:
2 left
💡 Hint
JsonResponse requires safe=False to return non-dict data like lists.