How can I select part of json with Function or Set node

guy: I have a HTTP_REQUEST with this information… an example … please

[
  {
    "id": "107",
    "street": "PASEO DEL REY",
    "city": "DREAM CITY",
    "restaurants": [
      {
        "id": "1234",
        "name": "the blue sky",
        "type": "bistro",
        "size": "medium"
      },
      {
        "id": "9876",
        "name": "the coffe shop",
        "type": "coffe",
        "size": "small"
      },
      {
        "id": "9876",
        "name": "the coffe shop",
        "type": "coffe",
        "size": "small"
      }
    ],
    "stores": [
      {
        "id": "23765",
        "name": "best clothes",
        "type": "retail",
        "size": "medium"
      },
      {
        "id": "23765",
        "name": "PC Market",
        "type": "electronic",
        "size": "medium"
      }
    ]
  }
]

How can I select “stores” node but only “name” and “type” fields ? I try using Set and Function node but i’m confused.

My Wish

[
  {
    "stores": [
      {
        "name": "best clothes",
        "type": "retail"
      },
      {
        "name": "PC Market",
        "type": "electronic"
      }
    ]
  }
]

Hi @Alexis,

The interconnected elements also caused me problems. But I think I have quite a workable solution now.

If it’s just about getting an attribute out of an array, Array.map() is probably best.

But this is already well documented: JavaScript Code Snippets | Docs

1 Like

Hi @Alexis, welcome to the community :tada:

@BillAlex beat me to it since I had to answer the door :smiley:, but let me post my suggestion anyway:

Since you specifically asked for a Function node, I think this snippet should do the job based on your example data:

return [{
  json: {
    stores: items[0].json.stores.map(s => { return {
        name: s.name,
        type: s.type
      }
    })
  }
}];

image

Here is an example you can copy straight into your canvas:

Example Workflow
1 Like