Reputation:
I have a below JSON structure. which I need to parse using the Jackson library. I am building a web-service, which accept below JSON in a POST method's BODY.
{
"organization": {
"products": [
"foo",
"bar",
"baz"
]
},
"mission" : "to be the best in domain"
}
Till, now I was having simple JSON body, which wasn't having nested and JSON element, like in this case organization
is another JSON node which contains a Set of products.
This JSON keys are not mandatory, And I am accepting/storing organization
JSON in JsonNode
. And doing below checks.
organization
is null.organization
is not null and it has products
key.But after that I don't know how to fetch the set of boards from this JsonNode
and store it in Java's HashSet.
My expected O/P should be to have a set of boards extracted from my organization
JsonNode.
P.S. :- I think I have to use the ObjectMapper
but couldn't find a direct way of getting the Set. Looks like I need to use some JsonParser with which I am not very familier.
Upvotes: 1
Views: 419
Reputation: 658
You can create DTOs(Data Transfer Objects) for your purpose. The nested objects could have the structure as below:
class Organization {
List<String> Products;
.....
}
class WebOrganizationRequest {
Organization organization;
String mission;
}
By creating objects in this way you are mapping your JSON objects to classes and Jackson will typecast the JSON as an instance of WebOrganizationRequest when you pass it in the controller with WebOrganizationRequest as the request body type.
Upvotes: 2