Help with JSON Situation

Hi! I’m having a little problem with a JSON that i’m receiving from a form in my workflow, the JSON is:

{
   "origem":"Facebook",
   "empreendimento":[
      {
         "id":"11",
         "nome":"AGL 24 - Park Club Cidade Jardim"
      }
   ],
   "campos_adicionais":[
      {
         "slug":"cf_em_qual_cidade_voce_quer_o_seu_ape",
         "valor":"Águas Lindas - GO"
      },
      {
         "slug":"SDR",
         "valor":"Alef"
      }
   ]
}

I need to filter de JSON and take the value of “slug”:“SDR” in “Campos_adicionais”, but since I got more then one “slug” key, I cant filter with the following sintax: {{$json[“body”][“campos_adcionais”][“slug”]}}.

Does anyone know how can I filter by the value instead the key? I don’t know if it’s possible, so if anyone has another solution it will be really helpful.

Thank you!

You can extract it in a Function node:

return items.map(i => {
  const o = i.json.body.campos_adicionais.find(i => i.slug === 'SDR');
  return { json: { valor: o && o.valor || 'Not found' }}
});
2 Likes

Welcome to the community @ViniciusSouza

Let us know if that helps.

1 Like

It did not work, just got this error

Test it like this:

{
  "nodes": [
    {
      "parameters": {},
      "name": "Start",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        250,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "const items = [ \n{ json :{\n   \"origem\":\"Facebook\",\n   \"empreendimento\":[\n      {\n         \"id\":\"11\",\n         \"nome\":\"AGL 24 - Park Club Cidade Jardim\"\n      }\n   ],\n   \"campos_adicionais\":[\n      {\n         \"slug\":\"cf_em_qual_cidade_voce_quer_o_seu_ape\",\n         \"valor\":\"Águas Lindas - GO\"\n      },\n      {\n         \"slug\":\"SDR\",\n         \"valor\":\"Alef\"\n      }\n   ]\n   }\n}\n]\n\nreturn items;"
      },
      "name": "Function",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        530,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "return items.map(i => {\n  const o = i.json.campos_adicionais.find(i => i.slug === 'SDR');\n  return { json: { valor: o && o.valor || 'Not found' }}\n});\n"
      },
      "name": "Function1",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        800,
        300
      ]
    }
  ],
  "connections": {
    "Start": {
      "main": [
        [
          {
            "node": "Function",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Function": {
      "main": [
        [
          {
            "node": "Function1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}
1 Like

Worked! Thank you vary much @RicardoE105 and @ivov !

Of course, let us know if you have other questions.

1 Like

Hi, I’m trying to apply this solution. It works when I test it as @RicardoE105 suggested, but when I want to substitute the test json with the json resulting from a previous node, I can’t get it to work.
I tried

var j = $node["Webhook"]
const items = Object.keys(j).map(function(_) { return j[_]; })

return items;

but the result is empty.

When trying

var j = $node["Webhook"].data or .json
const items = Object.keys(j).map(function(_) { return j[_]; })

return items;

I get the message “ERROR: All returned items have to contain a property named “json”!”.

Any help would be greatly appreciated!

Hey @cdesseaux,
As the error message suggests, all the items should be wrapped within a JSON object. In your return statement, you need to have something similar to this return {json: item}. This code snippet will wrap each item into a JSON object. If you’re interested to learn more you should check out