Get Data from JSON (Webhook)

Describe the problem/error/question

We have json from webhook like this

[
  {
    "headers": {
      "host": "xxxxxxx.com",
      "x-real-ip": "0.0.0.0",
      "x-forwarded-for": "0.0.0.0",
      "x-forwarded-proto": "https",
      "connection": "close",
      "content-length": "107343",
      "accept": "application/json, text/plain, */*",
      "content-type": "application/json",
      "user-agent": "Tally Webhooks",
      "accept-encoding": "gzip, compress, deflate, br"
    },
    "params": {},
    "query": {},
    "body": {
      "eventId": "c7511b68-c5e1-44ab-be7a-1b4dea973d98",
      "createdAt": "2024-02-01T03:02:40.199Z",
      "data": {
        "responseId": "g7j2ZK",
        "submissionId": "g7j2ZK",
        "respondentId": "g05L5d",
        "formId": "wLpByp",
        "formName": "MAINTENANCE SUMMARY ",
        "createdAt": "2024-01-31T11:19:28.000Z",
        "fields": [
          {
            "key": "question_Zj4Zpa",
            "label": "Company",
            "type": "DROPDOWN",
            "value": [
              "b45b12a8-1867-4279-833c-ec53a1f040da"
            ],
            "options": [
              {
                "id": "975ce9ef-c6dd-4fc8-bd3d-f73fe6584aff",
                "text": "Options 1"
              },
              {
                "id": "50338599-5e6d-425c-a5bb-f3d96b99354e",
                "text": "Options 2"
              },
              {
                "id": "b45b12a8-1867-4279-833c-ec53a1f040da",
                "text": "Options 3"
              },
              {
                "id": "73066652-7e9f-4a82-be1b-76728675c368",
                "text": "Options 4"
              },
              {
                "id": "01f6e47c-1200-40c1-8ccb-629dbe96593c",
                "text": "Options 5"
              }
            ]
          },
          {
            "key": "question_vG7KMX",
            "label": null,
            "type": "DROPDOWN",
            "value": [
              "7e5b4246-bafd-455d-94f4-80158adc0991"
            ],
            "options": [
              {
                "id": "8c5efe4e-2c7c-4e78-9094-f9a3141cb3c7",
                "text": "PIC A"
              },
              {
                "id": "7e5b4246-bafd-455d-94f4-80158adc0991",
                "text": "PIC B"
              },
              {
                "id": "a67e7b20-33e5-4e84-b2f6-31678259cbdd",
                "text": "PIC C"
              },
              {
                "id": "80c23cf4-5460-4132-bb42-4351da0564a7",
                "text": "PIC D"
              },
              {
                "id": "f4644e07-d769-41e6-a4ac-8a86e19a2729",
                "text": "PIC E"
              },
              {
                "id": "a0d13322-7164-46f0-9782-6768543f32d4",
                "text": "PIC F "
              },
              {
                "id": "30773f95-fedb-4d1b-b939-b1c64a21403c",
                "text": "PIC G"
              },
              {
                "id": "f1cceae4-4c01-4451-b38c-1f253440e99b",
                "text": "H"
              },
              {
                "id": "0e2a7be2-6512-4ce0-b7a6-3c98e97da3ce"
              }
            ]
          }
        ]
      }
    }
  }
]

how can we get the value like this

Company : Options 3
PIC : PIC B

and for company and PIC is random based on the results of the json.

i have tried this javascript

