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.
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.
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.
The value !== null guard matters because typeof null is also "object" in JavaScript.
Why typeof Alone Is Not Enough
This is the classic trap:
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.
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:
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.isArrayto detect arrays. - Use
value !== null && typeof value === "object"for non-array objects. - Do not rely on
typeofalone, because arrays andnullalso look like objects. - Normalize mixed API payload shapes early if the rest of your code expects one consistent structure.

