Oracle中JSON数组转关系型数据:查询无法获取数组值的问题
JSON数组转关系型数据查询错误修正
你的查询问题出在json_table的列路径定义上:
当你使用'$.items[*]'作为json_table的遍历路径时,已经将上下文定位到了items数组的单个元素上(比如每次迭代对应"item1"、"item2"这类字符串节点)。这时候你在columns里写path '$.items',相当于试图从当前的单个字符串节点中查找名为items的属性,显然找不到对应值。
修正后的查询
select items from test_table stg, json_table(stg.json_data, '$.items[*]' columns ( items path '$' ) );
更严谨的写法(指定数据类型)
如果需要明确字段类型,可以在列定义里加上类型声明:
select items from test_table stg, json_table(stg.json_data, '$.items[*]' columns ( items varchar(100) path '$' ) );
原理很简单:$在当前上下文里就代表数组的单个元素本身,直接用它就能取出数组里的每个字符串值。
内容的提问来源于stack exchange,提问作者samg
相关产品推荐
相关产品推荐

