如何在表中的 varchar 列中解析此 JSON?雪花
Posted
技术标签:
【中文标题】如何在表中的 varchar 列中解析此 JSON?雪花【英文标题】:How do I parse this JSON in a varchar column within my table? Snowflake 【发布时间】:2020-12-18 22:49:05 【问题描述】:我有一个表,其中有一列名为“message_json”,在该表中,我将此 json 存储为 varchar 数据类型。
"request_id": "b53e7cc3-89b1-495b-aab0-e0dd6243b32e",
"quote_id": "7a760b81-2c9c-4f20-9453-f7b72d4e06c6",
"tenant_abbreviation": "ahs",
"tenant_id": "ee312e77-8463-44bd-ad7e-2cd4e75c9e3d",
"event_detail":
"source": "Quote service",
"event_name": "quote_created",
"timestamp":
"seconds": 1608236418,
"nanos": 290575000
,
"id": "7a760b81-2c9c-4f20-9453-f7b72d4e06c6"
,
"quote":
"attribute":
"contract.renewal": "false",
"contract.yearsOfService": "0",
"description": "xx 3X3 xx $xx DC SC",
"mktgSourceKey": "5fdc118555b95efff7d29f23",
"order.method": "eCom",
"originalSalesChannel": "",
"plan.id": "xxx",
"product.familyName": "combo",
"product.name": "xx xx COMBO $x DC SC",
"product.origin": "TX3C217D",
"property.address1": "xxx xx xx",
"property.address2": "",
"property.ageOfHome": "",
"property.city": "xxx",
"property.country": "USA",
"property.dwellingType": "1",
"property.dwellingTypeCode": "SINGLE FAMILY RESIDENCE",
"property.motherInLaw": "",
"property.sizeOfHome": "4900",
"property.state": "xxxx",
"property.unitType": "",
"property.unitValue": "",
"property.zip5": "xxxxx",
"property.zip9": "xxxxxx",
"salesChannel": "DC",
"serviceFee": "xxxx"
我正在尝试创建一个新表,将每个键:值对分配给一列。 我试过 parse_json(message_json) 并且由于某种原因它返回的只是这个。
"event_detail":
"event_name": "xxxxxx",
"id": "77e49765-2b53-4d79-9442-8156b0bde3bc",
"source": "xxx xxx",
"timestamp":
"nanos": 830472300,
"seconds": 1572679265
,
"quote_id": "77e49765-2b53-4d79-9442-8156b0bde3bc",
"request_id": "d8ad7a0a-f390-4660-8dc4-2838853d3846",
"tenant_abbreviation": "xxx",
"tenant_id": "ee312e77-8463-44bd-ad7e-2cd4e75c9e3d"
我也尝试过 message_json:request_id 并收到此错误 函数“GET”的参数类型无效:(VARCHAR(16777216), VARCHAR(10))
感谢任何帮助
【问题讨论】:
【参考方案1】:我不知道你的情况有什么问题,但我可以通过添加右括号来修复问题中的 json 字符串:
create or replace temp table texts as
select '
"request_id": "b53e7cc3-89b1-495b-aab0-e0dd6243b32e",
"quote_id": "7a760b81-2c9c-4f20-9453-f7b72d4e06c6",
"tenant_abbreviation": "ahs",
"tenant_id": "ee312e77-8463-44bd-ad7e-2cd4e75c9e3d",
"event_detail":
"source": "Quote service",
"event_name": "quote_created",
"timestamp":
"seconds": 1608236418,
"nanos": 290575000
,
"id": "7a760b81-2c9c-4f20-9453-f7b72d4e06c6"
,
"quote":
"attribute":
"contract.renewal": "false",
"contract.yearsOfService": "0",
"description": "xx 3X3 xx $xx DC SC",
"mktgSourceKey": "5fdc118555b95efff7d29f23",
"order.method": "eCom",
"originalSalesChannel": "",
"plan.id": "xxx",
"product.familyName": "combo",
"product.name": "xx xx COMBO $x DC SC",
"product.origin": "TX3C217D",
"property.address1": "xxx xx xx",
"property.address2": "",
"property.ageOfHome": "",
"property.city": "xxx",
"property.country": "USA",
"property.dwellingType": "1",
"property.dwellingTypeCode": "SINGLE FAMILY RESIDENCE",
"property.motherInLaw": "",
"property.sizeOfHome": "4900",
"property.state": "xxxx",
"property.unitType": "",
"property.unitValue": "",
"property.zip5": "xxxxx",
"property.zip9": "xxxxxx",
"salesChannel": "DC",
"serviceFee": "xxxx"
' input;
select parse_json(input):request_id
from texts
-- "b53e7cc3-89b1-495b-aab0-e0dd6243b32e"
它甚至带着所谓的“缺失”部分回来了:
select parse_json(input):quote.attribute['property.address1']
from texts
-- "xxx xx xx"
【讨论】:
谢谢!这为我完成工作奠定了基础。你怎么知道使用我试过查看文档的语法。我没有看到“quote.attribute['property.address1']”语法。以上是关于如何在表中的 varchar 列中解析此 JSON?雪花的主要内容,如果未能解决你的问题,请参考以下文章
PL/SQL:如何运行存储在表列中的sql语句来获取sql语句的执行时间?