I have the following function in PostgreSQL

CREATE OR REPLACE FUNCTION public.translatejson(JSONB, TEXT)
RETURNS TEXT
AS
$BODY$
   SELECT ($1->$2)::TEXT
$BODY$
LANGUAGE sql STABLE;

When I execute it I receive the values surrounded by double quotes. For example:

SELECT id, translatejson("title", 'en-US') AS "tname" FROM types."FuelTypes";

作为回报,我得到一张这样的桌子

-------------------
| id | tname      |
-------------------
| 1  | "gasoline" |
| 2  | "diesel"   |
-------------------

The values in the 'title' column are in JSON format: { "en-US":"gasoline", "fr-FR":"essence" }. How I can omit the double quotes to return just the string of the result?

推荐答案

The -> operator returns a json result. Casting it to text leaves it in a json reprsentation.

->>运算符返回text结果.用那个来代替.

test=> SELECT '{"car": "going"}'::jsonb -> 'car';
 ?column? 
----------
 "going"
(1 row)

test=> SELECT '{"car": "going"}'::jsonb ->> 'car';
 ?column? 
----------
 going
(1 row)

Json相关问答推荐

Swift解码错误类型与`Bool`type不一致

PowerShell脚本未按预期生成预期的JSON输出

当console.log返回TRUE值时,解析的JSON中的字段未定义

使用Jolt v.0.1.1向每个json元素添加一个键-值对

NiFi QueryRecord处理器- Select 可选的JSON属性

Jolt-在数组列表中插入新的字段

如何在JSONata对象中迭代并向数组添加新字段?

JQ:获取该值的较短语法是什么

在 json 对象中存储多个键:值对

使用动态语言jQuery:根据匹配模式提取与其他值匹配的值

如何强制仅有一个元素的数组在JSON中生成方括号

使用 JQ 获取 JSON 中的替代元素(输出:JSON 对象)

使用 TypeScript 接口时如何修复未定义错误?

jq可以在两个JSON对象列表中依次添加对象吗?

如何为所有 API 端点全局设置 http.ResponseWriter Content-Type 标头?

如何找出实际安装了哪个版本的 bower 包?

在 Rails 3 中处理 JS/ERB 模板中的 JSON

JSON.parse 返回字符串而不是对象

在 Android 中使用带有 post 参数的 HttpClient 和 HttpPost

Peewee 模型转 JSON