JSON格式的答案中带有问题“标题”的JSON

kxxlusnw  于 2023-03-24  发布在  其他
关注(0)|答案(1)|浏览(156)

我需要帮助采取下面的JSON,并将其转换为更好的格式JSON,或者至少是我使用的webhook将接受的格式.当我通过webhook将此数据发送到不同的网站.我不需要所有的,但最重要的是'答案'部分.不是所有的答案都只是简单的文本,有些是多项选择,等等...但我主要只需要有JSON代码,这将是这样的:

{
    "type": "choice",
    "choice": {
      "label": "Barcelona"
    },
    "field": {
      "id": "mNgn3likMf3O",
      "type": "multiple_choice",
      "ref": "01F3ZGM0W3745KR2FW1CP8Q5S4",
      "title": "Please select the pillars of a SASE-ready architecture that your organization has adopted and implemented."
    }
  },

但需要在JSON格式。我将需要有其他数据,以及,如电话号码,电子邮件等,但答案是我不知道如何处理的一部分。我使用的是一个中间件,将在PHP采取这些数据,并将其转换为JSON格式。
详细信息将在答案部分,你会看到一个“ref”,它引用了代码顶部的问题块。你能帮助得到答案部分,让“ref”从顶部调用匹配的“ref”,这将是直接的问题,并且在答案中也显示“标题”?这将允许我们拥有webhook将接受的正确格式,并且我们可以根据需要定位。这有帮助吗?

