2

I have a REST API call that returns the following JSON object. I need to parse this with Spring's RestTemplate. The problem is that the first key ISBN:0132856204 is variable (the numbers change depending on the book). How would I go about doing this?

{
    "ISBN:0132856204": {
        "publishers": [
            {
                "name": "Pearson"
            }
        ],
        "pagination": "xxiv, 862p",
        "identifiers": {
            "isbn_13": [
                "978-0-13-285620-1"
            ],
            "openlibrary": [
                "OL25617855M"
            ]
        },
        "weight": "1340 grams",
        "title": "Computer networking",
        "url": "https://openlibrary.org/books/OL25617855M/Computer_networking",
        "number_of_pages": 862,
        "cover": {
            "small": "https://covers.openlibrary.org/b/id/7290810-S.jpg",
            "large": "https://covers.openlibrary.org/b/id/7290810-L.jpg",
            "medium": "https://covers.openlibrary.org/b/id/7290810-M.jpg"
        },
        "publish_date": "2013",
        "key": "/books/OL25617855M",
        "authors": [
            {
                "url": "https://openlibrary.org/authors/OL31244A/James_F._Kurose",
                "name": "James F. Kurose"
            },
            {
                "url": "https://openlibrary.org/authors/OL658909A/Keith_W._Ross",
                "name": "Keith W. Ross"
            }
        ],
        "subtitle": "A Top-Down Approach"
    }
}
3
  • you mean the value of the key ISBN which is 0132856204 right? Commented May 12, 2017 at 14:06
  • Please have a look at JsonPath. I think the "$..*" operator will give you the required details without specifying the dynamic root keys like ISBN:0132856204. Commented May 12, 2017 at 15:52
  • I do a GET on this URI with the ISBN as a parameter: https://openlibrary.org/api/books?bibkeys=ISBN:{isbn}&format=json&jscmd=data. I'd prefer to solve this with RestTemplate and Jackson (it's a school assignment), but JsonPath does look like it's worth looking into it. Commented May 12, 2017 at 18:26

3 Answers 3

1

In here "ISBN:0132856204" is a value and also a key for your business. To get ISBN first, what about wrapping json content with 1 more closure?

{
   "yourAwesomePlaceHolderKey"  :  
   {
      "ISBN:0132856204": {
       ......
       }
   }
}

First get the ISBN key as a value, then your ISBN value can be used as a key to get related content.

First goal will be extracting -String1,Object1- pair where String1 is "yourAwesomePlaceholderKey" and second goal will be again extracting -String2,Object2- from Object1 where String2 is your ISBN key.

Sign up to request clarification or add additional context in comments.

Comments

0

This is the way I solved it, using JsonPath for getting the book out of the JSON object and Jackson for mapping it to a Book object:

RestTemplate restTemplate = new RestTemplate();
String isbn = "0132856204";
String endpoint = "https://openlibrary.org/api/books?jscmd=data&format=json&bibkeys=ISBN:{isbn}";

//Get JSON as String
String jsonString = restTemplate.getForObject(endpoint, String.class, isbn);

//Configure JsonPath to use Jackson for mapping
Configuration.setDefaults(new Configuration.Defaults() {
    private final JsonProvider jsonProvider = new JacksonJsonProvider();
    private final MappingProvider mappingProvider = new JacksonMappingProvider();

    @Override
    public JsonProvider jsonProvider() {
        return jsonProvider;
    }

    @Override
    public MappingProvider mappingProvider() {
        return mappingProvider;
    }

    @Override
    public Set<Option> options() {
        return EnumSet.noneOf(Option.class);
    }
});

//Parse the JSON as a book
Book book = JsonPath.parse(jsonString).read("$.ISBN:" + isbn, Book.class);

1 Comment

Why do you call Configuration.setDefaults(...)?
0

You can use JsonProperty to solve

@JsonProperty("ISBN:0132856204")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.