Json值可以包含字符串值.例如.:
postgres=# SELECT to_json('Some "text"'::TEXT);
to_json
-----------------
"Some \"text\""
Run Code Online (Sandbox Code Playgroud)
如何将该字符串提取为postgres文本值?
::TEXT
不起作用.它返回引用的json,而不是原始字符串:
postgres=# SELECT to_json('Some "text"'::TEXT)::TEXT;
to_json
-----------------
"Some \"text\""
Run Code Online (Sandbox Code Playgroud)
谢谢.
PS我正在使用PostgreSQL 9.3
我正在尝试以下查询:
SELECT (json_data->'position'->'lat') + 1.0 AS lat FROM updates LIMIT 5;
Run Code Online (Sandbox Code Playgroud)
(+ 1.0只是强制转换为浮点数.我的实际查询要复杂得多,这个查询只是问题的一个测试用例.)
我收到错误:
ERROR: operator does not exist: jsonb + numeric
Run Code Online (Sandbox Code Playgroud)
如果我添加显式转换:
SELECT (json_data->'position'->'lat')::float + 1.0 AS lat FROM updates LIMIT 5;
Run Code Online (Sandbox Code Playgroud)
错误变成:
ERROR: operator does not exist: jsonb + double precesion
Run Code Online (Sandbox Code Playgroud)
我知道大多数jsonb值都不能转换成浮点数,但在这种情况下我知道lats都是JSON数.
是否有一个函数将jsonb值转换为浮点数(或为uncastable返回NULL)?
在下面的查询中,$ isComplete和$ isValid作为字符串返回.但是,它们保存为布尔值.如何获取要返回的这些字段的布尔表示?
query =
"SELECT
data #>> '{id}' AS id,
data #>> '{name}' AS name,
data #>> '{curator}' AS curator,
data #> '{$isValid}' as \"$isValid\",
data #> '{customer}' as customer,
data #> '{$createdTS}' as \"$createdTS\",
data #> '{$updatedTS}' as \"$updatedTS\",
data #> '{$isComplete}' as \"$isComplete\",
(count(keys))::numeric as \"numProducts\"
FROM
appointment_intakes,
LATERAL jsonb_object_keys(data #> '{products}') keys
GROUP BY id"
Run Code Online (Sandbox Code Playgroud) 似乎BIGINT
是MySQL上可用的最大整数,对吧?
你需要存储BIGINT(80)时该怎么办?
为什么在某些情况下,比如Twitter API文档中的某个地方,他们建议我们将这些大整数存储为varchar
?
选择使用一种类型而不是另一种类型背后的真正原因是什么?
我觉得我必须在这里错过一些简单的东西,但是我已经查看了PostgreSQL关于JSON和JSON运算符和函数的文档,并没有看到任何解释它的东西.
在PostgreSQL中很容易将事物变成JSON:
SELECT *, pg_typeof(j) FROM (VALUES
(to_json(5)),
(to_json(true)),
(to_json('foo'::TEXT))
) x (j);
Run Code Online (Sandbox Code Playgroud)
会给我一个很好的json
s 结果集:
j | pg_typeof
-------+-----------
5 | json
true | json
"foo" | json
Run Code Online (Sandbox Code Playgroud)
但是,如何将这些json
值转换回原始类型?当然,我不希望能够在一个结果集中完成所有操作,因为类型不一致.我只是个人意思.
铸造肯定不起作用:
SELECT to_json(5)::NUMERIC;
Run Code Online (Sandbox Code Playgroud)
给
ERROR: cannot cast type json to numeric
Run Code Online (Sandbox Code Playgroud)
如果我试图滥用这样的json_populate_record
功能:
SELECT json_populate_record(null::INTEGER, to_json(5));
Run Code Online (Sandbox Code Playgroud)
我明白了
ERROR: first argument of json_populate_record must be a row type
Run Code Online (Sandbox Code Playgroud)
在PG 9.4中,我可以很容易地告诉类型:SELECT json_typeof(to_json(5));
give number
,但这并没有帮助我实际提取它.
也不json_to_record
(也是9.4):
SELECT * FROM json_to_record(to_json(5)) x (i …
Run Code Online (Sandbox Code Playgroud) 我需要以某种方式从 json 转换为 int。我已经做了这个功能:
CREATE OR REPLACE FUNCTION json2int(p_json json)
RETURNS integer AS
$BODY$DECLARE
v_json json;
--v_char character varying;
v_int integer;
BEGIN
SELECT p_json->'additionalData'->'id' INTO v_json;
--SELECT CAST(v_json as character varying) INTO v_char;
SELECT CAST(v_json as integer) INTO v_int;
RETURN v_int;
END;$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;
ALTER FUNCTION json2int(json)
OWNER TO postgres;
Run Code Online (Sandbox Code Playgroud)
我尝试从 json 转换为 int,但没有用。然后我尝试从 json 转换为字符到 int,这也不起作用。所以我补充说:
CREATE CAST (json AS integer) WITH INOUT as implicit;
Run Code Online (Sandbox Code Playgroud)
现在当我运行我的函数时:
SELECT json2int('{"additionalData":{"id":"4","userType":"viewer"},"type":"wall"}');
Run Code Online (Sandbox Code Playgroud)
我收到此错误:
ERROR: invalid input syntax for integer: ""4"" …
Run Code Online (Sandbox Code Playgroud)