gpt4 book ai didi

postgresql - 将 Bytea 转换为 Json

转载 作者:行者123 更新时间:2023-11-29 12:39:50 24 4
gpt4 key购买 nike

我正在尝试分析 Postgresql 数据库中的数据,该数据库是 Json 文本但存储在 bytea 列中。一些行可以转换,但其他行不能。文本中没有代码点的 Json 在转换编码的 bytea 时工作正常

select encode(myByteaColumn, 'escape')::json -> aJsonProperty as myProp from myTable

但对于某些行,这会失败,因为字符串中有一些编码代码点,如德语变音符号(Ä、Ö 等),Zuständigkeit 这样的德语单词在像这样编码 bytea 时显示为 Zust\303\244ndigkeit

select encode(myByteaColumn, 'escape') from myTable

数据库设置为 UTF-8。

最佳答案

demo: db<>fiddle

convert_from()对我有用:

SELECT convert_from(decode('Zuständigkeit', 'escape'),'UTF8')

SELECT convert_from(decode('{"Zuständigkeit":"ABC"}', 'escape'),'UTF8')::jsonb -> 'Zuständigkeit'

作为旁注,您得到的是 \303\244,因为根据此 section在 PostgreSQL 文档中,encode(data bytea, 'escape') 函数:

converts zero bytes and high-bit-set bytes to octal sequences (\nnn) and doubles backslashes.

关于postgresql - 将 Bytea 转换为 Json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56188275/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com