How to Convert JSON to Array in TypeScript

A store dashboard gets its orders from an API as one long JSON string, and the chart code needs a real, typed list. That’s the everyday reason to convert JSON to array in TypeScript.

The short version is JSON.parse() plus a type annotation, then a runtime check so bad records can’t sneak in:

const orders: Order[] = JSON.parse(json);

In this tutorial, I’ll convert JSON to array in TypeScript by building a small order loader step by step. Every output comes from a real run on TypeScript 7.0.2 and Node.js v22.22.2 with --strict on.

Here’s what we’re building: a loader that accepts a JSON array or object, drops bad records and survives broken JSON.

Convert JSON to array in TypeScript: VS Code running orders_loader.ts; the terminal prints 3 orders, 0 skipped, $268.75 and a Bad JSON message
The finished loader: 3 orders, a $268.75 total and a caught JSON error.

Before You Start: The Sample Orders JSON

You need Node.js and TypeScript 5+ with strict mode. Each order in our sample has three fields, described by an Order interface (see TypeScript interfaces for arrays of objects):

FieldTypeNotes
idnumberOrder number, 5001-5003
customerstringJenna Morales, Tyler Brooks, Dana Whitfield
totalnumberUSD: 89.50, 142.25, 37.00

Step 1: Parse the JSON String into a Typed Array

First, the quickest working conversion. The annotation tells your editor each item’s shape:

interface Order {
  id: number;
  customer: string;
  total: number;
}

const json = `[
  {"id": 5001, "customer": "Jenna Morales", "total": 89.5},
  {"id": 5002, "customer": "Tyler Brooks", "total": 142.25},
  {"id": 5003, "customer": "Dana Whitfield", "total": 37}
]`;

const orders: Order[] = JSON.parse(json);

console.log(Array.isArray(orders), orders.length, "orders");
console.log(orders.map((o) => o.customer));

Output:

true 3 orders
[ 'Jenna Morales', 'Tyler Brooks', 'Dana Whitfield' ]
VS Code running JSON.parse on the orders string with an Order[] annotation; the terminal prints true 3 orders and three customer names
JSON.parse() returns a real array with three orders.

Checkpoint: the terminal prints true 3 orders and the three customer names.

JSON.parse() returns any, so the Order[] annotation is a promise you make, not a check. For a project file, importing the JSON file gives you types for free.

Step 2: Confirm the Parsed Value Is an Array

Next, we stop trusting any. Typing the result as unknown forces a check before use:

const json = `[{"id": 5001, "total": 89.5}]`;

const data: unknown = JSON.parse(json);
console.log(data.length);

Output:

orders_unknown.ts(4,13): error TS18046: 'data' is of type 'unknown'.
VS Code flagging data.length on an unknown JSON.parse result: red squiggle, Problems badge and tsc error TS18046
tsc stops with TS18046 because data is unknown.

TypeScript refuses to read .length until you prove what the value is. I use Array.isArray(data) as that proof; the finished loader below relies on it.

Checkpoint: tsc reports error TS18046: 'data' is of type 'unknown'. An empty array still passes, so pair this with checking if an array is null or empty.

Step 3: Validate Each JSON Record with a Type Guard

Now, we check every item. Here Tyler’s total arrives as the string "142.25". A type guard (a function returning v is Order) filters it out; checking if an object implements an interface explains the pattern:

interface Order {
  id: number;
  customer: string;
  total: number;
}

function isOrder(v: unknown): v is Order {
  if (typeof v !== "object" || v === null) return false;
  const o = v as Record<string, unknown>;
  return typeof o.id === "number"
    && typeof o.customer === "string"
    && typeof o.total === "number";
}

const json = `[
  {"id": 5001, "customer": "Jenna Morales", "total": 89.5},
  {"id": 5002, "customer": "Tyler Brooks", "total": "142.25"},
  {"id": 5003, "customer": "Dana Whitfield", "total": 37}
]`;

const data: unknown[] = JSON.parse(json);
const orders = data.filter(isOrder);       // Order[]
const bad = data.filter((v) => !isOrder(v));

console.log("Valid:", orders.map((o) => o.id));
console.log("Rejected:", bad);

Output:

Valid: [ 5001, 5003 ]
Rejected: [ { id: 5002, customer: 'Tyler Brooks', total: '142.25' } ]
VS Code running an isOrder type guard over parsed JSON; the terminal shows valid ids 5001 and 5003 and the rejected order with total as a string
The guard keeps orders 5001 and 5003 and rejects the string total.

