简体   繁体   中英

Searching JSONObject in springboot rest api (postgresql)

I want to perform search operations based on some JSON data stored in the database which has the following format:

   {   
     "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
 }

So, How can I search all the JSON's in the postgresql database for the key metadata.animal where all animals are "not dirty"?

PS: I would like to perform the search during a get request (like a get request to /animal/search/meta/animal?animal=not+dirty should search all the records and display all records with meta.animal=not dirty).

I assume this JSON is stored in a particular 1 column.

  1. create a similar VO bean file in java.

     public class DatabaseJson{ private Object item; private String dateCreated; private Meta meta; private String id; private String type; private String system; // generate getters settrers as well }

and your Meta class will be like this,

public class Meta{
        private String animal;
        private int key;
        private String life;
    // generate getters settrers as well
    }
  1. fetch this json data in your code, might be datatype is String.

    List<String jsonList = fetchAllJsonFromDatabase();

// Now you can convert this into your vo object(using fasterxml)

DatabaseJson databseJson = convertStringtoElement (inputString, DatabaseJson.class);

Now you can check particular value from databaseJson, (in your case, it's animal)

public static <T> T convertStringtoElement(String jsonString, Class<T> conversionClass)
    {
        T eee = null;
        try {
            com.fasterxml.jackson.databind.ObjectMapper objectMapper = new com.fasterxml.jackson.databind.ObjectMapper();
            objectMapper.addMixIn(JAXBElement.class, JAXBElementMixIn.class);
            if(StringUtils.isNotEmpty(jsonString)){
                com.fasterxml.jackson.databind.JsonNode jsonNode = objectMapper.readTree(jsonString);
                eee = objectMapper.treeToValue(jsonNode, conversionClass);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return eee;
    }

PostgreSQL supports native JSON data type since version 9.2 and thus i assume that your database table column is of type 'json'. If that is the case, one way is to direct your http get request to execute an SQL query like so:

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

You might want to check this and this .

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