我在 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_array_elements
:
select j->>'username', j->>'customerId'
from mytable, json_array_elements(Value::json) as j