简体   繁体   中英

postgresQL jsonb compare parsing

I have saved data in postgresQL jsonb and I want to compare and parse the jsonb data. For example

I have a table called 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. Is it possible?

Yes, it is possible to compare values from two different jsonb documents. 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM