首页 > 解决方案 > 在springboot rest api(postgresql)中搜索JSONObject

问题描述

我想基于存储在具有以下格式的数据库中的一些 JSON 数据执行搜索操作:

   {   
     "id": "ae002e48-f540-44ac-9050-3bd24902a",
     "type": "Devil",
     "System": "not your system",
     "dateCreated": "2012-12-12T15:22:53.798",
     "meta":{
             "key":123,
             "life": "get a life",
             "animal":"not dirty"
            },
     "item": null
 }

那么,如何在postgresql数据库中的所有 JSON 中搜索所有动物都“不脏”的关键 metadata.animal?

PS:我想在获取请求期间执行搜索(例如对 /animal/search/meta/animal?animal=not+dirty 的获取请求应该搜索所有记录并显示所有带有 meta.animal=not dirty 的记录) .

标签: javajsonpostgresqlspring-bootrest

解决方案


PostgreSQL自 9.2 版起支持本机 JSON 数据类型,因此我假设您的数据库表列的类型为“json”。如果是这种情况,一种方法是引导您的 http get 请求执行 SQL 查询,如下所示:

SELECT
    your_column_name ->> 'id' AS id
FROM
    your_table_name
WHERE
    your_column_name -> 'meta' ->> 'animal' = 'not dirty'

您可能想检查thisthis


推荐阅读