Using JDK 15
Text Blocks : Text blocks start with a โโโ (three double-quote marks) followed by optional whitespaces and a newline.
Please find the solution below to convert json to string and vice-versa, it might help you in some way.
public class Test {
public static void main(String[] args) throws JsonProcessingException {
String s = """
{
"students":[{
"name": "Albert",
"code":"GE",
"marks":[{"mark":"20"},{"mark":"40"}]},
{
"name": "Gert",
"code":"LE",
"marks":[{"mark":"26"}]},
{
"name": "John"
},
{
"name": "John Doe",
"code":"LP",
"marks":[{"mark":"40"}]}
]
}
""";
//JSON TO STRING
ObjectMapper mapper = new ObjectMapper();
String s2 = mapper.writeValueAsString(s);
System.out.println("JSON TO STRING:" + s2);
//STRING TO JSON
String s3 = mapper.readValue(s2, new TypeReference<>() {});
System.out.println("STRING TO JSON:" + s3);
}
}
Output:
JSON TO STRING:"{\n\"students\":[{\n\"name\": \"Albert\",\n\"code\":\"GE\",\n\"marks\":[{\"mark\":\"20\"},{\"mark\":\"40\"}]},\n{\n\"name\": \"Gert\",\n\"code\":\"LE\",\n\"marks\":[{\"mark\":\"26\"}]},\n{\n\"name\": \"John\"\n},\n{\n\"name\": \"John Doe\",\n\"code\":\"LP\",\n\"marks\":[{\"mark\":\"40\"}]}\n]\n}\n"
STRING TO JSON:{
"students":[{
"name": "Albert",
"code":"GE",
"marks":[{"mark":"20"},{"mark":"40"}]},
{
"name": "Gert",
"code":"LE",
"marks":[{"mark":"26"}]},
{
"name": "John"
},
{
"name": "John Doe",
"code":"LP",
"marks":[{"mark":"40"}]}
]
}
Answer from iamgirdhar on Stack OverflowUsing JDK 15
Text Blocks : Text blocks start with a โโโ (three double-quote marks) followed by optional whitespaces and a newline.
Please find the solution below to convert json to string and vice-versa, it might help you in some way.
public class Test {
public static void main(String[] args) throws JsonProcessingException {
String s = """
{
"students":[{
"name": "Albert",
"code":"GE",
"marks":[{"mark":"20"},{"mark":"40"}]},
{
"name": "Gert",
"code":"LE",
"marks":[{"mark":"26"}]},
{
"name": "John"
},
{
"name": "John Doe",
"code":"LP",
"marks":[{"mark":"40"}]}
]
}
""";
//JSON TO STRING
ObjectMapper mapper = new ObjectMapper();
String s2 = mapper.writeValueAsString(s);
System.out.println("JSON TO STRING:" + s2);
//STRING TO JSON
String s3 = mapper.readValue(s2, new TypeReference<>() {});
System.out.println("STRING TO JSON:" + s3);
}
}
Output:
JSON TO STRING:"{\n\"students\":[{\n\"name\": \"Albert\",\n\"code\":\"GE\",\n\"marks\":[{\"mark\":\"20\"},{\"mark\":\"40\"}]},\n{\n\"name\": \"Gert\",\n\"code\":\"LE\",\n\"marks\":[{\"mark\":\"26\"}]},\n{\n\"name\": \"John\"\n},\n{\n\"name\": \"John Doe\",\n\"code\":\"LP\",\n\"marks\":[{\"mark\":\"40\"}]}\n]\n}\n"
STRING TO JSON:{
"students":[{
"name": "Albert",
"code":"GE",
"marks":[{"mark":"20"},{"mark":"40"}]},
{
"name": "Gert",
"code":"LE",
"marks":[{"mark":"26"}]},
{
"name": "John"
},
{
"name": "John Doe",
"code":"LP",
"marks":[{"mark":"40"}]}
]
}
I suggest you store the students in a table with a column for each attribute from the JSON. I am not familiar with Spring, but other web frameworks I used are able to fetch a row from the database and seralize it to JSON in the way you want. They can also take JSON and put it into the database as individual columns rather than an single string.