postgresql - Is jsonb conversion to text deterministic?
问题描述
This is the short version of an overly long question that sadly attracted no answers.
Is it possible, given two jsonb variables x and y, to have both
1. (x = y) yield true, and
2. (x::text = y::text) yield false
I ask this question because it appears there is no promised order in which a jsonb object will be unpacked into a string. I'd just like to be sure this is the case.
Thanks in advance for feedback!
Edit:
The original question covers the "why" for this question, but the skinny is that I hope to group data in different rows based upon a hash of many columns represented as text, some of which are jsonb.
I don't care which way the object comes in or which way it gets unpacked, but I do care if two jsonb fields which are equivalent as jsonb are not equivalent as text strings.
As it seems I cannot count on text representations to be presented in the same way, I've normalized out the jsonb field to a separate table with the jsonb field set as a unique index.
And if I write more here... this question will approach the length of the one it derives from!
解决方案
由于JSON 对象定义,正式顺序不是确定性的:
对象是一组无序的名称/值对。
实际上,对象似乎是按键的长度然后按字母顺序排序的:
with example(col) as (
values
('{"cc": 1, "ab": 1, "a": 1, "aa": 1, "b": 2, "abc": 1}'::jsonb)
)
select col::text
from example
col
-------------------------------------------------------
{"a": 1, "b": 2, "aa": 1, "ab": 1, "cc": 1, "abc": 1}
(1 row)
请注意,此行为未记录在案,并且可能会在将来的版本中更改(尽管看起来不太可能)。
推荐阅读
- javascript - 计时器未在 React 组件内运行
- sublimetext3 - 如何在 Linux 上的 sublime 中为命令添加助记符
- nest-api - 在设备访问控制台上访问和编辑 Pub/Sub 主题
- sql - 查找一行是否以 clob 中的指定字符串开头,然后提取
- javascript - 如何获取表格的所有行并将其显示在警报消息中?
- python - Python api 发布请求
- javascript - 正则表达式捕获位填充
- typescript - 找不到模块“C:\native\win32\x64\12.13.0\edge_nativeclr”
- javascript - 当 playlist.m3u8 文件不断更新时,如何播放 HLS 流?
- python-3.x - 根据行值和行值总和为零在熊猫中创建新列