regex - 在 PostgreSQL 中使用子字符串获取 JSON 值

标签 regex json postgresql substring

我有一个带有以下 json 的表字段“数据”:

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

而且我只想获取 amount 的 json 值的值,因此在 PostgreSQL 中的上述情况下为 34.00。

到目前为止我有:

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

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

如有任何帮助,我们将不胜感激。

最佳答案

尝试:

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

关于regex - 在 PostgreSQL 中使用子字符串获取 JSON 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20952154/

相关文章:

php - 删除域扩展

python - DataFrame 列比较

javascript - Jquery:访问多维 JSON 对象的值并在表单字段中分配

sql - Postgresql 选择一系列由给定间隔隔开的记录

javascript - 使用正则表达式捕获负数

javascript - 在 AngularJS ng-repeat 中处理 json 数据

python - Scrapy 提取ld+JSON

sql - 将用户权限限制为仅对一个特定数据库

PostgreSQL 查询 : Column Sum for the latest available date of each month

regex - 在 Hive 中使用 regexp_extract