How do i convert a json array object of an object into a java 8 optional list of that object

MIike Eps :
   public Optional<GetCategoryResponseDto> GetCategory(AppUser foundUser) throws Exception {

        Optional<GetCategoryResponseDto> optional;
        JSONParser parser = new JSONParser();
        org.json.simple.JSONObject json;
        //fix for lazy user details not loaded
        if (foundUser.getAppUserDetail() == null) {
            foundUser = appUserService.findByID(foundUser.getId()).orElseThrow(() -> new ModelNotFoundException("Invalid user"));
        LOGGER.debug("foundUser {} ", gson.toJson(foundUser.getAppUserDetail().getPhoneNumber()));

        String output = getCategoryServiceController.myGetCategory();
        LOGGER.debug("output {} ", output);
        json = (JSONObject) parser.parse(output);
        ObjectMapper mapper = new ObjectMapper();
        GetCategoryResponseDto dto = new GetCategoryResponseDto();
        dto = mapper.readValue((DataInput) json, GetCategoryResponseDto.class);
        return Optional.of(dto);

Thats the updated code and still this line " GetCategoryResponseDto dto = mapper.readValue(json, GetCategoryResponseDto.class);" is still causing syntax errors

Deadpool :

If below line returns JSON formatted string then you don't need any JSONParser

String output = getCategoryServiceController.myGetCategory();

ObjectMapper has readValue method that takes String and Class<T> as arguments

public <T> T readValue(String content,
          Class<T> valueType)
        throws IOException,

You can just use that method

String output = getCategoryServiceController.myGetCategory();
LOGGER.debug("output {} ", output

GetCategoryResponseDto dto = mapper.readValue(json, GetCategoryResponseDto.class

return Optional.of(dto);

Note There are lot more unnecessary lines in GetCategory, for example

Optional<GetCategoryResponseDto> optional;
org.json.simple.JSONObject json = null;

And also i see mapper is null, you might face NullPointerException if it is null

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at


Login to comment


How to convert JSON string into List of Java object?

How to upcast object contained in Java 8 Optional?

Convert json array to a java list object

Filter an Optional<List<Object>> in java8

How do I convert an object array to an object list

How do I convert an object to an array?

Convert JSON array to Java Class Object List

How do I turn a JSON file into a Java 8 Object Stream?

How to convert Array into json object?

How convert JSON array or JSON object to list of object?

convert list<Object> to array of <Id> in java 8 stream

how do I convert an object to an array list in JS

How to convert object array to user defined object in java8

Convert List<Object[]> Array into an object

How do i convert an array with varying size to a JSON object?

How to convert array to a json object?

How do I convert a Json file (that contains a array) to a List (of a class object) C# Unity

How do I map a heterogeneous JSON array to a Java object?

How do i convert "[object Object]" to a number

how do i convert json object to array without key

How to Convert String Array JSON in a Java Object

How do I convert an Array with a JSON string into a JSON object (ruby)

How do I convert a JSON to an object literal?

How do I convert JSON object to CLLOCATIONDEGREES?

How do I remove "Optional()" from object in an array

How to convert List to varargs object in Java 8

How can I convert List of LinkedHashMap to List of Custom Object using Java 8

How to I convert stringified json to object in java?

How do i convert JSON object to an array as above?

TOP Ranking

  1. 1

    How to return a single asset OhMyBrew Laravel Shopify

  2. 2

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  3. 3

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  4. 4

    Loopback Error: connect ECONNREFUSED (MAMP)

  5. 5 port in URL

  6. 6

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  7. 7

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  8. 8

    Make a B+ Tree concurrent thread safe

  9. 9

    Group boxplot data while keeping their individual X axis labels in ggplot2 in R

  10. 10

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  11. 11

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  12. 12

    BigQuery - concatenate ignoring NULL

  13. 13

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  14. 14

    12.04.3--- Dconf Editor won't show com>canonical>unity option

  15. 15

    Razor Page - How to redirect to another folder's page using asp-for tag helper

  16. 16

    "django.db.utils.ProgrammingError: relation "auth_user" does not exist" Django V2.0

  17. 17

    How to exclude a specific git submodule from update?

  18. 18

    how to fix CMake Error in CMakeLists.txt: Generator NMake Makefiles does not support platform specification, but platform x64 was specified

  19. 19

    Is there a way to ask the user before loading the website

  20. 20

    How to remove the extra space from right in a webview?

  21. 21

    Optimising manipulation of large vectors