// Mendapatkan data JSON
const jsonData =  [
  {
    "headers": {
      "host": "n8njulia.klgsys.com",
      "x-real-ip": "107.178.206.213",
      "x-forwarded-for": "107.178.206.213",
      "x-forwarded-proto": "https",
      "connection": "close",
      "content-length": "107343",
      "accept": "application/json, text/plain, */*",
      "content-type": "application/json",
      "user-agent": "Tally Webhooks",
      "accept-encoding": "gzip, compress, deflate, br"
    },
    "params": {},
    "query": {},
    "body": {
      "eventId": "c7511b68-c5e1-44ab-be7a-1b4dea973d98",
      "createdAt": "2024-02-01T03:02:40.199Z",
      "data": {
        "responseId": "g7j2ZK",
        "submissionId": "g7j2ZK",
        "respondentId": "g05L5d",
        "formId": "wLpByp",
        "formName": "MAINTENANCE SUMMARY ",
        "createdAt": "2024-01-31T11:19:28.000Z",
        "fields": [
          {
            "key": "question_Zj4Zpa",
            "label": "Company",
            "type": "DROPDOWN",
            "value": [
              "b45b12a8-1867-4279-833c-ec53a1f040da"
            ],
            "options": [
              {
                "id": "975ce9ef-c6dd-4fc8-bd3d-f73fe6584aff",
                "text": "Options 1"
              },
              {
                "id": "50338599-5e6d-425c-a5bb-f3d96b99354e",
                "text": "Options 2"
              },
              {
                "id": "b45b12a8-1867-4279-833c-ec53a1f040da",
                "text": "Options 3"
              },
              {
                "id": "73066652-7e9f-4a82-be1b-76728675c368",
                "text": "Options 4"
              },
              {
                "id": "01f6e47c-1200-40c1-8ccb-629dbe96593c",
                "text": "Options 5"
              }
            ]
          },
          {
            "key": "question_vG7KMX",
            "label": null,
            "type": "DROPDOWN",
            "value": [
              "7e5b4246-bafd-455d-94f4-80158adc0991"
            ],
            "options": [
              {
                "id": "8c5efe4e-2c7c-4e78-9094-f9a3141cb3c7",
                "text": "PIC A"
              },
              {
                "id": "7e5b4246-bafd-455d-94f4-80158adc0991",
                "text": "PIC B"
              },
              {
                "id": "a67e7b20-33e5-4e84-b2f6-31678259cbdd",
                "text": "PIC C"
              },
              {
                "id": "80c23cf4-5460-4132-bb42-4351da0564a7",
                "text": "PIC D"
              },
              {
                "id": "f4644e07-d769-41e6-a4ac-8a86e19a2729",
                "text": "PIC E"
              },
              {
                "id": "a0d13322-7164-46f0-9782-6768543f32d4",
                "text": "PIC F "
              },
              {
                "id": "30773f95-fedb-4d1b-b939-b1c64a21403c",
                "text": "PIC G"
              },
              {
                "id": "f1cceae4-4c01-4451-b38c-1f253440e99b",
                "text": "H"
              },
              {
                "id": "0e2a7be2-6512-4ce0-b7a6-3c98e97da3ce"
              }
            ]
          }
        ]
      }
    }
  }
]

// Mendapatkan nilai Company
const companyValue = jsonData[0].body.data.fields.find(field => field.key === "question_Zj4Zpa").value[0];
const companyText = jsonData[0].body.data.fields.find(field => field.key === "question_Zj4Zpa").options.find(option => option.id === companyValue).text;

// Mendapatkan nilai PIC
const picValue = jsonData[0].body.data.fields.find(field => field.key === "question_vG7KMX").value[0];
const picText = jsonData[0].body.data.fields.find(field => field.key === "question_vG7KMX").options.find(option => option.id === picValue).text;

// Menampilkan hasil
return [ { companyText , picText }];
console.log(jsonData);

And this’s javascript is working with result is

so, how we can set “const jsonData =” from node “Webhook”?

If you have another solution for this, please tell me how.
Sorry, I’m not familiar with javascript or anything else.
Thank you for your help.

Information on your n8n setup

  • n8n version: 0.236.2
  • Database (default: SQLite): PostgreSQL
  • n8n EXECUTIONS_PROCESS setting (default: own, main):
  • Running n8n via (Docker, npm, n8n cloud, desktop app): Docker
  • Operating system: Linux

Hi there @rusdiaman :wave: You should be able to just $('Webhook').all() or $('Webhook').first() and get the data you’re looking for :+1: