JSON 提取返回 NULL 值

JSON Extract is returning NULL values

我的数据集样本如下所示:

WITH raw_string AS (
    SELECT CAST
    (
        '{
        "1261369621021236":["It was interactive and the guy was v nice"]
        ,"4806456876149982":["Mediahub"]
        ,"501699158168566":["Yes"]
        ,"293482916246997":["Do it IRL"]
        ,"1748555938809400":["Europe, Middle East, or Africa (EMEA)"]
        ,"3019471278301443":["Storytelling for Impact"]
        ,"1051072995806230":["Yes"],"687313266038215":["28042022"]
        ,"406048341357670":["Somewhat agree"]
        ,"304353711772145":["Very satisfied"]
        }'
        AS JSON
    ) AS answers_text
)

SELECT *,
JSON_EXTRACT_SCALAR(answers_text, '$["1261369621021236"]') AS q1 FROM raw_string

为什么返回 null?我试图获得一个值,在这种情况下应该是:“它是互动的,那个人非常好”

因为answer_text不是一个数组,只是作为一个带有属性的对象,去掉括号试试看:

SELECT *,
JSON_EXTRACT_SCALAR(answers_text, '$.1261369621021236') AS q1 FROM raw_string

看下面2个样本我就得出了上面的结论

Source 1

SELECT JSON_EXTRACT_SCALAR(JSON '{ "name" : "Jakob", "age" : "6" }', '$.age') AS scalar_age;

Source 2

SELECT json_extract_scalar('[1, 2, 3]', '$[2]');

我无法 运行 对付 presto。我唯一担心的是属性名称是否会与 big integer.I 混淆,还会质疑为什么在值周围加上括号。正常的应该没必要 json

来自 json_extract_scalar 文档:

The value referenced by json_path must be a scalar (boolean, number or string):

但是您有一个 json 数组,它不是标量值,因此您需要提供标量值的路径 - '$.1261369621021236[0]':

-- data
WITH raw_string AS (
    SELECT json '{"1261369621021236":["It was interactive and the guy was v nice"],
    "4806456876149982":["Mediahub"]}' AS answers_text
)

-- query
SELECT JSON_EXTRACT_SCALAR(answers_text, '$.1261369621021236[0]') AS q1 
FROM raw_string

输出:

q1
It was interactive and the guy was v nice

或使用JSON_EXTRACT:

-- query
SELECT JSON_EXTRACT(answers_text, '$.1261369621021236') AS q1 
FROM raw_string

输出:

q1
["It was interactive and the guy was v nice"]

还有一些选项使用转换为不同的类型。例如,提供的 json 可以转换为 map(varchar, array(varchar)) 并且可以通过键和数组索引提取值(请注意,在 presto 中从 1 开始):

SELECT cast(answers_text as map(varchar, array(varchar)))['1261369621021236'][1] AS q1 
FROM raw_string