简体   繁体   English

解析雪花中的json键值,键中有一个点

[英]parsing json key-value in snowflake with key having a dots in it

{
  "deviceLocale": "en_US",
  "deviceSerialNumber": "xxxxxxxxxx",
  "eventSource": "abc",
  "ext.user.browser": "Mobile Safari",
  "ext.user.browser.version": "1.0.4",
  "ext.user.device.family": "iPhone", 
  "ext.user.os": "iOS",
  "ext.user.os.version": "1.3.0",
  "Timestamp": 158007896874 }

This is the sample json that I have..这是我拥有的示例 json。

Parsing in snowflake在雪花中解析

```
select distinct
eve_id,
json_payload:ext.useragent.device.family::varchar as type,
json_payload:ext.useragent.os::varchar as osname,
json_payload:ext.useragent.os.version::varchar as os 
from XYZ table, lateral flatten (input => json_payload)
```

But all these three fields are giving NULL values and I see the data in the json format.但是这三个字段都给出了 NULL 值,我看到了 json 格式的数据。 So I guess parsing is not right.所以我猜解析是不对的。 I know in snowflake while parsing if we use a dot or : then it refers to the nested key.我在解析雪花时知道我们是否使用点或 : 那么它指的是嵌套键。 But in my case I have a simple json with no nested keys.但就我而言,我有一个没有嵌套键的简单 json。

Any idea?任何的想法?

firstly you can put the name in double quotes, like so:首先,您可以将名称放在双引号中,如下所示:

SELECT parse_json('{
        "deviceLocale": "en_US",
        "deviceSerialNumber": "xxxxxxxxxx",
        "eventSource": "abc",
        "ext.user.browser": "Mobile Safari",
        "ext.user.browser.version": "1.0.4",
        "ext.user.device.family": "iPhone", 
        "ext.user.os": "iOS",
        "ext.user.os.version": "1.3.0",
        "Timestamp": 158007896874 }') AS json_payload,
    json_payload:"ext.user.device.family"::varchar as type,
    json_payload:"ext.user.os"::varchar as osname,
    json_payload:"ext.user.os.version"::varchar as os;

gives:给出:

JSON_PAYLOAD    TYPE    OSNAME  OS
{    "Timestamp": 158007896874,    "deviceLocale": "en_US",    "deviceSerialNumber": "xxxxxxxxxx",    "eventSource": "abc",    "ext.user.browser": "Mobile Safari",    "ext.user.browser.version": "1.0.4",    "ext.user.device.family": "iPhone",    "ext.user.os": "iOS",    "ext.user.os.version": "1.3.0"  }    iPhone  iOS 1.3.0

OR you can use the [''] format like json_payload['ext.user.os.version']::varchar as os which allow the avoidance of double quotes (in case you want to avoid that).或者您可以使用['']格式,如json_payload['ext.user.os.version']::varchar as os允许避免双引号(如果您想避免这种情况)。

In you access SQL you have json_payload:ext.useragent.device.family::varchar but the useragent section is only user in your JSON.在您访问 SQL 时,您有json_payload:ext.useragent.device.family::varcharuseragent部分只是您 JSON 中的user So that will give you trouble.所以会给你带来麻烦。

Also in your example you are using a LATERAL FLATTEN, but asking how to access the flattened members of the object you are flattening.同样在您的示例中,您使用的是 LATERAL FLATTEN,但询问如何访问您正在展平的对象的展平成员。 So the flatten is not needed.所以不需要扁平化。 But if you are want to flatting then you will get a row per top level item, at which point you would be want to filter the key .. but I suspect this is not what your trying to do.但是,如果您想要扁平化,那么您将在每个顶级项目中获得一行,此时您可能想要过滤key .. 但我怀疑这不是您想要做的。 But if you are is a good idea to alias the flatten to help show the intention.但是,如果您是一个好主意,则将展平别名化以帮助显示意图。

WITH jp AS (
    SELECT parse_json('{
      "deviceLocale": "en_US",
      "deviceSerialNumber": "xxxxxxxxxx",
      "eventSource": "abc",
      "ext.user.browser": "Mobile Safari",
      "ext.user.browser.version": "1.0.4",
      "ext.user.device.family": "iPhone", 
      "ext.user.os": "iOS",
      "ext.user.os.version": "1.3.0",
      "Timestamp": 158007896874 }') AS json_payload
)
SELECT 
    f.key,
    f.path,
    f.value
FROM jp, LATERAL FLATTEN (input => json_payload) f;

gives:给出:

KEY PATH    VALUE
Timestamp   Timestamp   158007896874
deviceLocale    deviceLocale    "en_US"
deviceSerialNumber  deviceSerialNumber  "xxxxxxxxxx"
eventSource eventSource "abc"
ext.user.browser    ['ext.user.browser']    "Mobile Safari"
ext.user.browser.version    ['ext.user.browser.version']    "1.0.4"
ext.user.device.family  ['ext.user.device.family']  "iPhone"
ext.user.os ['ext.user.os'] "iOS"
ext.user.os.version ['ext.user.os.version'] "1.3.0"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM