Posted on

What Is JSON? A Beginner’s Guide with Real API Examples

Monochrome technical illustration for What Is JSON?

JSON is a small text format for representing data. APIs use it to describe request bodies and responses with named keys, values, arrays and nested objects.

In the Weather Station example, JSON carries a temperature reading from a terminal or device to an API. It also carries the record, status details and schema metadata back to the client.

A JSON object has keys and values

{
  "data": {
    "temperature": 22.5
  }
}Code language: JSON / JSON with Comments (json)
  • data is a key whose value is another object.
  • temperature is a key whose value is the number 22.5.
  • Double quotes surround key names and text values.
  • Braces mark the start and end of each object.

The JSON types you will use

TypeExampleUse
String"outdoor"Text
Number22.5Measurements and quantities
BooleantrueYes/no state
NullnullExplicitly empty value
Array["red", "blue"]Ordered list
Object{"temperature":22.5}Named fields

A valid temperature record

{"temperature":22.5}Code language: JSON / JSON with Comments (json)

The API validates this value against the endpoint schema. Because temperature is a number, 22.5 is valid while "22.5" is text and may be rejected or interpreted differently. Send the type the schema expects.

What a collection response looks like

{
  "count": 1,
  "fields": [
    {"name": "temperature", "label": "Temperature", "field_type": "number"}
  ],
  "records": [
    {
      "uuid": "…",
      "data": {"temperature": 22.5},
      "created_at": "…"
    }
  ]
}Code language: JSON / JSON with Comments (json)

The outer object groups related response information. records is an array because a collection can contain many records. Each record’s data object contains the fields defined by the endpoint.

Common invalid JSON

{"temperature": 22.5,}Code language: JSON / JSON with Comments (json)

The trailing comma is invalid JSON. JSON also requires double quotes, not single quotes, around keys and text:

{'temperature': 'warm'}Code language: JavaScript (javascript)

This example has both single quotes and a text value where a numeric temperature is expected. A parser or API may return a 400 response. Read the response body for the field and validation detail.

JSON is not the same as a Python dictionary

Python dictionaries use Python syntax. JSON is text sent over HTTP. Python uses True, False and None; JSON uses lowercase true, false and null. Use a JSON library rather than hand-editing complex payloads.

import json

payload = {"temperature": 22.5}
body = json.dumps(payload)
print(body)Code language: JavaScript (javascript)

Inspect JSON with curl

curl -i "https://meowmeowscratch.com/api/v1/YOUR_USERNAME/weather-station/readings/" \
  -H "Authorization: Bearer $MEOW_APP_API_KEY"Code language: JavaScript (javascript)

Use the response status and body together. A successful response might contain 200 OK and a JSON collection. A malformed body usually produces a client error such as 400; missing or invalid credentials produce 401.

Next step

Now learn how GET, POST, PATCH and DELETE change what an endpoint does in HTTP Methods Explained. Then use the Meow SDK guide to send JSON from Python.