I am working on a Java Spring Boot application. I have a JSON in the following format. I need to search for a string and return the results in the same JSON format containing only the matching entries.
The original JSON is:
{
"version": "1.0.2",
"students": ["John Smith", "John Green", "Martin King"],
"teachers": ["John Lord", "Martin Evans", "Steve John", "Gordon Lee", "Leo Martin"],
"authors" : ["Martin Fowler", "Erich Gamma"]
}
For example, if I search for the string "mart", it should return the JSON below:
{
"version": "1.0.2",
"students": ["Martin King"],
"teachers": ["Martin Evans", "Leo Martin"],
"authors" : ["Martin Fowler"]
}
How can I do this efficiently? Thank you.
2
Answers
We can use java
HashMaps
for this. here is an example of how you can do this in spring.and after sending a request to the endpoint with the keyword
mart
this is the outputYou can create a record/POJO called
Result
that syncs up with the JSON data, and filter on the fields.Here is an immutable/functional approach:
Result