Challenge - 5 Problems
JsonResponse Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2: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)
Attempts:
2 left
💡 Hint
JsonResponse converts the dictionary to JSON with correct types.
✗ Incorrect
JsonResponse converts the Python dictionary to a JSON string with proper JSON syntax and types. Numbers remain numbers, strings remain strings.
📝 Syntax
intermediate2: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
Attempts:
2 left
💡 Hint
JsonResponse expects a dictionary or list, not a plain string.
✗ Incorrect
Passing a plain string to JsonResponse causes a TypeError because it expects a dict or list to convert to JSON.
❓ state_output
advanced2: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'})
Attempts:
2 left
💡 Hint
JsonResponse sets the content type for JSON data.
✗ Incorrect
JsonResponse automatically sets the Content-Type header to 'application/json' to indicate JSON data.
🔧 Debug
advanced2: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)
Attempts:
2 left
💡 Hint
safe=False is for allowing non-dict data like lists.
✗ Incorrect
The safe parameter when False allows non-dict data like lists. Since data is a dict, safe=False has no effect and the data is returned correctly.
🧠 Conceptual
expert3: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
Attempts:
2 left
💡 Hint
JsonResponse requires safe=False to return non-dict data like lists.
✗ Incorrect
By default, JsonResponse expects a dictionary. To return a list, safe must be set to False.