解析存储在`text`列中的json

问题描述 投票:0回答:1

我在 Stack Overflow 上看到了很多关于解析 json 的问题,但没有一个能完全让我到达我需要的地方。

我有一张桌子,例如

 _____________________________________
|                                     |
| PreferenceId::varchar | Value::text |
 _____________________________________
|                                                                                  |
| 1   | [{"username":"test","customerId":"504116aa-bf95-4736-8322-917e5055681d"}]  |

我已经尝试过

jsonb_array_elements
json_to_recordset
但当我尝试解析和查询这些数据时,我不断地在 postgres 中遇到错误。通常会出现诸如
function jsonb_array_elements doesn't exist
等错误。当我尝试使用该函数进行静态测试查询时,它工作正常。

select *
from json_to_recordset('[{"operation":"U","taxCode":1000},{"operation":"U","taxCode":10001}]')
as x("operation" text, "taxCode" int);

select v."Value"::json->>'username' --- username comes back as null
json postgresql
1个回答
1
投票

以下是如何使用

json_array_elements
:

select j->>'username', j->>'customerId'
from mytable, json_array_elements(Value::json) as j
© www.soinside.com 2019 - 2024. All rights reserved.