⚒️ JSON data utility: extract key-value pairs by index

190 views · ⚒️ Engineering

Description

Extract a key–value pair by index from JSON to fields in n8n

This template takes a JSON object and a row index and returns exactly one key–value pair at that index. It’s a handy helper when you only need a single entry from a structured JSON payload (e.g., picking one form field for downstream logic).

Who’s it for

How it works

  1. Manual Trigger (When clicking ‘Test workflow’) starts the flow.
  2. Set → Input JSON Node holds your sample payload with:
    • myData: an object of key → value pairs.
    • rowIndex: a 0‑based index indicating which pair to extract.
  3. Code (Python) → Find Key‑Value Pair iterates myData and returns [key, value] at rowIndex as result.
  4. Set → Key maps result[0] to a field named result.
  5. Set → Value maps result[1] to a field named result[1].
  6. The selected key and value are then available to any downstream nodes.

How to set up

  1. Open the workflow and select Input JSON Node.
  2. Replace the sample with your own JSON:
    {
      "myData": { "name": "Alice", "age": "30", "city": "Paris" },
      "rowIndex": "1"
    }
  3. Click Execute Workflow. Check the Key and Value nodes for the outputs.

Requirements

How to customize

Add‑ons

Use Case Examples

Common troubleshooting

IssuePossible CauseSolution
“Index out of range”rowIndex is larger than the number of keysUse a valid 0‑based index; add a guard in the Code node to clamp or default.
Wrong key returnedObject key order differs from expectationsObject key order isn’t guaranteed across sources—prefer pick by key name for reliability.
Empty/invalid outputmyData is not an object or is emptyEnsure myData is a flat object with at least one key.
Python errorsCode node’s Python runtime not availableEnable Python in the Code node or convert the snippet to JavaScript.
Value type mismatchValue isn’t a stringCast as needed in the Set node or normalize types in the Code node.

Need Help?

If you’d like this to pick by key, handle nested JSON, accept data via Webhook or fully customized to your needs, write to us and we’ll adapt the template to your exact use case.

📥 Import

Download workflow.json and import into n8n: Workflow menu → Import from File

📖 Importing guide · 🔑 Credential setup