如何将Postgres JSON转换为整数 [英] How to convert postgres json to integer

查看:579
本文介绍了如何将Postgres JSON转换为整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以使用 to_json(1)将int转换为json,但是如何将json转换为int?这可能太慢了:

I can use to_json(1) to cast int to json, but how can I convert json to int? This may be too slow:

to_json(1)::text::int

此外,json是从二进制块(bson)还是简单的文本包装器包装的?

Also, is json wrapped from a binary block (bson) or a simple wrapper of text?

推荐答案

对我有用的(使用posgtgresql 5.6)是

What works for me (using posgtgresql 5.6) is

SELECT (tablename.jsoncolumnname->>'jsonfiledname')::int FROM tablename;

like

SELECT (users.data->>'failed_login_attempts_count')::int FROM users;

假设 users 表有一个名为json的列数据类似于:

Assuming users table has a json column named data which is something like:

{"failed_login_attempts_count":"2","comment":"VIP"}

这篇关于如何将Postgres JSON转换为整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