JSON
Object Detection
Data Parsing
Arrays
JavaScript

Determine if Json results is object or array

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

When you parse JSON in JavaScript, the result can be an object, an array, or even a primitive such as a string or number. The reliable approach is to parse once and then inspect the resulting value with Array.isArray and a guarded object check.

Parse First, Then Inspect

JSON text is just a string until you call JSON.parse. Only after parsing does it make sense to ask whether the result is an array or an object.

javascript
1const raw = '{"name":"Ada","role":"admin"}';
2const value = JSON.parse(raw);
3
4console.log(value.name);

At that point, value is a JavaScript value rather than raw JSON text.

Detect Arrays with Array.isArray

If the parsed result might be a list, use Array.isArray.

javascript
1const list = JSON.parse('[1, 2, 3]');
2const record = JSON.parse('{"id":1,"name":"Ada"}');
3
4console.log(Array.isArray(list));
5console.log(Array.isArray(record));

This is the correct array check because arrays are a special kind of object in JavaScript. Generic type checks do not separate them cleanly.

Detect Plain JSON Objects

After ruling out arrays, you can test for a non-null object.

javascript
1function classifyJson(raw) {
2  const value = JSON.parse(raw);
3
4  if (Array.isArray(value)) {
5    return "array";
6  }
7
8  if (value !== null && typeof value === "object") {
9    return "object";
10  }
11
12  return "primitive";
13}
14
15console.log(classifyJson('[1, 2]'));
16console.log(classifyJson('{"ok":true}'));
17console.log(classifyJson('"hello"'));
18console.log(classifyJson('null'));

The value !== null guard matters because typeof null is also "object" in JavaScript.

Why typeof Alone Is Not Enough

This is the classic trap:

javascript
1const value = JSON.parse('[1, 2, 3]');
2
3if (typeof value === "object") {
4  console.log("object");
5}

That condition is true for arrays as well, so it does not answer the real question.

If your code treats arrays differently from plain objects, Array.isArray must come first.

Working with API Responses

This check is common when an API sometimes returns a single record and sometimes a list. A small normalization helper can keep the rest of the code simpler.

javascript
1async function fetchItems(url) {
2  const response = await fetch(url);
3  const data = await response.json();
4
5  if (Array.isArray(data)) {
6    return data;
7  }
8
9  if (data !== null && typeof data === "object") {
10    return [data];
11  }
12
13  throw new Error("Unexpected JSON payload");
14}

This lets downstream code work with one predictable shape.

If You Already Have a Parsed Value

Sometimes the value is already parsed for you, such as when you use response.json() or receive a value from another function. In that case, do not call JSON.parse again.

Use the same checks directly on the value:

javascript
1function isJsonObject(value) {
2  return value !== null && typeof value === "object" && !Array.isArray(value);
3}
4
5console.log(isJsonObject({ name: "Ada" }));
6console.log(isJsonObject(["Ada"]));
7console.log(isJsonObject(null));

Avoid Manual String Inspection

You may see code that looks at the first non-whitespace character and guesses that [ means array while { means object. That can work in narrow cases, but it is weaker than parsing because it does not validate the JSON and breaks easily on malformed input.

Parsing once is more robust because it gives you both syntax validation and runtime type inspection.

Common Pitfalls

The biggest mistake is using typeof value === "object" and assuming that means plain object. Arrays and null both break that assumption.

Another mistake is parsing the same JSON text multiple times. Parse once, store the result, then inspect the stored value.

Developers also sometimes forget that valid JSON can be a string, number, boolean, or null. Not every payload is an object or an array.

Finally, if you already got the value from response.json(), treat it as parsed data, not as raw JSON text.

Summary

  • Parse JSON first, then inspect the resulting JavaScript value.
  • Use Array.isArray to detect arrays.
  • Use value !== null && typeof value === "object" for non-array objects.
  • Do not rely on typeof alone, because arrays and null also look like objects.
  • Normalize mixed API payload shapes early if the rest of your code expects one consistent structure.

Course illustration
Course illustration

All Rights Reserved.