1

I want to save JSON data in dynamodb DB table as a column name with the corresponding value, i,e:

Date = 2017-06-12
Cash Buyer = 5751.0

To push the data into dynamodb table I need the JSON to be in the form of List<Map<String,Object>> Format.

The json data looks like this:

{"dataset":{"id":19701916,"column_names":["Date","Cash Buyer","Cash Seller \u0026 Settlement","3-months Buyer","3-months Seller","15-months Buyer","15-months Seller","Dec 1 Buyer","Dec 1 Seller","Dec 2 Buyer","Dec 2 Seller","Dec 3 Buyer","Dec 3 Seller"],"frequency":"daily","type":"Time Series","premium":false,"limit":null,"transform":null,"column_index":null,"start_date":"2012-01-03","end_date":"2017-06-14","data":[["2017-06-14",5684.0,5684.5,5715.0,5717.0,null,null,5780.0,5790.0,5790.0,5800.0,5790.0,5800.0],["2017-06-13",5658.0,5659.0,5692.0,5693.0,null,null,5750.0,5760.0,5765.0,5775.0,5760.0,5770.0],["2017-06-12",5751.0,5752.0,5782.0,5783.0,null,null,5835.0,5845.0,5850.0,5860.0,5845.0,5855.0]"database_id":13206}}

This JSON Data has column name in one list and thier corresponding value in another Array List.I am getting this JSON Data from an API. Can someone help me in this?

2
  • Do I understand correct, that you want a json like that: {"columnName1": "value1", "columnName2": "value2",... }? Commented Jun 16, 2017 at 8:50
  • Yes exactly ... Commented Jun 16, 2017 at 8:56

1 Answer 1

1

First of all, the json is invalid (you can validate it here). Once validated, you can use any Json serialisation tool to read and transform it. Below is an example with Jackson (documentation here):

//Deserialisation
ObjectMapper mapper = new ObjectMapper();
Map<String, Object> dataMap = (Map<String, Object>) mapper.readValue("{\"dataset\":{\"id\":19701916,\"column_names\":[\"Date\",\"Cash Buyer\",\"Cash Seller Settlement\",\"3-months Buyer\",\"3-months Seller\",\"15-months Buyer\",\"15-months Seller\",\"Dec 1 Buyer\",\"Dec 1 Seller\",\"Dec 2 Buyer\",\"Dec 2 Seller\",\"Dec 3 Buyer\",\"Dec 3 Seller\"],\"frequency\":\"daily\",\"type\":\"Time Series\",\"premium\":false,\"limit\":null,\"transform\":null,\"column_index\":null,\"start_date\":\"2012-01-03\",\"end_date\":\"2017-06-14\",\"data\":[[\"2017-06-14\",5684.0,5684.5,5715.0,5717.0,null,null,5780.0,5790.0,5790.0,5800.0,5790.0,5800.0],[\"2017-06-13\",5658.0,5659.0,5692.0,5693.0,null,null,5750.0,5760.0,5765.0,5775.0,5760.0,5770.0],[\"2017-06-12\",5751.0,5752.0,5782.0,5783.0,null,null,5835.0,5845.0,5850.0,5860.0,5845.0,5855.0]]}}", Map.class);
System.out.println(transform((Map<String, Object>)dataMap.get("dataset")));


//Transformation

private static List<Map<String,Object>> transform (Map<String, Object> dataMap){
    List<Map<String,Object>> result = new ArrayList<>();
    List<String> columns = (List<String>) dataMap.get("column_names");
    List<List<Object>> data = (List<List<Object>>) dataMap.get("data");

    for(List<Object> dataElement : data){
        Map<String, Object> element = new HashMap<>();
        for(int i = 0 ; i < dataElement.size() ; i++){
            element.put(columns.get(i), dataElement.get(i));
        }
        result.add(element);
    }

    return result;
}

P.S. This is just an example using Map and List. You can create Pojos and use those to get rid of casting.

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

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.