Nikzin
Nikzin

Reputation: 366

How to extract a specific part from JSON response with changing fields names in JSON structure (as in this example) with Java in Spring Boot?

"21231" as selected pink at the picture is a changing part, depending on request parameter.

I’m new and in my student assignment I have to consume various web API recourses as JSON with java in Spring Boot. One of my task: I have to get a artist description from a request to Wikipedia API with given names of the article. Here I have article name as a parameter in the request: for example “Nirvana_(band)” as ={article} (there could be other article names as parameter in the request):

https://en.wikipedia.org/w/api.php?action=query&format=json&prop=extracts&exintro=true&redirects=true&titles={article}

The JSON response is shown: as at the picture. JSON Here I only need to extract a string as selected on the picture light green: "< p >< b>Nirvana was an American rock … < /p>\ n< p>"

If I think to use restTemplate.getForObject() my problem will be here: the JSON structure will be with variation in terms of title "21231" (object selected pink on the image): I don’t know how to map this to object.

Thus I don’t know what approach (method) I have to use in this case? (to consume a JSON like this in order to get to a specific value from it). Any kind suggestions? Thank you!

Upvotes: 1

Views: 1827

Answers (2)

Ankit
Ankit

Reputation: 2256

You can use also use Jquery for making the query and parsing the response from frontend layer. Here is the following code that prints the actual content (as per your requirement) on the web console.

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
   var value='India'; //you can pass dynamic value here 
   var playListURL = 'http://en.wikipedia.org/w/api.php?format=json&action=query&titles='+value+'&prop=revisions&rvprop=content&callback=?';

    $.getJSON(playListURL ,function(data) {
        $.each(data.query.pages, function(i, item) {
        var responseText =  item.revisions[0];
        $.each(responseText,(index,item) => {
            if(index == '*'){
                console.log('real value=>'+item); 
            }
            console.log(index);
        });
        console.log(responseText.contentformat);
        });
    });
});
</script>
</head>
</html>

Upvotes: 1

Dimitri Mestdagh
Dimitri Mestdagh

Reputation: 44745

If you have a dynamic key/value pair, you can use a Map-like structure to map your objects, for example:

public class Page {
    private int pageId;
    private int ns;
    private String title;
    private String extract;

    // Getters, setters, ...
}

public class Query {
    private Map<String, Page> pages;

    // Getters, setters, ...
}

This will result in a Query object containing a Map<String, Page> where the key is "21231" and the value is a proper Page object.

If you expect that this will only contain a single value, you can use a Java 8 stream:

query.getPages()
    .values().stream()
    .findAny()
    .orElseThrow(() -> new RuntimeException("There is no page"));

Alternatively you could create your own datastructure (like a PagePair) and write a custom deserializer, but that's a bit more work.

Upvotes: 3

Related Questions