简体   繁体   English

将字符串转换为 json 对象数组,然后查询 json

[英]Converting a string to an array of json objects and then querying the json

I have the following string:我有以下字符串:

'[{"start":"s1","end":"e1"},{"start":"s2","end":"e2"}]'

I want to be able to execute an SQL query that returns 2 columns as follows:我希望能够执行一个返回 2 列的 SQL 查询,如下所示:

start   end
s1      e1
s2      e2

I'm envisioning something like我正在设想类似的东西

with t as (
    select '[{"start":"s1","end":"e1"},{"start":"s2","end":"e2"}]' as data
)

select ???选择 ??? from t从T

Can someone fill in the ???有人可以填写吗???

If you are using Postgres you can use the ->> operator together with jsonb_array_elements() :如果您使用Postgres,您可以将->>运算符与jsonb_array_elements()一起使用:

with t(data) as (
  values ('[{"start":"s1","end":"e1"},{"start":"s2","end":"e2"}]'::jsonb)
)
select a.item ->> 'start' as "start", 
       a.item ->> 'end' as "end"
from t
  cross join jsonb_array_elements(t.data) as a(item)

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

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