How to Convert a JSON Request into a List of Specific Format n Spring?

Question

Source: https://stackoverflow.com/questions/70978408/how-to-convert-a-json-request-into-a-list-of-specific-format-n-spring

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.