bazaarvoice / jolt

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

Apply cardinality to root level object #1165

Open shubidubapp opened 1 year ago

shubidubapp commented 1 year ago

I am getting a data that is based on the amount of results can either be an array of objects or a single object like below.

{ << when i have only one result I am getting this
  "foo": "x",
  "bar": "y"
}

[ << when i have more then one result i am getting an array of objects
  {
    "foo": "x0",
    "bar": "y0"
  },
  {
    "foo": "x1",
    "bar": "y2"
  },
]

On each cases as a result I want an array of object(s) like below

[
  {
    "foo": "x",
    "bar": "y"
  },
  ...
]

I want to be able to apply cardinality to the root, if its a single object turn it into "MANY" and if its already an array do nothing. I can't find a way where I can apply cardinality to root object. right now what I am doing is this;

[
  {
    "operation": "shift",
    "spec": { "@": "data" }
  },
  {
    "operation": "cardinality",
    "spec": {
      "data": "MANY"
    }
  },
  {
    "operation": "shift",
    "spec": {
      "data": ""
    }
  }
]

Where I am first moving the object to a key ("data") apply cardinality and then move the result back to the root. Which works, but I dont think is the correct way of doing it.