bazaarvoice / jolt

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

Creating an array #1235

Open Linuski opened 9 months ago

Linuski commented 9 months ago

I have this kind of json: { "entities": { "organisations": [ { "id": "b9c0b61b-e843-48c8-83a2-52bf2ea00971", "label": "ReCAAP ISC" }, ... other things ], "places": [ { "id": "97fe568b-5b25-46dc-bb85-0a7f52996f1d", "label": "Bangladesh" }, ... other things ] } }

I would like to have this: [
{ "id": "b9c0b61b-e843-48c8-83a2-52bf2ea00971", "label": "ReCAAP ISC" }, { "id": "97fe568b-5b25-46dc-bb85-0a7f52996f1d", "label": "Bangladesh" } ]

I used this transformation: [{ "operation": "shift", "spec": { "entities": { "places": { "": "R0[#2]" }, "organisations": { "": "R0[#2]" } } } }, { "operation": "shift", "spec": { "R0": { "*": "" } } } ]

which works well for thsi case but when I have this other case: { "entities": { "organisations": [ { "id": "b9c0b61b-e843-48c8-83a2-52bf2ea00971", "label": "ReCAAP ISC" } ] } } the result is not an array: { "id": "b9c0b61b-e843-48c8-83a2-52bf2ea00971", "label": "ReCAAP ISC" } } How can I fix this?

gbouget commented 9 months ago

Does this specification cover all cases?

[
  {
    "operation": "shift",
    "spec": {
      "entities": {
        "organisations|places": {
          "*": {
            "id|label": "&2.&1.&"
          }
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": {
        "*": "[]"
      }
    }
  }
]