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