JSON basics

Decide which of twelve snippets are valid JSON, name the six data types, find values in a nested order, and explain seven common errors. Check your answers in a free JSON formatter afterwards.

Exercise25 minBachelor +2CC BY 4.0
4 tasks · 25 min

What you will learn

  • Tell valid JSON from invalid
  • Name the six JSON data types
  • Read a value from a nested object and an array
  • Explain the most common JSON errors

JSON (JavaScript Object Notation) is how most web services exchange data: when an app shows the weather or a shop shows your order, the data usually arrives as JSON. It is plain text with a few strict rules. Paste any snippet into the JSON Formatter to check it. This exercise takes about 25 minutes and has four tasks.

The rules in short

  • An object is wrapped in curly brackets { } and holds pairs of a key and a value: "city": "Hamburg".

  • An array is a list in square brackets [ ], with the items separated by commas.

  • Keys and strings always use double quotes " ". Single quotes are not allowed.

  • Numbers use a point for decimals (12.5) and have no quotes and no leading zeros.

  • true, false and null are written in lower case, without quotes.

  • No comma after the last item, and no comments.

Worked example: one order

An order from a fictional shop
{
  "order_id": 1042,
  "paid": false,
  "customer": {
    "name": "Anna Berger",
    "company": "Northwind Trading"
  },
  "items": [
    { "product": "Desk lamp", "units": 2 },
    { "product": "Cable", "units": 5 }
  ],
  "note": null
}

Read it from the outside in. The whole thing is an object with five keys. order_id holds a number, paid a boolean and note is null (no value). customer holds another object, so the company is found at customer, then company. items holds an array of two objects; the first item is a desk lamp.

Common mistakes

  • Copying from a word processor. It may turn straight quotes " into curly ones, which JSON does not accept.

  • A missing comma between two pairs, or one too many after the last.

  • Brackets that do not match: every { needs a } and every [ a ]. Indenting the text, as the formatter does, makes this easy to see.

  • Numbers as strings by accident. "42" is a string; 42 is a number. Calculations need the number.

Extension for fast finishers

Write a JSON object for yourself as a fictional customer of Fourth Coffee: a name (string), a number of loyalty points (number), whether you get the newsletter (boolean), your three favourite drinks (array) and an address as a nested object with street and city. Check it in the JSON Formatter until it is valid. Use made-up details only.

One valid answer
A fictional customer
{
  "name": "Sam Reed",
  "loyalty_points": 120,
  "newsletter": true,
  "favourite_drinks": ["flat white", "chai latte", "hot chocolate"],
  "address": {
    "street": "Harbour Road 5",
    "city": "Hamburg"
  }
}
Teacher notes

How to run it (25 minutes). 5 minutes: read the rules and the worked example together; ask learners to point at an object, an array and a boolean. 15 minutes: the four tasks, alone. 5 minutes: learners paste two "not valid" snippets into the JSON Formatter and read its error message aloud.

Typical mistakes.

  • Calling "just a string" invalid because it is not an object. A single string, number or array is valid JSON too.

  • Missing that {"zip": 01234} is invalid. It looks harmless.

  • Counting the keys inside "customer" when asked how many items the array holds.

Follow-up questions.

  • Why are phone numbers and postcodes better stored as strings?

  • Why are the rules so strict? (Every program must read the text the same way.)

  • Where else have you met data like this, for example in app settings or exported data?

Differentiation.

  • Easier: give learners the rules on paper and let them check each snippet in the formatter straight away.

  • Harder: learners write the same order as a CSV table and explain what gets lost (the nesting) and how they would handle the two items.

Use it with a class
  1. 1. Copy the class link and share it with your class.
  2. 2. Learners finish the exercise and get a result code.
  3. 3. Paste the codes into Class results to see everyone at once.
Change the tasks in the builder

Reuse this material

CC BY 4.0

You may copy, adapt and share it, also commercially, as long as you credit ManyPI and link back.

"JSON basics" by ManyPI (https://manypi.com/edu/json-basics-exercise), licensed under CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/)