0

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".

Sandeep
  • 3
  • 3
  • Does this answer your question? [How to parse JSON in Java](https://stackoverflow.com/questions/2591098/how-to-parse-json-in-java) – João Dias Dec 08 '21 at 11:45

1 Answers1

0

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);
oisleyen
  • 92
  • 2
  • 8