Reading JSON responses

Take a real nested response apart, learn the six JSON types by sight, and write code that reaches the value you want without crashing when the shape is not what you expected.

The first lesson ended with body.data.words, a single string sitting one level inside the envelope. Most responses are not that polite. Here is what the Country Lookup API returns for country=Japan:

{
  "status": "ok",
  "error": null,
  "data": {
    "search": "JAPAN",
    "countries": [
      {
        "name": {
          "common": "Japan",
          "official": "Japan",
          "native": {
            "jpn": { "official": "日本", "common": "日本" }
          }
        },
        "tld": [".jp", ".みんな"],
        "cca2": "JP",
        "ccn3": "392",
        "cca3": "JPN",
        "independent": true,
        "capital": ["Tokyo"],
        "region": "Asia",
        "subregion": "Eastern Asia",
        "languages": { "jpn": "Japanese" },
        "latlng": [36, 138],
        "landlocked": false,
        "flag": "🇯🇵"
      }
    ]
  }
}

(A few fields are trimmed to fit.) Suppose all you want is the capital city. It is in there, four levels deep, inside two different lists. By the end of this page you will be able to look at a response like this and write the path to any value in it on the first try.

Try it liveCountry Lookup2 credits
GEThttps://api.apiverve.com/v1/countrylookup
curl "https://api.apiverve.com/v1/countrylookup?country=Japan" \
  -H "x-api-key: YOUR_API_KEY"
Sign in or create a free account to run this with your own key.

Find the capital in the response before reading on. How many brackets do you pass through to get there?

The six things a JSON value can be

JSON is plain text, but it describes a small, fixed set of value types. Learn to recognise them by sight and any response becomes readable.

TypeLooks likeExample from the response above
StringText in double quotes"Japan", "392"
NumberDigits, no quotes36, 138
Booleantrue or false, no quotes"independent": true
Nullnull, no quotes"error": null in the envelope
Object{ } holding "key": value pairs"name": { "common": ... }
Array[ ] holding values in order"capital": ["Tokyo"]

Two details in that table trip people up.

"392" is a string, not a number. The quotes decide the type, not what the characters look like. ccn3 is a country's numeric ISO code, but it is sent as text because codes like "036" (Australia) have a leading zero that a number would throw away. If you compare country.ccn3 === 392 in JavaScript, it is always false. Compare with "392", or convert deliberately.

false and null are not the same as missing. "landlocked": false is a real answer: Japan is not landlocked. You will see later in this lesson why the difference between a value being false, null and absent is one of the most useful things to get right.

Objects and arrays are the two containers, and they are how nesting happens. An object holds named values, so you reach into it by name. An array holds values in order, so you reach into it by position, starting at zero.

Check yourself

In the Country Lookup response, what type is the value of "ccn3": "392"?

Answer the check above to continue.

Run every example on your own key. Free plan, no card.

Scaling up?

Volume pricing, custom SLAs, and dedicated support for high-traffic teams.

Contact sales