如何通过两个键使用Jolt聚合JSON数据?

时间:2018-08-21 19:38:43

标签: java json parsing aggregate jolt

如果我输入了JSON对象列表。

如何按日期将数据嵌套在 Java 中,然后按类别嵌套,并按日期对数据进行降序排序?

输入:

    { "data":[{
      "date": "2015-02-26",
      "buyer": "Ryan",
      "category": "clothes",
      "quantity":"10.0"
    },
    {
      "date": "2015-02-18",
      "buyer": "Lisa",
      "category": "food",
      "quantity": "2.0"
    },    
    {
      "date": "2015-02-18",
      "buyer": "Brian",
      "category": "food",
      "quantity": "11.0",
    },
    {
      "date": "2015-02-26",
      "buyer": "Jim",
      "category": "clothes",
      "quantity": "20.0",
    },
    {
      "date": "2015-02-26",
      "buyer": "Tom",
      "category": "food",
      "quantity": "40.0",
    },
    {
      "date": "2015-02-18",
      "buyer": "Alyssa",
      "category": "clothes",
      "quantity": "13.0",
    }]
}

您可以在下面的输出中看到,我试图首先按日期对数据进行分组,然后在要按类别对对象进行分组的日期之内。

所需的输出:

{
    "2015-02-26”:{
                    “clothes”:[{
                                "date": "2015-02-26",
                                "buyer": "Ryan",
                                "category": "clothes",
                                "quantity":"10.0"
                                },
                                {
                                    "date": "2015-02-26",
                                    "buyer": "Jim",
                                    "category": "clothes",
                                    "quantity": "20.0",
                                }],
                    "food":[{
                                  "date": "2015-02-26",
                                  "buyer": "Tom",
                                  "category": "food",
                                  "quantity": "40.0",
                            }]
                }
     "2015-02-18":{
                    “clothes”:[{
                                  "date": "2015-02-18",
                                  "buyer": "Alyssa",
                                  "category": "clothes",
                                  "quantity": "13.0",
                                }],
                    "food":[{
                          "date": "2015-02-18",
                          "buyer": "Lisa",
                          "category": "food",
                          "quantity": "2.0"
                        },
                        {
                          "date": "2015-02-18",
                          "buyer": "Brian",
                          "category": "food",
                          "quantity": "11.0",
                        }]
                } 
}

1 个答案:

答案 0 :(得分:0)

让人惊讶的是

规格

[
  {
    "operation": "shift",
    "spec": {
      "data": {
        // Ex. send the first data item to 
        //    2015-02-26.clothes[] 
        //    where we want clothes to always be an array 
        //    even if it only got one value assigned to it.
        "*": "@(0,date).@(0,category)[]"
      }
    }
  }
]