skip to Main Content

I’m currently trying to perform an operation in jolt that I believe would be easiest if I could group my data by a value first. I am trying to group this list by the value columnIndex

Input:

{
  "temp_sfp": [
    {
      "value": 600,
      "valueOperator": "=",
      "uom": "mcg",
      "name": "Vitamin D (as D3 Cholecalciferol)",
      "percentDvOperator": "=",
      "percentDv": "75.0",
      "columnIndex": 1
    },
    {
      "value": 2,
      "valueOperator": "=",
      "uom": null,
      "name": "Bacillus Coagulans",
      "percentDvOperator": "=",
      "percentDv": null,
      "columnIndex": 1
    },
    {
      "value": 300,
      "valueOperator": "=",
      "uom": "mcg",
      "name": "Vitamin D (as D3 Cholecalciferol)",
      "percentDvOperator": "=",
      "percentDv": "50.0",
      "columnIndex": 0
    },
    {
      "value": 1,
      "valueOperator": "=",
      "uom": "mg",
      "name": "Bacillus Coagulans",
      "percentDvOperator": "=",
      "percentDv": null,
      "columnIndex": 0
    }
  ]
}

Desired Output:

{
  "sfp_group": [
    {
      "1": [
        {
          "value": 600,
          "valueOperator": "=",
          "uom": "mcg",
          "name": "Vitamin D (as D3 Cholecalciferol)",
          "percentDvOperator": "=",
          "percentDv": "75.0",
          "columnIndex": 1
        },
        {
          "value": 2,
          "valueOperator": "=",
          "uom": null,
          "name": "Bacillus Coagulans",
          "percentDvOperator": "=",
          "percentDv": null,
          "columnIndex": 1
        }
      ]
    },
    {
      "2": [
        {
          "value": 300,
          "valueOperator": "=",
          "uom": "mcg",
          "name": "Vitamin D (as D3 Cholecalciferol)",
          "percentDvOperator": "=",
          "percentDv": "50.0",
          "columnIndex": 0
        },
        {
          "value": 1,
          "valueOperator": "=",
          "uom": "mg",
          "name": "Bacillus Coagulans",
          "percentDvOperator": "=",
          "percentDv": null,
          "columnIndex": 0
        }
      ]
    }
  ]
}

From previous posts I’ve tried something like this to try and group on column index

{
  "operation": "shift",
  "spec": {
    "@1": "",
    "temp_sfp": {
      "*": {
        "columnIndex": {
          "*": {
            "*": {
              "@4": "&2"
            }
          }
        }
      }
    }
  }
}

but I can’t seem to really wrap my head around it.

2

Answers


  1. The trick is to use @1,columnIndex on the right hand side, it’s suitable to duplicate them for your case.

    So, you can use the following shift transformation spec :

    [
      {
        "operation": "shift",
        "spec": {
          "*": {
            "*": {
              "@": "sfp_group[@(1,columnIndex)].@1,columnIndex"
            }
          }
        }
      }
    ]
    

    which will give the desired result if columnIndex values are 0,1 as in the current input, but if they are different such as 1,2 as stated within the desired output, then the following spec would be better to use(to be generic)

    [
      {
        "operation": "shift",
        "spec": {
          "*": {
            "*": {
              "@": "@1,columnIndex.@1,columnIndex"
            }
          }
        }
      },
      {
        "operation": "shift",
        "spec": {
          "*": "sfp_group[]"
        }
      }
    ]
    
    Login or Signup to reply.
  2. Alternative syntax 🙂

    [
      {
        "operation": "shift",
        "spec": {
          "temp_sfp": {
            "*": "@columnIndex" // or @columnIndex[] to print an array even with one element
          }
        }
      },
      {
        "operation": "shift",
        "spec": {
          "*": "sfp_group[].&"
        }
      }
    ]
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search