Checkpoint: Valid: [ 5001, 5003 ], with order 5002 listed under Rejected.

Since isOrder is a type predicate, filter() returns Order[] (see filtering arrays in TypeScript).

Step 4: Turn a JSON Object into an Array with Object.values()

Some APIs send an object keyed by ID instead of an array. Same orders, new shape:

type Sale = { customer: string; total: number };

const json = `{
  "5001": {"customer": "Jenna Morales", "total": 89.5},
  "5002": {"customer": "Tyler Brooks", "total": 142.25}
}`;

const byId: Record<string, Sale> = JSON.parse(json);

const sales = Object.values(byId);
const withIds = Object.entries(byId).map(
  ([id, s]) => ({ id: Number(id), ...s }),
);

console.log(sales);
console.log(withIds);

Output:

[
  { customer: 'Jenna Morales', total: 89.5 },
  { customer: 'Tyler Brooks', total: 142.25 }
]
[
  { id: 5001, customer: 'Jenna Morales', total: 89.5 },
  { id: 5002, customer: 'Tyler Brooks', total: 142.25 }
]
VS Code converting a JSON object keyed by order ID into arrays with Object.values and Object.entries; the terminal prints both arrays
Object.values() drops the keys; Object.entries() keeps them as ids.

Checkpoint: two arrays, and the second has id: 5001 and id: 5002 back as numbers. For plain objects, see converting an object to an array in TypeScript.

Test the JSON-to-Array Order Loader

The finished loadOrders() combines all four steps:

interface Order {
  id: number;
  customer: string;
  total: number;
}

function isOrder(v: unknown): v is Order {
  if (typeof v !== "object" || v === null) return false;
  const o = v as Record<string, unknown>;
  return typeof o.id === "number"
    && typeof o.customer === "string"
    && typeof o.total === "number";
}

function loadOrders(text: string) {
  try {
    const data: unknown = JSON.parse(text);
    const list = Array.isArray(data) ? data : Object.values(data ?? {});
    const orders = list.filter(isOrder);
    return { orders, skipped: list.length - orders.length };
  } catch (err) {
    console.log("Bad JSON:", (err as Error).message);
    return { orders: [] as Order[], skipped: 0 };
  }
}

const json = `[
  {"id": 5001, "customer": "Jenna Morales", "total": 89.5},
  {"id": 5002, "customer": "Tyler Brooks", "total": 142.25},
  {"id": 5003, "customer": "Dana Whitfield", "total": 37}
]`;

const { orders, skipped } = loadOrders(json);
const total = orders.reduce((sum, o) => sum + o.total, 0);
console.log(`${orders.length} orders, ${skipped} skipped,`,
  `$${total.toFixed(2)}`);
loadOrders(`[{"id": 5004,}]`);

Output:

3 orders, 0 skipped, $268.75
Bad JSON: Expected double-quoted property name in JSON at position 13 (line 1 column 14)

It returns three orders totaling $268.75 with nothing skipped. The trailing comma in the last call throws a SyntaxError, which the loader logs instead of crashing. To reshape the result for a chart, see mapping an array of objects in TypeScript.

Troubleshooting: Why Does JSON.parse() Throw a SyntaxError?

  • Trailing commas or single quotes: JSON allows neither; fix the source or the loader logs Bad JSON.
  • Dates stay strings: JSON.parse() never creates Date objects; map them with new Date().

Important: as Order[] and annotations don’t check anything at runtime.

Where to Take It Next with JSON Arrays

Generate the Order interface from a sample payload with converting JSON to a TypeScript interface. Going the other way? See converting objects to JSON in TypeScript.

A few more TypeScript array guides pair well with this one:

Frequently Asked Questions

How do I convert JSON to an array in TypeScript?

Call JSON.parse() on the string and annotate the result as Order[].

Does JSON.parse() check the type in TypeScript?

No. It returns any, so validate items with a type guard.

How do I convert a JSON object to an array?

Parse it, then use Object.values() for the values or Object.entries() to keep the keys.

Why should I type JSON.parse() as unknown?

It forces a check, like Array.isArray(), before use.

How do I handle invalid JSON?

Wrap JSON.parse() in try...catch; invalid input throws a SyntaxError.

Where is JSON.parse() documented?

See JSON.parse() on MDN.