我在PostgreSQL中有以下功能
CREATE OR REPLACE FUNCTION public.translatejson(JSONB, TEXT)
RETURNS TEXT
AS
$BODY$
SELECT ($1->$2)::TEXT
$BODY$
LANGUAGE sql STABLE;
当我执行它时,我收到双引号括起来的值.例如:
SELECT id, translatejson("title", 'en-US') AS "tname" FROM types."FuelTypes";
作为回报,我得到一张这样的桌子
-------------------
| id | tname |
-------------------
| 1 | "gasoline" |
| 2 | "diesel" |
-------------------
"标题"列中的值采用JSON格式:{"en-US":"汽油","fr-FR":"精华"}.我如何省略双引号只返回结果的字符串?
Cra*_*ger 78
该->
运算符返回一个json
结果.将它投射到text
json reprsentation中.
该->>
运算符返回一个text
结果.改用它.
test=> SELECT '{"car": "going"}'::jsonb -> 'car';
?column?
----------
"going"
(1 row)
test=> SELECT '{"car": "going"}'::jsonb ->> 'car';
?column?
----------
going
(1 row)