{
  "event_id": "01GW5AQQGETNY8SRM4MZHY1892",
  "event_type": "form_response",
  "form_response": {
    "form_id": "B05nYbDj",
    "token": "01GW5AQQGETNY8SRM4MZHY1892",
    "landed_at": "2023-03-22T18:54:08Z",
    "submitted_at": "2023-03-22T18:54:08Z",
    "definition": {
      "id": "B05nYbDj",
      "title": "Cisco SASE Survey for Burwood Group (copy)",
      "fields": [
        {
          "id": "mNgn3likMf3O",
          "ref": "01F3ZGM0W3745KR2FW1CP8Q5S4",
          "type": "multiple_choice",
          "title": "Please select the pillars of a SASE-ready architecture that your organization has adopted and implemented.",
          "properties": {}
        },
        {
          "id": "c5z21uSbVsHn",
          "ref": "833343d3-4840-4cb2-841f-37afdfbc3a89",
          "type": "opinion_scale",
          "title": "How would you *rate your company's overall SASE-ness*?",
          "properties": {}
        },
        {
          "id": "eUR85WQGMJmv",
          "ref": "e0d9b18c-45ce-4d24-ae51-35fa44c8c577",
          "type": "multiple_choice",
          "title": "Which of these *network solutions* are you currently employing in your organization?",
          "properties": {}
        },
        {
          "id": "SEHdZOtBuMPH",
          "ref": "80808a56-8eff-40fe-8267-cdbd8b5b2143",
          "type": "multiple_choice",
          "title": "Which of these *cloud security solutions* are you currently employing in your organization?",
          "properties": {}
        },
        {
          "id": "JyTK17cLO22J",
          "ref": "67427da4-b210-4253-adde-36dd524e91f8",
          "type": "multiple_choice",
          "title": "At your company, how often are *perimeter/edge security controls and monitoring capabilities* included in your network engineering and design?",
          "properties": {}
        },
        {
          "id": "Ndry3UZqKz9j",
          "ref": "968531e4-98fb-46f4-9694-dca9fc41d9fd",
          "type": "multiple_choice",
          "title": "Does your organization adopt a *zero-trust posture* when determining access to company resources?",
          "properties": {}
        },
        {
          "id": "9wS9mei2XxA4",
          "ref": "939b6dca-742c-4036-a48b-a9221b03cb28",
          "type": "multiple_choice",
          "title": "What *solutions/vendors* is your company leveraging to implement a *zero-trust approach*?",
          "properties": {}
        },
        {
          "id": "zpQHU05AiOHg",
          "ref": "2155696f-6c00-4953-b128-c8af2e090c7b",
          "type": "long_text",
          "title": "What are the top 3 *limitations and/or challenges *to achieve SASE success for your company?",
          "properties": {}
        },
        {
          "id": "HTFv9Xi8ffhL",
          "ref": "ac6ca834-9648-4a5a-91a5-42b39e4281ca",
          "type": "multiple_choice",
          "title": "Which *internal team in your organization* is primarily responsible for your *SASE strategy*?",
          "properties": {}
        },
        {
          "id": "u8J4DhCofU8e",
          "ref": "66395568-1533-437f-abe8-7a1b8c834b64",
          "type": "short_text",
          "title": "What is your *full name*?",
          "properties": {}
        },
        {
          "id": "HCDcgXhRPJcs",
          "ref": "c929257d-099a-4cd3-876b-1c1cdede1eb7",
          "type": "short_text",
          "title": "What is your *role and/or job title*?",
          "properties": {}
        },
        {
          "id": "Ua8uGbaVxtmt",
          "ref": "386728a9-c4d7-43d5-af2e-1cb822a1f5ce",
          "type": "short_text",
          "title": "What is the *name of your company or organization*?",
          "properties": {}
        },
        {
          "id": "Lt3LXb90adwu",
          "ref": "9913e0e9-bbe3-4696-961e-c377cf932edd",
          "type": "short_text",
          "title": "Where is* your company or organization located*?",
          "properties": {}
        },
        {
          "id": "nDL4kTJXeHjL",
          "ref": "cabf2dfc-319b-4066-a56f-cee5b0eaedcc",
          "type": "email",
          "title": "Please provide us your *business* *email address*.",
          "properties": {}
        },
        {
          "id": "0cPtqsbaL5ZD",
          "ref": "04360852-006a-48aa-a809-346a2c1109b6",
          "type": "phone_number",
          "title": "Finally, please provide us your *direct phone number* so we can verify your responses and information if required.",
          "properties": {}
        }
      ]
    },
    "answers": [
      {
        "type": "choice",
        "choice": {
          "label": "Barcelona"
        },
        "field": {
          "id": "mNgn3likMf3O",
          "type": "multiple_choice",
          "ref": "01F3ZGM0W3745KR2FW1CP8Q5S4"
        }
      },
      {
        "type": "number",
        "number": 42,
        "field": {
          "id": "c5z21uSbVsHn",
          "type": "opinion_scale",
          "ref": "833343d3-4840-4cb2-841f-37afdfbc3a89"
        }
      },
      {
        "type": "choice",
        "choice": {
          "label": "Barcelona"
        },
        "field": {
          "id": "eUR85WQGMJmv",
          "type": "multiple_choice",
          "ref": "e0d9b18c-45ce-4d24-ae51-35fa44c8c577"
        }
      },
      {
        "type": "choice",
        "choice": {
          "label": "Barcelona"
        },
        "field": {
          "id": "SEHdZOtBuMPH",
          "type": "multiple_choice",
          "ref": "80808a56-8eff-40fe-8267-cdbd8b5b2143"
        }
      },
      {
        "type": "choice",
        "choice": {
          "label": "Barcelona"
        },
        "field": {
          "id": "JyTK17cLO22J",
          "type": "multiple_choice",
          "ref": "67427da4-b210-4253-adde-36dd524e91f8"
        }
      },
      {
        "type": "choice",
        "choice": {
          "label": "Barcelona"
        },
        "field": {
          "id": "Ndry3UZqKz9j",
          "type": "multiple_choice",
          "ref": "968531e4-98fb-46f4-9694-dca9fc41d9fd"
        }
      },
      {
        "type": "choice",
        "choice": {
          "label": "Barcelona"
        },
        "field": {
          "id": "9wS9mei2XxA4",
          "type": "multiple_choice",
          "ref": "939b6dca-742c-4036-a48b-a9221b03cb28"
        }
      },
      {
        "type": "text",
        "text": "Lorem ipsum dolor",
        "field": {
          "id": "zpQHU05AiOHg",
          "type": "long_text",
          "ref": "2155696f-6c00-4953-b128-c8af2e090c7b"
        }
      },
      {
        "type": "choice",
        "choice": {
          "label": "Barcelona"
        },
        "field": {
          "id": "HTFv9Xi8ffhL",
          "type": "multiple_choice",
          "ref": "ac6ca834-9648-4a5a-91a5-42b39e4281ca"
        }
      },
      {
        "type": "text",
        "text": "Lorem ipsum dolor",
        "field": {
          "id": "u8J4DhCofU8e",
          "type": "short_text",
          "ref": "66395568-1533-437f-abe8-7a1b8c834b64"
        }
      },
      {
        "type": "text",
        "text": "Lorem ipsum dolor",
        "field": {
          "id": "HCDcgXhRPJcs",
          "type": "short_text",
          "ref": "c929257d-099a-4cd3-876b-1c1cdede1eb7"
        }
      },
      {
        "type": "text",
        "text": "Lorem ipsum dolor",
        "field": {
          "id": "Ua8uGbaVxtmt",
          "type": "short_text",
          "ref": "386728a9-c4d7-43d5-af2e-1cb822a1f5ce"
        }
      },
      {
        "type": "text",
        "text": "Lorem ipsum dolor",
        "field": {
          "id": "Lt3LXb90adwu",
          "type": "short_text",
          "ref": "9913e0e9-bbe3-4696-961e-c377cf932edd"
        }
      },
      {
        "type": "email",
        "email": "an_account@example.com",
        "field": {
          "id": "nDL4kTJXeHjL",
          "type": "email",
          "ref": "cabf2dfc-319b-4066-a56f-cee5b0eaedcc"
        }
      },
      {
        "type": "phone_number",
        "phone_number": "+34123456789",
        "field": {
          "id": "0cPtqsbaL5ZD",
          "type": "phone_number",
          "ref": "04360852-006a-48aa-a809-346a2c1109b6"
        }
      }
    ],
    "ending": {
      "ref": "default_tys"
    }
  }
wljmcqd8

wljmcqd81#

让我看看我是否理解了。
试试这个:

<?php

$data = json_decode (/* here the json string received */, true);

$defs = &$data['form_response']['definition']['fields'];
$answs = &$data['form_response']['answers'];

$defsRefs = array_column($defs, 'ref');

$arrResult = [];

foreach ($answs as $a)
{
    if (($key = array_search($a['field']['ref'], $defsRefs)) !== false)
    {
        $a['field']['title'] = $defs[$key]['title'];
        $arrResult[] = $a;
    }
}

$result = json_encode($arrResult /*, JSON_PRETTY_PRINT*/);

相关问题