【问题标题】:How to filter a value in json field type on Postgres 9.2?如何在 Postgres 9.2 上过滤 json 字段类型中的值?
【发布时间】:2019-06-21 01:59:33
【问题描述】:

我的json字段数据是这样的:

{"active":true,"id":"xxxxxxxxxxxxxxxxx","settings":{"secret":"xxxxxxxxxxxxxxxxxxxxxxxxxx","token":"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx","expires":"2019-12-16 01:11:23"},"plan":"Sample"}

然后我尝试像这样查询该字段:

select * from integrations.accounts where field -> 'id' = 'xxxxxx';

但它给了我一个错误:

SQL 错误 [42883]:错误:运算符不存在:json -> 未知

发现9.2版本不支持箭头操作符(->):

不支持的版本:9.3 / 9.2

有没有其他方法可以做到这一点?

【问题讨论】:

  • 9.2 不再维护,所以无论如何你都应该升级。

标签: postgresql


【解决方案1】:

由于您的版本没有真正支持 JSON(强烈建议升级!)您必须以任何方式手动解析 JSON 文本。例如在正则表达式的帮助下:

demo:db<>fiddle

SELECT 
    (regexp_matches(my_json_value::text, '"id":"(.*?)"'))[1]

【讨论】:

  • @Juan 这个答案对你有帮助吗?如果没有,我该如何帮助?如果是这样,请不要忘记接受(关闭)并为有用的答案投票。 :)
猜你喜欢
  • 2022-01-09
  • 2012-09-06
  • 2017-11-24
  • 2017-12-06
  • 1970-01-01
  • 2015-06-01
  • 1970-01-01
  • 2013-07-22
  • 2012-09-06
相关资源
最近更新 更多