【问题标题】:How do I aggregate JSON data using Jolt by two keys?如何通过两个键使用 Jolt 聚合 JSON 数据?
【发布时间】:2018-08-21 19:38:43
【问题描述】:

如果我有一个 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",
                        }]
                } 
}

【问题讨论】:

  • 看起来不是同一个问题
  • 您可以查看 JSON 序列化/反序列化库,例如 Jackson,以按照您喜欢的方式对其进行转换。
  • @dahightime 查看问题的第三个答案,其中提到了具有移位操作的 jolt 库。
  • 我将如何使用 jolt 来做到这一点?我看到了演示,它只显示聚合一个键

标签: java json parsing aggregate jolt


【解决方案1】:

令人惊讶的容易。

规格

[
  {
    "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)[]"
      }
    }
  }
]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-09-07
    • 1970-01-01
    • 2022-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-05
    相关资源
    最近更新 更多