Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Oppiskele Reading a Full JSON Payload | Datan Tyypit n8n:ssä
Tekoälyautomaatio Työnkulut n8n:llä

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

Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 3. Luku 3

Kysy tekoälyä

expand

Kysy tekoälyä

ChatGPT

Kysy mitä tahansa tai kokeile jotakin ehdotetuista kysymyksistä aloittaaksesi keskustelumme

Awesome!

Completion rate improved to 4.17

bookReading a Full JSON Payload

Pyyhkäise näyttääksesi valikon

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

Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 3. Luku 3
some-alt