Issue
I am trying to map the following JSON to a model in my Spring Boot app:
{
"quote": [
{
"market": "Market-X",
"date_applied": 1573589600000,
"values": [
{
"price": 10
},
{
"price": 20
}
]
},
{
"market": "Market-Y",
"date_applied": 1572459600000,
"values": [
{
"price": 100
},
{
"price": 200
}
]
}
]
}
In order t read this JSON properly, I created the following models:
QuoteResponse:
@Getter
@Setter
public class QuoteResponse {
private List<Quote> quote;
}
Quote:
@Getter
@Setter
public class Quote {
@JsonProperty("market")
private String market;
@JsonProperty("values")
private List<Values> values;
}
Values:
@Getter
@Setter
public class Values {
@JsonProperty("price")
private int price;
}
1. Is there a better approach for reading JSON without creating the corresponding model classes same as JSON structure?
2. What is the proper to map JSON to these models?
Solution
Looking at your question, the answer depends on the situation. Most of the time when you make an API call to third-party services they will return a JSON object of a different structure in that case you cannot possibly create models to map values from the response JSON. But in some cases if you want to retrieve some fields or if you are making an API call between internal microservices then it's feasible to make a model to map values from the response JSON.
i) As for the first way (creating models) you have already done it so there's nothing to say.
ii) As for the second way there are plenty of JSON parsing libraries. Like @BharathYes said org.json is one of them along with that there are others like Alibaba's fastJSON, googles GSON, etc.
My personal opinion is to use Alibaba's fastJSON.
you can look into this link regarding fastJSON : https://www.baeldung.com/fastjson
To give you a simple example :
you can convert your JSON to the required POJO,
QuoteResponse quoteResponse = JSONObject.parseObject(jsonString ,QuoteResponse.class);
OR you can also use the JSONObject class itself (if you don't want to create a model)
i)
JSONObject quoteObject = JSONObject.parseObject(jsonString ,JSONObject.class);
ii)
JSONObject quoteObject = JSONObject.parseObject(jsonString);
You can either use i) OR ii) they both will get the job done.
So to sum it up you can do it like this using fastJSON
String jsonString = "{\n" +
" \"quote\": [\n" +
" {\n" +
" \"market\": \"Market-X\",\n" +
" \"date_applied\": 1573589600000,\n" +
" \"values\": [\n" +
" {\n" +
" \"price\": 10\n" +
" },\n" +
" {\n" +
" \"price\": 20\n" +
" }\n" +
" ]\n" +
" },\n" +
" {\n" +
" \"market\": \"Market-Y\",\n" +
" \"date_applied\": 1572459600000,\n" +
" \"values\": [\n" +
" {\n" +
" \"price\": 100\n" +
" },\n" +
" {\n" +
" \"price\": 200\n" +
" }\n" +
" ]\n" +
" }\n" +
" ]\n" +
"}";
JSONObject jsonPayload = JSONObject.parseObject(jsonString);
List<JSONObject> quoteArray = jsonPayload.getList("quote", JSONObject.class);
for (JSONObject quote : quoteArray) {
System.out.println(quote);
System.out.println(quote.getJSONArray("values"));
}
P.S make sure to import from import com.alibaba.fastjson2.* package since it contains more features.
Answered By - Akhil
Answer Checked By - Timothy Miller (JavaFixing Admin)