使用substring在PostgreSQL中获取JSON值

时间:2014-01-06 14:30:24

标签: regex json postgresql substring

我有一个带有以下json的表格字段“data”:

[{"reason": "Other", "bla": 12345, "amount": "34.00", "moredata": [23, 22], "date": "2014-01-02T01:55:28.646364+00:00", "message": "Bla", "applied_to": "client"}]

而且我只想获得金额的json值的值,所以在PostgreSQL中的情况下为34.00。

到目前为止,我有:

substring(data from '%#"_amount_: ___.___#"%' for '#'),

可悲的是,这给了我“金额”:“34.00”,而不仅仅是34.00。如果金额值为9.00或102.00,它也不起作用。

任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:5)

尝试:

substring(data from '(?:"amount": ")([\d\.]*)')

看到它正常工作herehere它的作用:

NODE                     EXPLANATION
--------------------------------------------------------------------
  (?:                      group, but do not capture:
--------------------------------------------------------------------
    "amount": "              '"amount": "'
--------------------------------------------------------------------
  )                        end of grouping
--------------------------------------------------------------------
  (                        group and capture to \1:
--------------------------------------------------------------------
    [\d\.]*                  any character of: digits (0-9), '\.' (0
                             or more times (matching the most amount
                             possible))
--------------------------------------------------------------------
  )                        end of \1