Home > front end >  How to convert input string to json string or json object in Springboot
How to convert input string to json string or json object in Springboot

Time:12-08

How to convert input string to json string or json object in Springboot

I want below string to be converted into json of specified format.

String request = "xyz"

expected json output = "{"abc":{"efg":"request"}}".

Inner "request" in the above json should be "xyz".

CodePudding user response:

If there is no restriction about library, you can use below code block

    ObjectMapper objectMapper = new ObjectMapper();
    ObjectNode node = objectMapper.createObjectNode();
    JsonNode innerNode = objectMapper.createObjectNode();
    ((ObjectNode)innerNode).put("efg", request);
    node.set("abc",innerNode);
  • Related