Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Construct JSON Object based on Query Result List

Trying to figure out a solution for a problem I am facing, but cannot find any material online so far to help me.

Essentially what I have is a method in a rest controller that passes a query string to my Hibernate DAO and gets in return data requested.

e.g.

@RequestMapping("/submitQuery")
    public Object submitQuery() {
// example of a query string, note this is dynamic and thus never hardcoded
        String query = "SELECT C.amount, C.transactionDate, R.amount, R.transactionDate FROM CAR C, RFT R";

        return  DAO.submitQuery(query);
    }

DAO:

public List<T> submitQuery(String query) {
        Query q = getSession().createQuery(query);
        return q.list();
    }

This query string will be dynamic so the option of creating a entity and attaching it against query is not an option.

The above method will return data in following format:

[
    [
        -4890.38,
        1451826000000,
        25.04,
        1421499600000
    ],
    [
        -660,
        1413205200000,
        25.04,
        1421499600000
    ],
    [
        -10768.53,
        1423054800000,
        25.04,
        1421499600000
    ]
]

So no headers and on top of that dates have been converted into digits. What i want to achieve is have the method return the results in following format:

[
    [
        "amountc" : -4890.38,
        "datec" : "01-03-2014",
        "amountr" : 25.04,
        "dater" : "01-03-2014"
    ],
    [
        "amountc" : -660,
        "datec" : "03-02-2014",
        "amountr" : 25.04,
        "dater" : "03-02-2014"
    ],
    [
        "amountc" : -10768.53,
        "datec" : "01-02-2014",
        "amountr" : 25.04,
        "dater" : "01-02-2014"
    ]
]

Any advice/assistance would be appreciated.

NOTE: Cannot use DTOs or Entities as the Query String is dynamic and changes. Query string i have there is just as example.

like image 307
Aeseir Avatar asked Aug 04 '15 08:08

Aeseir


People also ask

Can JSON contain lists?

Array Datatype in JSON Similar to other programming languages, a JSON Array is a list of items surrounded in square brackets ([]). Each item in the array is separated by a comma. The array index begins with 0. The square brackets [...] are used to declare JSON array.

Can we convert list to JSON in Java?

We can convert a list to the JSON array using the JSONArray. toJSONString() method and it is a static method of JSONArray, it will convert a list to JSON text and the result is a JSON array.

How do I query a JSON object in SQL?

You don't need a custom query language to query JSON in SQL Server. To query JSON data, you can use standard T-SQL. If you must create a query or report on JSON data, you can easily convert JSON data to rows and columns by calling the OPENJSON rowset function.

Can we convert JSONArray to JSON object?

Core Java bootcamp program with Hands on practiceWe can also add a JSONArray to JSONObject. We need to add a few items to an ArrayList first and pass this list to the put() method of JSONArray class and finally add this array to JSONObject using the put() method.


2 Answers

I think Sunil already answered. Check the edited section on how to convert to map. Or try the following

public List<Map<String,Object>> submitQuery(String query) {
        Query q = getSession().createQuery(query);
        return q.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP).list();
    }
like image 198
Shiju K Babu Avatar answered Oct 04 '22 23:10

Shiju K Babu


create a bean class let ABC.java which contains the setter and getter methods and override toString into DTO class like this.

  public class ABC implements Serializable{

    private double amountc;
    private String datec;
    private double amountr;
    private String  dater;

    ... setter and getters

    @Override
    public String toString(){
        return "[\"amountc:\""+amountc+",\"datec:\""+datec+",\"amountr:\""+amountr+",\"dater:\""+dater+"]";
    }
}

and make few change into DAO implementations class

public List<ABC> submitQuery(String query) {
    Query q = getSession().createQuery(query).setResultTransformer(Transformers.aliasToBean(ABC.class));
    List<ABC> resultList=q.list();
    System.out.println(resultList); //desire output 
    return resultList;
}

please make sure that data types should be same into entity and DTO class ABC

EDITED

in case of you don't want any bean or DTO then you can use Criteria.ALIAS_TO_ENTITY_MAP which transformer the result query into a map object with key-value pair.keys name are same as alias name into sql query.if you are not using alias into sql query then key's are like 0,1,2,3,.... and so on.

public List<ABC> submitQuery(String query) {
        Query q = getSession().createQuery(query).setResultTransformer(Criteria.ALIAS_TO_ENTITY_MAP);
        List<ABC> resultList=q.list();
        System.out.println(resultList); //desire output 
        return resultList;
    }
like image 40
Sunil Avatar answered Oct 04 '22 23:10

Sunil