Select values from json child-elements

Hi there, I’m trying to select some child-values from some json data I receive from a woocommerce webhook. Specifically, I’m looking to extract the “sku” values (and only that) from the products purchased on the order received. So, the json data from woocommerce looks like this (simplified):

[
    {
        "body": {
            "id": 9182,
            "parent_id": 0,
            "number":9615,
            "line_items": [
                {
                    "id":1606,
                    "name":"product-name-1",
                    "quantity":1,
                    "sku":"NN99-1234",
                    "price":20
                },
                 {
                    "id":1608,
                    "name":"product-name-2,
                    "quantity":1,
                    "sku":"RR88-7890",
                    "price":15
                }
             ]
        },
        "header": {
            "host":"n8n.thinkmojo.com",
            "x-real-ip":"35.209.249.90"
        },
        "query": {
        }
    }
]

The problem is that I can’t seem to find a way to select just the “sku” values. I can only find a way to get either the whole “line_items” values (that don’t need), or the sku of one specific product only. But I want the sku of each and everyone of the products from that order.

Here’s what I tried that in a function node (webhook->function node).

{{$node[“Webhook1”].json[“body”][“line_items”]}}

But this gives me a too much data:

[Array: [{"id":1614,"name":"test-seb-api-2","product_id":9604,"variation_id":0,"quantity":1,"tax_class":"","subtotal":"0.00","subtotal_tax":"0.00","total":"0.00","total_tax":"0.00","taxes":[],"meta_data":[{"id":14453,"key":"_reduced_stock","value":"1"}],"sku":"NN99-1234","price":0},{"id":1615,"name":"test-seb-api","product_id":9592,"variation_id":0,"quantity":1,"tax_class":"","subtotal":"0.00","subtotal_tax":"0.00","total":"0.00","total_tax":"0.00","taxes":[],"meta_data":[{"id":14454,"key":"_reduced_stock","value":"1"}],"sku":"AQ67-2376-2","price":0}]]

I also tried this:

{{$node[“Webhook1”].json[“body”][“line_items”][0][“sku”]}}

But this gives me the sku of only one product.

NN99-1234

If we take this example, my goal is to end up with these values:

NN99-1234
AQ67-2376-2

I’m sure there’s an easy way to do this, but an’t seem to find it. Any help would be amazing. Thanks a lot!

ahh yeah, line_items is an array. You gotta iterate thought it in order to get both values or reference them individually like {{$node[“Webhook1”].json[“body”][“line_items”][0][“sku”]}} and {{$node[“Webhook1”].json[“body”][“line_items”][1][“sku”]}}. Either way, probably you want to use that data in the next node (by using expressions) so I will recommend you map the data to a format n8n understand as shown in the example below.

Let me know if that it’s not clear enough.

{
  "nodes": [
    {
      "parameters": {},
      "name": "Start",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        250,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "return [\n  {\n    json: {\n        \"body\": {\n            \"id\": 9182,\n            \"parent_id\": 0,\n            \"number\":9615,\n            \"line_items\": [\n                {\n                    \"id\":1606,\n                    \"name\":\"product-name-1\",\n                    \"quantity\":1,\n                    \"sku\":\"NN99-1234\",\n                    \"price\":20\n                },\n                 {\n                    \"id\":1608,\n                    \"name\":\"product-name-2\",\n                    \"quantity\":1,\n                    \"sku\":\"RR88-7890\",\n                    \"price\":15\n                }\n             ]\n        },\n        \"header\": {\n            \"host\":\"n8n.thinkmojo.com\",\n            \"x-real-ip\":\"35.209.249.90\"\n        },\n        \"query\": {\n        }\n    }\n  }  \n]"
      },
      "name": "Function",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        570,
        300
      ],
      "notesInFlow": true,
      "notes": "mockup webhook"
    },
    {
      "parameters": {
        "functionCode": "const result = []\n\nconst lineItems = items[0].json.body.line_items\n\nfor (const lineItem of lineItems) {\n   result.push({ json: lineItem })\n}\n\nreturn result;\n\n\n\n"
      },
      "name": "Function1",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        820,
        300
      ]
    }
  ],
  "connections": {
    "Start": {
      "main": [
        [
          {
            "node": "Function",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Function": {
      "main": [
        [
          {
            "node": "Function1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

Awesome thanks! I think that works now!

Ricardo, i have the same question but i dont have an idea how i can solve it ?

This function dont work:

const result = []

const lineItems = items[0].json.methodResponse.member

for (const lineItem of lineItems) {
   result.push({ json: lineItem })
}

return result;



Any idea ?

Stefan