How to Convert a JSON Request into a List of Specific Format n Spring?
Question
I have a request JSON list like this:
[
{
Id: 0,
values: [a, b, c, d]
},
{
Id: 1,
values: [1, 2, 3, 4]
},
.
.
.
]
I want to convert the above list to a list like this:
[
{
Name: a,
Count: 1
},
{
Name: b,
Count: 2
}
{
Name: c,
Count: 3
}
{
Name: d,
Count: 4
}
]
I have a dto class consisting of name and counting attributes.
Answer
Your problem is to generate a set using members of two different sets in the original JSON file according to positions. The code will rather long if you try to do it in Java.
It is easy to accomplish this in SPL, the open-source Java package. Three lines of code are enough:
A |
|
1 |
=create(Name,Count) |
2 |
>transpose(json(file("data.json").read()).(#2)).(A1.record(~)) |
3 |
=json(A1) |
SPL offers JDBC driver to be invoked by Java. Just store the above SPL script as convert.splx and invoke it in Java as you call a stored procedure:
…
Class.forName("com.esproc.jdbc.InternalDriver");
con= DriverManager.getConnection("jdbc:esproc:local://");
st = con.prepareCall("call convert()");
st.execute();
…
View SPL source code.
SPL Official Website 👉 https://www.scudata.com
SPL Feedback and Help 👉 https://www.reddit.com/r/esProcSPL
SPL Learning Material 👉 https://c.scudata.com
SPL Source Code and Package 👉 https://github.com/SPLWare/esProc
Discord 👉 https://discord.gg/cFTcUNs7
Youtube 👉 https://www.youtube.com/@esProc_SPL
Chinese version