简体   繁体   English

从JSON postgres数组中获取元素

[英]Get element from array of JSON postgres

I have a column x with data type jsonb 我有一个数据类型为jsonb的列x

and the value looks like: 值看起来像:

[  
    [{"string":"whateverstring1"}],
    [{"string":"whateverstring2"}]
]

How to return each element of the array? 如何返回数组的每个元素?

Something like this: " whateverstring1 "," whateverstring2 " 像这样的东西:“ whatthingstring1 ”,“ whatthingstring2

demo:db<>fiddle 演示:分贝<>小提琴

SELECT jsonb_array_elements(jsonb) -> 0 -> 'string' 
FROM (
    SELECT '[[{"string":"whateverstring1"}],[{"string":"whateverstring2"}]]'::jsonb
) s
  1. jsonb_array_elements extract each element into one row jsonb_array_elements每个元素提取到一行
  2. -> 0 gives the first element of the nested arrays which is {"string":"whateverstring1"} -> 0给出嵌套数组的第一个元素,即{"string":"whateverstring1"}
  3. -> 'string' gives the value of the elements -> 'string'给出元素的值

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

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