我有一个带有以下 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\.]*)')
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/