如何将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"}