Search code examples
arraysjsondynamicjolt

Need Jolt spec to convert the input json with dynamic field to output json


I need jolt spec for the below input json with dynamic date field and array of items. That jolt spec should convert this input json to below output json.

Input Json:

{
  "market": {
    "2024-04-13": [
      {
        "code": "ABC",
        "name": "Name1",
        "date": "2024-04-13T00:00:00.000Z",
        "rev": 100,
        "sold": 1
      }
    ],
    "2024-04-14": [
      {
        "code": "DEF",
        "name": "Name2",
        "date": "2024-04-14T00:00:00.000Z",
        "rev": 233.32,
        "sold": 3
      },
      {
        "code": "EFG",
        "name": "DISCOUNT",
        "date": "2024-04-14T00:00:00.000Z",
        "rev": 300.05,
        "sold": 1
      },
      {
        "code": "MNO",
        "name": "GROUP",
        "date": "2024-04-14T00:00:00.000Z",
        "rev": 45.33,
        "sold": 3
      }
    ],
    "2024-04-15": [
      {
        "code": "POR",
        "name": "TEST",
        "date": "2024-04-15T00:00:00.000Z",
        "rev": 100,
        "sold": 1
      }
    ]
  }
}

Output json

{
  "market": {
    "DATE": [
      {
        "code": "ABC",
        "name": "Name1",
        "date": "2024-04-13T00:00:00.000Z",
        "rev": 100,
        "sold": 1
      },
      {
        "code": "DEF",
        "name": "Name2",
        "date": "2024-04-14T00:00:00.000Z",
        "rev": 233.32,
        "sold": 3
      },
      {
        "code": "EFG",
        "name": "DISCOUNT",
        "date": "2024-04-14T00:00:00.000Z",
        "rev": 300.05,
        "sold": 1
      },
      {
        "code": "MNO",
        "name": "GROUP",
        "date": "2024-04-14T00:00:00.000Z",
        "rev": 45.33,
        "sold": 3
      },
      {
        "code": "POR",
        "name": "TEST",
        "date": "2024-04-15T00:00:00.000Z",
        "rev": 100,
        "sold": 1
      }
    ]
  }
}

The below Jolt spec I tried but it is not giving me exact output. It is grouping first few items, but I need them separately similar like the output json.

Jolt spec

[
  {
    "operation": "shift",
    "spec": {
      "market": {
        "*": {
          "*": {
            "code": "market[&1].code",
            "name": "market[&1].name",
            "date": "market[&1].date",
            "rev": "market[&1].rev",
            "sold": "market[&1].sold"
          }
        }
      }
    }
  }
]

Solution

  • You should separate by those dates combined with the sub-index values(0,1,2) in order to be able to distincuish all sub-objects such as

    [
      {
        "operation": "shift",
        "spec": {
          "*": { //lhe layer for the "market"
            "*": { //the layer for the arrays
              "*": { //the layer for the indexes of the arrays
                "*": "&3.&2_&1.&" // &3 : going 3 levels up to bring the literal "market"
                                  // &2 : the date values
                                  // &1 : the indexes of the arrays
                                  // &  : the leaf node which represents the replication the respective value 
              }
            }
          }
        }
      },
      { //arrange the key names of the outermost objects
        "operation": "shift",
        "spec": {
          "*": {
            "*": {
              "*": "&2.DATE[#2].&"
            }
          }
        }
      }
    ]
    

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

    enter image description here

    Edit : the following option(which @samersaleh [thanks to him] proposed) would be more straightforward :

    [
      { 
        "operation": "shift",
        "spec": {
          "*": {
            "*": {
              "*": "&2.DATE[]"
            }
          }
        }
      }
    ]