Home > Software engineering >  How to parse a JsonString into key value pairs?
How to parse a JsonString into key value pairs?

Time:07-07

I have a PostRequest here that I want to be able to save data to different tables. About the @RequestBody I get a JsonString that I want to split to be able to execute an INSERT INTO query.

Here is my PostRequest:

    @PostMapping(value = "/config/test/{tableName}/{schemaName}")
    public String postValue(@RequestBody String values, @PathVariable("tableName") String tableName, @PathVariable("schemaName") String schemaName) {
        String keyString = "";
        String valueString = "";
        final String sql = "INSERT INTO "   schemaName   "."   tableName   "("   keyString   ") VALUES("   valueString   ")";
        final Query query = em.createNativeQuery(sql);
        query.executeUpdate();
        return values;
    }

And here's my JSONString:

{
    "id": 23,
    "indexNummer": 4,
    "indexName": "Gewichtung Alter Periode ohne Maßnahmen",
    "minVal": 51.0,
    "maxVal": 85.0,
    "indexWert": 1
}

Is there any way to split my string so that my two strings keyString, valueString are filled as follows?

keyString = "id,indexNumber,indexName,minVal,maxVal,indexValue" valueString="23,4, "Is there any way to split my string so that my two strings keyString, valueString are filled as follows?

keyString = "id, indexNumber, indexName, minVal, maxVal, indexValue"

valueString="23, 4, "Gewichtung Alter Periode ohne Maßnahmen", 51.0, 85.0, 1"

CodePudding user response:

You can convert JSONString into Map or you can directly read it is map and then do the following

@PostMapping(value = "/config/test/{tableName}/{schemaName}")
public Map postValue(@RequestBody Map<String, Object> values, @PathVariable("tableName") String tableName, 
@PathVariable("schemaName") String schemaName) {
        String keyString = "";
        String valueString = "";

        Set<String> keySet = values.keySet();

        for (String key : keySet) {

            // add comma after first key-value pair only.
            if (keyString.length() > 0) {
                keyString  = ",";
                valueString  = ",";
            }

            keyString  = key;

            Object valueObj = values.get(key);
            if (valueObj instanceof String) {
                valueString = valueString   "\""   valueObj.toString()   "\"";
            } else if (valueObj instanceof Integer) {
                Integer valueInt = (Integer) valueObj;
                valueString = valueString   valueInt;
            } else if (valueObj instanceof Double) {
                Double valueDouble = (Double) valueObj;
                valueString = valueString   valueDouble;
            }
        }

        final String sql = "INSERT INTO "   schemaName   "."   tableName   "("   keyString   ") VALUES("   valueString   ")";
        final Query query = em.createNativeQuery(sql);
        query.executeUpdate();
        return values;
}

CodePudding user response:

Use com.fasterxml.jackson.databind.ObjectMapper as below:

ObjectMapper mapper =new ObjectMapper();
        
        String string="{\r\n"
                  "    \"id\": 23,\r\n"
                  "    \"indexNummer\": 4,\r\n"
                  "    \"indexName\": \"Gewichtung Alter Periode ohne Maßnahmen\",\r\n"
                  "    \"minVal\": 51.0,\r\n"
                  "    \"maxVal\": 85.0,\r\n"
                  "    \"indexWert\": 1\r\n"
                  "}";
        
        TypeReference<HashMap<String, String>> typeRef = new TypeReference<HashMap<String, String>>() {
        };
        HashMap<String, String> map = mapper.readValue(string, typeRef);
        String keys = map.keySet().stream().collect(Collectors.joining(","));
        String values = map.values().stream().collect(Collectors.joining(","));
        System.out.println(keys);
        System.out.println(values);

Output :

indexNummer,maxVal,minVal,indexName,id,indexWert

4,85.0,51.0,Gewichtung Alter Periode ohne Maßnahmen,23,1

  • Related