0

I am creating a Java service that gets a JSON Object from a HTTP GET Request. This is an example of the JSON Object I am getting:

{
    "data": [{
        "itemNumber": "547325645",
        "manufacturer": "LV3",
        "name": "Levis 501",
        "minimumQuantity": "1.0",
        "maximumQuantity": "10.0",
        "prices": [{
            "currency": "EUR",
            "amount": "80.0"
        }]
    }, {
        "itemNumber": "224145625",
        "manufacturer": "LV3",
        "name": "Levis 502",
        "minimumQuantity": "1.0",
        "maximumQuantity": "10.0",
        "prices": [{
            "currency": "EUR",
            "amount": "90.0"
        }]
    }],
    "pagination": {
        "offset": 0,
        "limit": 2,
        "total": 1925
    }
}

Right now I am using Jackson to map my HttpResponse into a JSON Object:

    productListResponse = new BufferedReader (
        new InputStreamReader(getResponse.getEntity().getContent(), StandardCharsets.UTF_8))
            .lines()
            .collect(Collectors.joining("\n"));
    JsonNode productListJson = mapper.readTree(productListResponse);

The problem where I run into now is that I can't parse this JSON properly. For example, I want to omit many values, like the quantities or the currencies. I also want to convert the manufacturer value to another text, in this case "Levis". But what fails for now is the data list, this does not get along with the mapper. I get an empty object back, which does not contain the JSON, but I have already tested if I get a response at all and I do. The next problem would be, of course, that the prices are also in a list and there I also do not know how to go on.
Simply put, I just want a semicolon-separated CSV file where I pull individual values out of the JSON. Of course, I have already done a lot of research and also found reliable information, but it does not apply to my case.
These are some of the sources I have already used:
https://www.baeldung.com/java-converting-json-to-csv
Converting JSON to XLS/CSV in Java
https://docs.aspose.com/cells/java/convert-json-to-csv/

danya
  • 1
  • 1
  • Can you use a Jersey or Spring RestController as the endpoint for your incoming GET request? These frameworks will map the incoming JSON in the request to a Java POJO object for you. You shouldn't need to write code to parse raw JSON strings yourself, use a framework to do it for you. – Kevin Hooke Feb 01 '21 at 18:41
  • That's really a good idea and unfortunately I didn't know about it :D I'm only bound to Java 1.8 and write a standalone application, so that should work. Do you have maybe a link to an explanation on how to do this with Jersey? I just come across very complicated examples – danya Feb 01 '21 at 18:49
  • The Jersey docs have tutorials and examples. This would be a simpler approach than writing code to parse raw JSON strings by hand https://eclipse-ee4j.github.io/jersey.github.io/documentation/latest/getting-started.html – Kevin Hooke Feb 01 '21 at 18:54

0 Answers0