如何将来自 JSON 的数据插入到 Oracle APEX 中的表中

问题描述 投票:0回答:1

我在 Oracle Apex 中有一个应用程序,需要使用来自 Webhook 的 JSON 数据,并将其插入到我的表中。

数据采用以下结构:

{
  "event_type": "WEBHOOK.MARKED_OPPORTUNITY",
  "entity_type": "CONTACT",
  "event_identifier": "my-event-identifier",
  "timestamp": "2018-03-13T14:09:02.724-03:00",
  "event_timestamp": "2018-03-13T14:07:04.254-03:00",
  "contact": {
    "uuid": "c2f3d2b3-7250-4d27-97f4-eef38be32f7f",
    "email": "[email protected]",
    "name": "Contact Name",
    "job_title": "Developer",
    "bio": "This is my bio",
    "website": "http://rdstation.com.br",
    "personal_phone": "48 30252598",
    "mobile_phone": "48 30252598",
    "city": "Florianópolis",
    "facebook": "Contact Facebook",
    "linkedin": "Contact Linkedin",
    "twitter": "Contact Twitter",
    "tags": [
      "tag 1",
      "tag 2"
    ],
    "cf_custom_field_example": [
      "Option1",
      "Option2"
    ],
    "company": {
      "name": "Company Example 0"
    },
    "funnel": {
      "name": "default",
      "lifecycle_stage": "Lead",
      "opportunity": false,
      "contact_owner_email": "[email protected]",
      "interest": 20,
      "fit": 0,
      "origin": "Orgânico"
    }
  }
}

我的问题是,当我触发 POST 时,如何将一些数据插入到我的表中?

我正在我的 RESTful 数据服务 POST 方法上尝试这个 PL/SQL

DECLARE
    new_id INTEGER;
    current_date DATE;
    blob_body BLOB := :body;
    clob_variable CLOB := CONVERT_TO_CLOB(blob_body);
    
    v_name VARCHAR2(100);
    v_email VARCHAR2(100);
BEGIN
    SELECT SYSDATE INTO current_date FROM dual;

    DECLARE
        v_json_obj JSON_OBJECT_T;
    BEGIN
        v_json_obj := JSON_OBJECT_T.parse(clob_variable);
        
        v_name := v_json_obj.get_String('name');
        v_email := v_json_obj.get_String('email');
    END;

    INSERT INTO LEADS (ID, NOME, EMAIL)
    VALUES (121212, v_name, v_email)
    RETURNING ID INTO new_id;

    :status_code := 201;
    :forward_location := '../employees/' || new_id;

EXCEPTION
    WHEN VALUE_ERROR THEN
        :errmsg := 'Wrong value.';
        :status_code := 400;
    WHEN OTHERS THEN 
        :status_code := 400;
        :errmsg := SQLERRM;
END;

我需要在我的表 LEADS 上插入 JSON 中的姓名和电子邮件值。

oracle-apex
1个回答
0
投票

使用JSON_TABLE将json解析为关系数据。首先验证sql中的语句(我用的是sqldeveloper)

create table leads (ID NUMBER, NOME VARCHAR2(100), EMAIL VARCHAR2(100));

INSERT INTO leads (id, nome,email) 
WITH json_tab AS
(
    SELECT
'{
  "event_type": "WEBHOOK.MARKED_OPPORTUNITY",
  "entity_type": "CONTACT",
  "event_identifier": "my-event-identifier",
  "timestamp": "2018-03-13T14:09:02.724-03:00",
  "event_timestamp": "2018-03-13T14:07:04.254-03:00",
  "contact": {
    "uuid": "c2f3d2b3-7250-4d27-97f4-eef38be32f7f",
    "email": "[email protected]",
    "name": "Contact Name",
    "job_title": "Developer",
    "bio": "This is my bio",
    "website": "http://rdstation.com.br",
    "personal_phone": "48 30252598",
    "mobile_phone": "48 30252598",
    "city": "Florianópolis",
    "facebook": "Contact Facebook",
    "linkedin": "Contact Linkedin",
    "twitter": "Contact Twitter",
    "tags": [
      "tag 1",
      "tag 2"
    ],
    "cf_custom_field_example": [
      "Option1",
      "Option2"
    ],
    "company": {
      "name": "Company Example 0"
    },
    "funnel": {
      "name": "default",
      "lifecycle_stage": "Lead",
      "opportunity": false,
      "contact_owner_email": "[email protected]",
      "interest": 20,
      "fit": 0,
      "origin": "Orgânico"
    }
  }
}'  AS json_data FROM dual
)
SELECT 121212, name, email
  FROM  json_tab t,
        JSON_TABLE(
            json_data,
            '$' columns (
                name VARCHAR2(4000) PATH '$.contact.name',
                email VARCHAR2(4000) PATH '$.contact.email'
            )
        );

1 row inserted.

然后将该代码放入您的 ords 端点的 pl/sql 中(代码未经测试)。

DECLARE
    new_id INTEGER;
    current_date DATE;
BEGIN
    current_date := SYSDATE; -- no need for select into for this 

    INSERT INTO leads (id, nome,email) 
    WITH json_tab AS
    (
        SELECT :body AS json_data FROM dual
    )
    SELECT 121212, name, email
      FROM  json_tab t,
            JSON_TABLE(
                json_data,
                '$' columns (
                    name VARCHAR2(4000) PATH '$.contact.name',
                    email VARCHAR2(4000) PATH '$.contact.email'
                )
            )
    RETURNING ID INTO new_id;

    :status_code := 201;
    :forward_location := '../employees/' || new_id;

EXCEPTION
    WHEN VALUE_ERROR THEN
        :errmsg := 'Wrong value.';
        :status_code := 400;
    WHEN OTHERS THEN 
        :status_code := 400;
        :errmsg := SQLERRM;
END;

© www.soinside.com 2019 - 2024. All rights reserved.