使用regex_replace来提取JSON数组中的所有值并删除分隔符后的值。

huangapple go评论54阅读模式
英文:

Use regex_replace to extract all value in json array and remove value after separate

问题

提取Snowflake中JSON数组中所有ID的方法是将正则表达式调整为提取冒号(:)之前的值。以下是您的查询的修改版本:

SELECT
  player_army_load,
  TRIM(REGEXP_REPLACE(player_army_load, ':.*$', ''), '{|"') AS element_id
FROM x

这将提取冒号(:)之前的所有值,如您所需的198002、198006和320906。

英文:

I want to be able to extract all ids from json array in snowflake before ':'

I have this json format for example: { "198002": 2115960, "198006": 604560, "320906": 302280 }

I have tried something like this:
select
player_army_load
,trim(regexp_replace(player_army_load,'\:.*$',''),'{|"') as element_id
from x

but this extract only first value, but I want 198002,198006,320906

extract all value before ":"

答案1

得分: 2

我看到您需要从JSON中提取键值,您可以使用OBJECT_KEYS函数:

select OBJECT_KEYS(PARSE_JSON(col1))
from jsontable;

结果:

[   "198002",   "198006",   "320906" ]

如果您想将结果作为字符串获取,可以使用ARRAY_TO_STRING函数:

SELECT ARRAY_TO_STRING( OBJECT_KEYS(PARSE_JSON(col1)), ',' )
FROM jsontable;

结果:

198002,198006,320906
英文:

I see you need to extract keys from the json, in this case you can use OBJECT_KEYS:

select OBJECT_KEYS(PARSE_JSON(col1))
from jsontable;

Result :

[   "198002",   "198006",   "320906" ]

And if you want to get your result as a string then use ARRAY_TO_STRING :

SELECT ARRAY_TO_STRING( OBJECT_KEYS(PARSE_JSON(col1)), ',' )
FROM jsontable;

Result :

198002,198006,320906

答案2

得分: 0

这是给你尝试在你的对象中使用for循环的代码部分:

const data = {1:"one", 2:"two", 3:"three", 4:"four"}

for(const i in data){
  // 使用 i 执行你的任务
}
英文:

this is for you try for loop in your object

const data = {1:"one",2:"two", 3:"three", 4:"four"}

for(const i in data){
  **perform your task with i**
}

huangapple
  • 本文由 发表于 2023年5月17日 19:22:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/76271545.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定