我有一个jsonb字段(让我们称之为" info")具有这样的价值
{"a": "foo", "c": "{ \"d\": \"dog\"}", \"b\": \"bark\"}
使用Postgres如何删除反斜杠? 我尝试使用
REPLACE(info::text, '\', '')
或者这个 REGEXP_REPLACE(info :: text,' \','')
其中任何一个都有效。
请有人帮忙..
答案 0 :(得分:1)
查询以降低jsonb:
WITH RECURSIVE z3 AS (
WITH z2 AS (
WITH RECURSIVE z AS (
WITH x AS (
-- just your variable
SELECT '{"a":"foo", "b":"BARK", "c" :{ "d": "dog"}}'::jsonb AS j
) SELECT j,
ARRAY [ a ] AS a
FROM (
SELECT j,
jsonb_object_keys(j) AS a
FROM x
) AS a
UNION ALL SELECT j,
a || b
FROM (
SELECT j,
a,
jsonb_object_keys(j#>(a)) AS b
FROM z
WHERE j#>>(a) ~ '^\{.*\}$'
) AS A
) SELECT *,
dense_rank() OVER (ORDER BY a) AS i
FROM z
WHERE NOT j#>>(a) ~ '^\{.*\}$'
) SELECT i,
jsonb_set(j, a, to_jsonb(lower(j#>>(a)))) AS j
FROM (
SELECT * FROM z2 ORDER BY i LIMIT 1
) AS a
UNION ALL SELECT z2.i,
jsonb_set(z3.j,
z2.a,
to_jsonb(lower(z3.j#>>(z2.a))))
FROM z3
JOIN z2 ON (z2.i = z3.i + 1)
) SELECT j FROM z3 ORDER BY i DESC LIMIT 1;
一些解释: z CTE查找jsonb对象中的所有字符串。 '一个'是json字符串的路径。输出:
j | a | dense_rank
----------------------------------------------+-------+------------
{"a": "foo", "b": "BARK", "c": {"d": "dog"}} | {a} | 1
{"a": "foo", "b": "BARK", "c": {"d": "dog"}} | {b} | 2
{"a": "foo", "b": "BARK", "c": {"d": "dog"}} | {c,d} | 3
(3 rows)
z3和dense_rank用于每个选择一个(递归调用)应用低于json。