简体   繁体   中英

How do I aggregate JSON data using Jolt by two keys?

If I have an input of a list of JSON objects. How do I go about nesting the data in Java by the date, and then the category in and also sorting it by date in descending order?

Input:

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

You can see in my below output, that I am trying to group the data by the date first, and then within the date I want to group the objects by the category.

Desired Output:

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

Surprising easy.

Spec

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM