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-Automatiseringsarbejdsgange 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

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 3. Kapitel 3

Spørg AI

expand

Spørg AI

ChatGPT

Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat

Suggested prompts:

Can you show me an example of the JSON input and expected output?

How do I handle missing or invalid qty or price values in line_items?

Can you explain how to round the subtotal to 2 decimals in JavaScript?

Awesome!

Completion rate improved to 4.17

bookReading a Full JSON Payload

Stryg for at vise menuen

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

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 3. Kapitel 3
some-alt