簡體   English   中英

嵌套 JSON 使用雪花 SQL 解析

[英]Nested JSON parsing using Snowflake SQL

我在解析 Snowflake 中的某個嵌套 JSON 結構時遇到問題。 這里的問題是某些元素使用了特殊字符,例如 @ 和 #。 這些字符使我無法在嘗試訪問某些元素時使用簡單的點表示法,而無需使用結構的扁平部分上的連接和 where 子句進行真正復雜的查詢。 這是 JSON 文件的示例:

{
  "ContractTerm": [
    {
      "@ID": 123123,
      "CodeTermTypeID": {
        "#text": "some_text 123123"
      },
      "ContractID": {
        "#text": "other_text 123123",
        "@href": "/businessObject/123123",
        "@ID": 123123
      },
      "ContractTermID": 123123
    },
    {
      "@ID": 234234,
      "CodeTermStatusID": {
        "#text": "some_text_again 234234"
      },
      "CodeTermTypeID": {
        "#text": "some_text 234234"
      },
      "ContractID": {
        "#text": "some_other_text 234234",
        "@href": "/businessObject/234234",
        "@lxID": 234234
      },
      "ContractTermID": 234234
    },
    {
      "@lD": 345345,
      "CodeTermTypeID": {
        "#text": "another_text 345345"
      },
      "ContractID": {
        "#text": "another_text 345345",
        "@href": "/businessObject/345345",
        "@lxID": 345345
      },
      "ContractTermID": 345345
    }
  ]
}

是否可以獲取以 @ 和 # 開頭的元素,例如在 SQL 代碼中使用一些轉義字符或類似字符?

在帶有特殊字符的屬性周圍使用引號。 例如:

WITH x as (
SELECT parse_json('{
  "ContractTerm": [
    {
      "@ID": 123123,
      "CodeTermTypeID": {
        "#text": "some_text 123123"
      },
      "ContractID": {
        "#text": "other_text 123123",
        "@href": "/businessObject/123123",
        "@ID": 123123
      },
      "ContractTermID": 123123
    },
    {
      "@ID": 234234,
      "CodeTermStatusID": {
        "#text": "some_text_again 234234"
      },
      "CodeTermTypeID": {
        "#text": "some_text 234234"
      },
      "ContractID": {
        "#text": "some_other_text 234234",
        "@href": "/businessObject/234234",
        "@lxID": 234234
      },
      "ContractTermID": 234234
    },
    {
      "@lD": 345345,
      "CodeTermTypeID": {
        "#text": "another_text 345345"
      },
      "ContractID": {
        "#text": "another_text 345345",
        "@href": "/businessObject/345345",
        "@lxID": 345345
      },
      "ContractTermID": 345345
    }
  ]
}') as var)
SELECT y.value:"@ID"
FROM x,
LATERAL FLATTEN(input=>x.var:ContractTerm) y

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM