bazaarvoice / jolt

JSON to JSON transformation library written in Java.
Apache License 2.0
1.54k stars 328 forks source link

Discard object if array inside the object is empty #1193

Closed juandbotiva closed 1 year ago

juandbotiva commented 1 year ago

Hi, is it possible to have a transformation discard an object if an array inside this object is empty ?

Input

{
  "customer_orders": [
    {
      "customer_order_id": 5531,
      "customer": {
        "id": 16136
      },
      "items": []
    },
    {
      "customer_order_id": 5532,
      "customer": {
        "id": 16137
      },
      "items": [
        {
          "item_id": "324682934",
          "name": "keyboard"
        }
      ]
    },
    {
      "customer_order_id": 5533,
      "customer": {
        "id": 16138
      },
      "items": [
        {
          "item_id": "324682936",
          "name": "Mouse"
        }
      ]
    }
  ]
}

Output

{
  "customer_orders": [
    {
      "customer_order_id": 5532,
      "customer": {
        "id": 16137
      },
      "items": [
        {
          "item_id": "324682934",
          "name": "keyboard"
        }
      ]
    },
    {
      "customer_order_id": 5533,
      "customer": {
        "id": 16138
      },
      "items": [
        {
          "item_id": "324682936",
          "name": "Mouse"
        }
      ]
    }
  ]
}

Any suggestions would be greatly appreciated. Thanks in advance.

janvi04 commented 1 year ago

Hi @juandbotiva You can use the below specs:

[
   {
      "operation":"modify-overwrite-beta",
      "spec":{
         "customer_orders":{
            "*":{
               "sizeOfItems":"=size(@(1,items))"
            }
         }
      }
   },
   {
      "operation":"shift",
      "spec":{
         "customer_orders":{
            "*":{
               "sizeOfItems":{
                  "0":null,
                  "*":{
                     "@2":"&4"
                  }
               }
            }
         }
      }
   },
   {
      "operation":"remove",
      "spec":{
         "customer_orders":{
            "*":{
               "sizeOfItems":""
            }
         }
      }
   }
]

I hope this helps.

rishabh9926 commented 1 year ago

Hi @juandbotiva

[
  { 
    "operation": "shift",
    "spec": {
      "customer_orders": {
        "*": {
          "items": {
            "*": {
              "@2": "customer_orders[]"
      }
     }
    }
   }
  }
 }
]

Check this!

janvi04 commented 1 year ago

Thanks for sharing this @rishabh9926

rishabh9926 commented 1 year ago

Thanks for sharing this @rishabh9926

Welcome @janvi04 Ma'am!