Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Reading a Full JSON Payload | Datatyper i n8n
AI-automatiseringsarbeidsflyter med n8n

bookReading a Full JSON Payload

A JSON object is a single structured thing; an array is a list of things. Most real payloads are just objects containing arrays of objects.

order.json

order.json

copy
  • Starts with { … } → top-level object;
  • order_id → string;
  • customer → nested object with email;
  • line_itemsarray [ ... ] of objects (each line item);
  • currency → string;
  • paid → boolean.

What the Payload Actually Describes

The object encodes an order: who (customer), what (line_items), money (currency), and status (paid). Totals aren't stored, they're derived. With a consistent shape, any node (or tiny code step) can compute them later. Before handing anything to AI, spell out the exact calculation you want. This prevents guessing, keeps outputs predictable, and makes the result easy to validate.

AI prompt:

Tiny Helpers

Sometimes you don't need a full function, a short expression or a few lines of code can handle totals or counts right inside your workflow. Use this to count how many line_items exist.

{{$json["line_items"] ? $json["line_items"].length : 0}}

If you need to calculate totals, a quick reduce loop inside a Code node does the trick.

const items = $json.line_items ?? [];
const subtotal = items.reduce((s, it) => s +
  (Number(it.qty) || 0) * (Number(it.price) || 0), 0);
return [{ json: { subtotal: Number(subtotal.toFixed(2)) } }];
Note
Note

If upstream accidentally sends "19.99" as a string, cast with Number(...) before math.

question mark

What does the expression {{$json["line_items"].length}} return?

Select the correct answer

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 3. Kapittel 3

Spør AI

expand

Spør AI

ChatGPT

Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår

Awesome!

Completion rate improved to 4.17

bookReading a Full JSON Payload

Sveip for å vise menyen

A JSON object is a single structured thing; an array is a list of things. Most real payloads are just objects containing arrays of objects.

order.json

order.json

copy
  • Starts with { … } → top-level object;
  • order_id → string;
  • customer → nested object with email;
  • line_itemsarray [ ... ] of objects (each line item);
  • currency → string;
  • paid → boolean.

What the Payload Actually Describes

The object encodes an order: who (customer), what (line_items), money (currency), and status (paid). Totals aren't stored, they're derived. With a consistent shape, any node (or tiny code step) can compute them later. Before handing anything to AI, spell out the exact calculation you want. This prevents guessing, keeps outputs predictable, and makes the result easy to validate.

AI prompt:

Tiny Helpers

Sometimes you don't need a full function, a short expression or a few lines of code can handle totals or counts right inside your workflow. Use this to count how many line_items exist.

{{$json["line_items"] ? $json["line_items"].length : 0}}

If you need to calculate totals, a quick reduce loop inside a Code node does the trick.

const items = $json.line_items ?? [];
const subtotal = items.reduce((s, it) => s +
  (Number(it.qty) || 0) * (Number(it.price) || 0), 0);
return [{ json: { subtotal: Number(subtotal.toFixed(2)) } }];
Note
Note

If upstream accidentally sends "19.99" as a string, cast with Number(...) before math.

question mark

What does the expression {{$json["line_items"].length}} return?

Select the correct answer

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 3. Kapittel 3
some-alt