1

Product:

+-------------+--------------+-------+
| ProductCode |  OrderDate   | User  |
+-------------+--------------+-------+
|      100    | 2015-01-01   | John  |
|      100    | 2015-01-01   | Peter |
|      100    | 2015-01-02   | Max   |
|      200    | 2015-01-01   | Max   |
|      200    | 2015-01-05   | John  |
|      300    | 2015-01-10   | Peter |
+-------------+--------------+-------+

Expected JSON structure:

{
"100": {
    "2015-01-01": {
        "Jon": {
            "spent": "...",
            "comment": "..."
        },
        "Peter": {
            "spent": "...",
            "comment": "..."
        }
    },
    "2015-01-02": {
        "Max": {
            "spent": "...",
            "comment": "..."
        }
    }
},
"200": {
    "2015-01-01": {
        "Max": {
            "spent": "...",
            "comment": "..."
        }
    },
    "2015-01-05": {
        "Jon": {
            "spent": "...",
            "comment": "..."
        }
    }
},
"300": {
    "2015-01-10": {
        "Peter": {
            "spent": "...",
            "comment": "..."
        }
    }
}

}

What I have done so far:

//Create ListMultimap
ListMultimap<String, Product> productCodeMap = ArrayListMultimap.create();
ListMultimap<String, Product> productDateMap = ArrayListMultimap.create();
ListMultimap<String, Product> productUserMap = ArrayListMultimap.create();

//Fetch products
products  = dao.getProducts();

//Create map with relevant keys
for(Product product : products){
    productCodeMap.put(product.getProductCode(), product);
}
for(String productDate : productCodeMap.keySet()){
    for(Product product : productCodeMap.get(productDate)){
        productDateMap.put(product.getProductOrderDate(), product);
    }
}
for(String productUser : productDateMap.keySet()){
    for(Product product : productDateMap.get(productDate)){
        productUserMap.put(productUser, product);
    }
}

How should I iterate the maps to generate desired JSON structure? Should I use a different collection datastructure instead?

I am using Jackson Streaming API to generate the JSON.

Will be great even if you could suggest to generate equivalent JAVA String as what I am really doing is generating a JSON by manual writing.

  • I would just sort the rows from the first to last column. When the value changes you need a new nested value. – Peter Lawrey Dec 03 '15 at 18:13
  • 3
    Having duplicate names (`"user"`) is *strongly* discouraged. It's not technically invalid, but largely unsupported. See [Does JSON syntax allow duplicate keys in an object?](http://stackoverflow.com/questions/21832701/does-json-syntax-allow-duplicate-keys-in-an-object) – Andreas Dec 03 '15 at 18:17
  • 3
    I agree with @PeterLawrey, since that is the easiest and fastest implementation, but if you prefer a Map setup, you should create a `Map>>` (or `Map>`). – Andreas Dec 03 '15 at 18:27
  • Having a list of users would be shorter and supported. – Peter Lawrey Dec 03 '15 at 18:29
  • `"2015-01-01" : ["Jon", "Peter"]` – Jos Dec 03 '15 at 18:31
  • @Andreas Sorry I have updated the intended JSON structure – FakirTrappedInCode Dec 03 '15 at 22:09
  • Thanks @PeterLawrey Can you please elaborate with a pseudocode regd: When the value changes you need a new nested value – FakirTrappedInCode Dec 03 '15 at 22:15
  • @PondicherryFellow Sort the table left to right. To start with, you have to use the first two columns are nested tags. Then for each row check if the first column is different. If it is you have to start a new outer structure. If the second column is different, you have to start a new nested structure otherwise, just output the last column. – Peter Lawrey Dec 04 '15 at 09:19

0 Answers0