Search code examples
jsonjolt

Splitting a record into multiple records


I have a JSON record:

{
  "field1": "Tom"
  "field2": "x::y::z",
  "field3": "a::b::c"
}

What I want is to split this record into multiple records where each record represents the pairing of field2 and field 3.

It means first record would have field2 as x and field3 as a, second record would have field2 as y and field3 as b and so on.

What API of jolt can help here?

Expected output:

{
  "field1": "Tom",
  "field2": "x",
  "field3": "a"
}
{
  "field1": "Tom",
  "field2": "y",
  "field3": "b"
}
{
  "field1": "Tom",
  "field2": "z",
  "field3": "c"
}

I think first stage of transformation can be:

{
  "operation": "modify-overwrite-beta",
  "spec": {
    "field2": "=split('::', @(1,field2))",
    "field3": "=split('::', @(1,field3))"
  }
}

This would result into field2 and field3 being converted to an array.


Solution

  • You can the split the values within modify, and then separate key-value pairs into individual objects within shift transformation such as

    [
      {
        "operation": "modify-overwrite-beta",
        "spec": {
          "*2|*3": "=split('::', @(1,&))"
        }
      },
      {
        "operation": "shift",
        "spec": {
          "*2|*3": {
            "*": {
              "@3,field1": "[&1].field1",
              "@": "[&1].&2"
            }
          }
        }
      },
      {
        "operation": "cardinality",
        "spec": {
          "*": {
            "*": "ONE"
          }
        }
      }
    ]
    
    

    the demo on the site http://jolt-demo.appspot.com/ is :

    enter image description here