如何在Postgresql中从类似json的结构中选择一个参数?

时间:2017-05-23 15:51:04

标签: json postgresql

我的数据库有一个包含类似json的数据结构的列(但没有实际的json标记)它看起来像这样:

---
ID: "-2293428132623007080"
RES_ID: '0'
NUMBER: "ДЕП002М Москва-Черновцы"
TRIP_SEATS_MAP: 'true'
SHOW_PICKUP_DROPOFF: 'false'
HAS_TRANSFER: 'false'
PLACES_COUNT: '49'...

我需要从中选择HAS_TRANSFER值。参数被分隔为行,没有分隔符。有没有办法选择单独的参数值?

1 个答案:

答案 0 :(得分:0)

一种方法是在HAS_TRANSFER:和换行\r

之间提取数据
with t(col) as(
    select 'ID: "-2293428132623007080"
    RES_ID: ''0''
    NUMBER: "ДЕП002М Москва-Черновцы"
    TRIP_SEATS_MAP: ''true''
    SHOW_PICKUP_DROPOFF: ''false''
    HAS_TRANSFER:     ''false''
    PLACES_COUNT: ''49'''::text
    union all
    select 'ID: "-2293428132623007080"
    RES_ID: ''0''
    NUMBER: "ДЕП002М Москва-Черновцы"
    TRIP_SEATS_MAP: ''true''
    SHOW_PICKUP_DROPOFF: ''false''
    HAS_TRANSFER:''bla''
    PLACES_COUNT: ''49'''::text
)

-- below is actual query:
select  substring(col from  'HAS_TRANSFER:\s*([^\r]+)')  from t