[英]postgresQL jsonb compare parsing
I have saved data in postgresQL jsonb and I want to compare and parse the jsonb data.我已经将数据保存在 postgresQL jsonb 中,我想比较和解析 jsonb 数据。 For example
例如
I have a table called test我有一个叫做 test 的表
If the column has a column called category and category data,如果该列有一个名为类别和类别数据的列,
in the category column在类别栏中
{categories:[{"id" : 1}] }
in the category data column在类别数据列中
{categories_data: [{"categories_id": 1}]}
If data is stored like this, I want to parse the corresponding object by comparing the id in the category column with the categories_id in the category or data column.如果数据是这样存储的,我想通过将category列中的id与category或data列中的categories_id进行比较来解析对应的对象。 Is it possible?
是否有可能?
Yes, it is possible to compare values from two different jsonb documents.是的,可以比较来自两个不同 jsonb 文档的值。 This is how to parse them:
这是解析它们的方法:
SELECT
jsonb_array_elements(category->'categories')->'id',
jsonb_array_elements(categories_data->'categories_data')->'categories_id'
FROM yourtable
Demo:演示:
WITH j (category,categories_data)AS (
VALUES ('{"categories":[{"id" : 1}]}'::jsonb,
'{"categories_data": [{"categories_id": 1}]}'::jsonb)
)
SELECT
jsonb_array_elements(category->'categories')->'id'
=
jsonb_array_elements(categories_data->'categories_data')->'categories_id'
FROM j;
?column?
----------
t
(1 row)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.