JSON_EXTRACT return 空值
JSON_EXTRACT return null value
我想在 BigQuery 中查询以下 JSON 字符串:
'{"_id":"xxxxx","description":"stuff","userId":"1234}, {"_id":"xxxxx","description":"stuff","userId":"1234}'
我正在尝试 运行 以下查询,但这不起作用:
WITH test AS (
SELECT '{"_id":"xxxxx","description":"stuff","userId":"1234}' raw_json UNION ALL
SELECT '{"_id":"xxxxx","description":"stuff","userId":"1234}'
)
select JSON_EXTRACT('raw_json', '$.userId') AS json_extract,
from test
为什么?
最终我想查询一个 JSON 字符串,该字符串作为字符串存储在云存储中。
在您的示例中存在一些引用问题:
- 查询中 raw_json 周围的单引号使 BQ 认为您正在查询字符串 'raw_json' 的 'userId' 字段。这个字符串没有这样的字段(它甚至没有正确格式化 json,我很惊讶没有引发错误),所以结果为空。
以下作品:
WITH test AS (
SELECT '{"_id":"xxxxx","description":"stuff","userId":"1234"}' raw_json
UNION ALL
SELECT '{"_id":"xxxxx","description":"stuff","userId":"1234"}'
)
select JSON_EXTRACT(raw_json, '$.userId') AS json_extract,
from test
请注意,显然 recommended way 是使用 JSON_VALUE
:
WITH test AS (
SELECT '{"_id":"xxxxx","description":"stuff","userId":"1234"}' raw_json
UNION ALL
SELECT '{"_id":"xxxxx","description":"stuff","userId":"1234"}'
)
select JSON_VALUE(raw_json, '$.userId') AS json_extract,
from test
我想在 BigQuery 中查询以下 JSON 字符串:
'{"_id":"xxxxx","description":"stuff","userId":"1234}, {"_id":"xxxxx","description":"stuff","userId":"1234}'
我正在尝试 运行 以下查询,但这不起作用:
WITH test AS (
SELECT '{"_id":"xxxxx","description":"stuff","userId":"1234}' raw_json UNION ALL
SELECT '{"_id":"xxxxx","description":"stuff","userId":"1234}'
)
select JSON_EXTRACT('raw_json', '$.userId') AS json_extract,
from test
为什么?
最终我想查询一个 JSON 字符串,该字符串作为字符串存储在云存储中。
在您的示例中存在一些引用问题:
- 查询中 raw_json 周围的单引号使 BQ 认为您正在查询字符串 'raw_json' 的 'userId' 字段。这个字符串没有这样的字段(它甚至没有正确格式化 json,我很惊讶没有引发错误),所以结果为空。
以下作品:
WITH test AS (
SELECT '{"_id":"xxxxx","description":"stuff","userId":"1234"}' raw_json
UNION ALL
SELECT '{"_id":"xxxxx","description":"stuff","userId":"1234"}'
)
select JSON_EXTRACT(raw_json, '$.userId') AS json_extract,
from test
请注意,显然 recommended way 是使用 JSON_VALUE
:
WITH test AS (
SELECT '{"_id":"xxxxx","description":"stuff","userId":"1234"}' raw_json
UNION ALL
SELECT '{"_id":"xxxxx","description":"stuff","userId":"1234"}'
)
select JSON_VALUE(raw_json, '$.userId') AS json_extract,
from test