We're sorry but this app doesn't work properly without JavaScript enabled. Please enable it to continue.

This lesson's interactive features are locked, please to keep using them

JSON Syntax

JSON (JavaScript Object Notation), is a standard for representing structured data based on JavaScript's object syntax. It is commonly used to transmit data in web apps via HTTP. For example, the HTTP pyfetch() requests we have been using in this course have been returning Jello projects, users, and issues as JSON.

JSON supports the following primitive data types:

  • Strings, e.g. "Hello, World!"
  • Numbers, e.g. 42 or 3.14
  • Booleans, e.g. true
  • Null, e.g. null

And the following collection types:

  • Arrays, e.g. [1, 2, 3]
  • Object literals, e.g. {"key": "value"}

JSON looks a lot like Python dictionaries and lists. The following is valid JSON data:

{
  "movies": [
    {
      "id": 1,
      "genre": "Action",
      "title": "Iron Man",
      "director": "Jon Favreau"
    },
    {
      "id": 2,
      "genre": "Action",
      "title": "The Avengers",
      "director": "Joss Whedon"
    }
  ]
}

Parsing HTTP Responses As JSON

Python provides us with tools to help us work with JSON. After making an HTTP request with pyfetch(), we get a response object. That response object offers us methods that help us interact with the response. One such method is the .json() method. The .json() method takes the response stream returned by a pyfetch request and returns an awaitable that resolves into a Python object (dictionaries and lists) parsed from the JSON body of the HTTP response!

resp = await pyfetch(...)
python_object_response = await resp.json()

Assignment

Our get_projects function is almost done, we just need to parse the response data as JSON and return it.

The result of this method is not JSON. It is the result of taking JSON data from the HTTP response body and parsing that input into a Python object.