如何在SQL中使用JSON_TABLE查询带有嵌套数组的JSON数据?

cl25kdpy  于 2023-05-30  发布在  其他
关注(0)|答案(1)|浏览(92)

输入代码我是JSON的新手,尝试使用JSON_TABLE查询复杂的JSON数据结构。这是我提出的数据和查询。问题是phonenumber是一个数组,其中包含在数组中,并且不显示结果。任何帮助将不胜感激。

JSON数据示例

{"instId ":1," instappId ":1000,"instappNumber":"Z1009","appDate":"2020 - 09 - 19T09:29:04.633","contactInfo":{"instId ":1,"instappId":1000,"firstName":"Dan","middleName":null,"lastName":"Wood","emailAddress":"dan. wood@www.example.com","phoneNumbers":email.com1,"phoneID:2001,"phoneType":"家庭电话","电话号码":"1234567890",}}]"workHistory":[{"instId ":1, [ { "instId": 1,
"companyiterationID:1,"companyName":"ABC Inc," startDate ":"2019 - 01 - 11,"结束日期":空,

}
        ]

}

查询

选择A。* FROM mytable b,JSON_TABLE(b. json_data,'$. data [*]' COLUMNS(instId number(5)PATH '$. instId',instappId number(15)PATH'$. instappId',instappNumber VARCHAR2(30)PATH '$. instappNumber',appDate DATE PATH'$. appDate',NESTED PATH '$. contactInfo [*]' COLUMNS(firstName VARCHAR2(50)PATH '$. firstName',middleName VARCHAR2(50)PATH '$. middleName',lastName VARCHAR2(50)PATH'$. lastName',emailAddress VARCHAR2(50)PATH '$. emailAddress',NESTED PATH'$. phoneNumbers [*]'COLUMNS(phoneNumberId NUMBER(15)PATH'$. phoneNumberId',phoneType VARCHAR2(50)PATH'$. phoneType',phoneNumber VARCHAR2(50)PATH'$. phoneNumber')),NESTED PATH'$. workHistory [*]' COLUMNS(companyName VARCHAR2(50)PATH'$. companyname',startDate DATE PATH'$. startDate' endDate DATE PATH'$。endDate'))A;

bn31dyow

bn31dyow1#

我检查了你的代码,肯定,你的JSON是无效的。

  • 仔细检查你的JSON --首先检查你是如何构建这个对象的,或者你是如何接收它的--因为它是无效的。

这是我能够构建的代码:

SELECT *
FROM JSON_TABLE('{
    "instId": 1,
    "instappId": 1000,
    "instappNumber": "Z1009",
    "appDate": "2020-09-19T09:29:04.633",
    "contactInfo": {
        "instId": 1,
        "instappId": 1000,
        "firstName": "Dan",
        "middleName": null,
        "lastName": "Wood",
        "emailAddress": "dan.wood@email.com",
        "phoneNumbers": [{
                "instId": 1,
                "phoneID": 2001,
                "phoneType": "Home Phone",
                "phoneNumber": "1234567890"
            }]
        },
    "workHistory": [{
        "instId": 1,
        "companyiterationID": 1,
        "companyName": "ABC Inc",
        "startDate": "2019-01-11",
        "EndDate": null
      }]
}', '$[*]' COLUMNS(instId NUMBER(5) PATH '$.instId',
           instappId number(15) PATH '$.instappId',
           instappNumber VARCHAR2(30) PATH '$.instappNumber',
           appDate DATE PATH '$.appDate',
           firstName VARCHAR2(100) PATH '$.contactInfo.firstName', 
           middleName VARCHAR2(50) PATH '$.middleName',
           lastName VARCHAR2(50) PATH '$.lastName',
           emailAddress VARCHAR2(50) PATH '$.emailAddress',
  NESTED PATH  '$.phoneNumbers[*]' COLUMNS (
             phoneNumberId NUMBER(15) PATH '$.phoneNumberId',
             phoneType VARCHAR2(50) PATH '$.phoneType',
             phoneNumber VARCHAR2(50) PATH '$.phoneNumber'),
  NESTED PATH  '$.workHistory[*]' COLUMNS (
            companyName VARCHAR2(50) PATH '$.companyName',
            startDate DATE PATH '$.startDate',
            endDate DATE PATH '$.EndDate'))
  );

| INSTID|立即|INSTAPPNUMBER|批准|名字|MIDDLENAME|联系方式|电子邮件地址|音素编号|电话类型|电话号码|公司名称|开始日期|结束|
| - -----|- -----|- -----|- -----|- -----|- -----|- -----|- -----|- -----|- -----|- -----|- -----|- -----|- -----|
| 一个|一千|Z1009| 2020年9月19日|丹|联系我们|联系我们|联系我们|联系我们|联系我们|联系我们|美国广播公司|2019年1月11日|联系我们|
db<>fiddle demo

相关问题