简体   繁体   English

嵌套 JSON 使用雪花 SQL 解析

[英]Nested JSON parsing using Snowflake SQL

I have a problem parsing a certain nested JSON structure in Snowflake.我在解析 Snowflake 中的某个嵌套 JSON 结构时遇到问题。 The problem here is the usage of special characters like @ and # for example for some of the elements.这里的问题是某些元素使用了特殊字符,例如 @ 和 #。 Those characters prevents me from using simple dot notation when trying to access some of the elements without resourcing to a really complicated queries utilizing joins and where clauses on the flattened parts of the structure.这些字符使我无法在尝试访问某些元素时使用简单的点表示法,而无需使用结构的扁平部分上的连接和 where 子句进行真正复杂的查询。 Here's an example of how the JSON file looks like:这是 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
    }
  ]
}

Is it possible to get those elements starting with @ and # like using some escape characters or something similar in SQL code?是否可以获取以 @ 和 # 开头的元素,例如在 SQL 代码中使用一些转义字符或类似字符?

Use quotes around the attributes with special characters.在带有特殊字符的属性周围使用引号。 For example:例如:

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