Snowflake cloud data platform 雪花:如何读取JSON格式但包装在XML响应中的数据

Snowflake cloud data platform 雪花:如何读取JSON格式但包装在XML响应中的数据,snowflake-cloud-data-platform,Snowflake Cloud Data Platform,我从API获取数据响应,作为XML,其中包含JSON数据 如何使用Snowflake读取JSON数据以加载到表中 <?xml version="1.0" encoding="utf-8"?> <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSch

我从API获取数据响应,作为XML,其中包含JSON数据

如何使用Snowflake读取JSON数据以加载到表中

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <soap:Body>
        <GetFilteredResponse xmlns="Log_EngIP">
            <GetFilteredCustomReportResult>
                <DtoObject>
                    <ReportData>[{"ID":1,"Name":"Default","OwnerID":1,"SortOrder":null,"Terms":"+0"},{"ID":3,"Name":"VIP","OwnerID":1,"SortOrder":null,"Terms":"+30"},{"ID":9,"Name":"Telesphere (EmailAndStatus)","OwnerID":1,"SortOrder":0,"Terms":"+0"},{"ID":10,"Name":"Default","OwnerID":693,"SortOrder":null,"Terms":"+30"}]</ReportData>
                    <NumberOfPages>1</NumberOfPages>
                </DtoObject>
                <Response>Successful</Response>
                <Successful>true</Successful>
            </GetFilteredCustomReportResult>
        </GetFilteredResponse>
    </soap:Body>
</soap:Envelope>
您可以/XML,使用JSON字符串提取ReportData元素值,重新解析JSON,然后使用查询其字段

下面是一个简单的示例,假设提供的示例是位置性的:

以tbl为例 -带有1行XML解析变量数据的示例“表” 选择parse_xml'[{ID:1,Name:Default,OwnerID:1,SortOrder:null,Terms:+0},{ID:3,Name:VIP,OwnerID:1,SortOrder:null,Terms:+30},{ID:9,Name:Telesphere-EmailAndStatus,OwnerID:1,SortOrder:0,Terms:+0},{ID:10,Name:Default,OwnerID:693,SortOrder:null,Terms:+30}]1succfulTrue'xmlv ,第一个报告数据json为 -从XML中将JSON提取为字符串值,然后将其作为自己的变量重新分类 -这使用了遍历的符号样式,但也可以使用XMLGET/GET 从tbl中选择parse_jsonxmlv:$.$.$.$[0].$[0].$jsonv -从上面形成的JSON数组变量生成行 选择e.value:ID,e.value:Name from first_report_data_json,first_report_data_json.jsonv e; 这将产生:

+------------+-------------------------------+                                  
| E.VALUE:ID | E.VALUE:NAME                  |
|------------+-------------------------------|
| 1          | "Default"                     |
| 3          | "VIP"                         |
| 9          | "Telesphere (EmailAndStatus)" |
| 10         | "Default"                     |
+------------+-------------------------------+
注意:对XML节点GetFilteredCustomReportResult的访问和进一步所需的数组样式访问$[0]可能是因为定义它的xmlns链接中的模式可读,即它实际上是一个元素数组,不一定是单个的


在实际情况中,完整地说,您将使用“展平”将它们拆分为自己的整行,以进行独立处理。我在这里省略了这一点,只是针对所提供的示例中存在的单个元素。

您能否向我解释一下,当XML文件是一行时,您为什么给了$[0]它工作正常,但如果XML文件看起来像每种标记格式的多行,它会给出错误作为错误解析XML:不是XML元素文件