Home > Enterprise >  What does JOLT [&] syntax means?
What does JOLT [&] syntax means?

Time:07-30

I've read this and this and this but explanations are pretty scarce. I understand what "&" means. But I can't wrap my head around what [&] means =\

Can someone please explain me what this [&] does applicable to this example? What exactly happening in lines

"@1": "outer[&4].inner[&2].t",
"@(3,eventTypeCode)": "outer[&4].inner[&2].etc"

Input JSON :

{
  "uID": 1000358546,
  "events": [
    {
      "eventTypeCode": "FEEDBACK",
      "transports": [
        "PUSH",
        "SMS"
      ]
    },
    {
      "eventTypeCode": "MARKETING",
      "transports": [
        "PUSH",
        "EMAIL"
      ]
    },
    {
      "eventTypeCode": "ORDER_STATUS",
      "transports": [
        "SOC_VK"
      ]
    }
  ]
}

Spec :

[
  {
    "operation": "shift",
    "spec": {
      "events": {
        "*": {
          "transports": {
            "*": {
              "*": {
                "@1": "outer[&4].inner[&2].t",
                "@(3,eventTypeCode)": "outer[&4].inner[&2].etc"
              }
            }
          }
        }
      },
      "*": "&"
    }
  }
]

Result :

{
  "uID": 1000358546,
  "outer": [
    {
      "inner": [
        {
          "t": "PUSH",
          "etc": "FEEDBACK"
        },
        {
          "t": "SMS",
          "etc": "FEEDBACK"
        }
      ]
    },
    {
      "inner": [
        {
          "t": "PUSH",
          "etc": "MARKETING"
        },
        {
          "t": "EMAIL",
          "etc": "MARKETING"
        }
      ]
    },
    {
      "inner": [
        {
          "t": "SOC_VK",
          "etc": "ORDER_STATUS"
        }
      ]
    }
  ]
}

CodePudding user response:

Just count the single colon(colon without opening curly brace) along with the other opening curly braces while going the tree up(from inner to outer) for any wildcard such as @,$,&,[&]. Only difference among & and [&] is the second one tries to build an array while the first one replicates the respective value of the targeted level. So, [&] would do nothing(doesn't build an array) whenever the ampersand fully points out null values unlike to &

A side note : key[&] yields (with/without a dot after key) key name of the array while key& yields a simple concatenation (prefixing the respective value of &)

Let me try to explain using this spec :

[
  {
    "operation": "shift",
    "spec": {
      "events": {
        "*": {
          "transports": {
            "*": {
              "*": {
                "@1": "@(4,eventTypeCode).[&2]"
              }
            }
          }
        }
      }
    }
  }
]

you'd get the result

{
  "FEEDBACK" : [ "PUSH", "SMS" ],
  "MARKETING" : [ "PUSH", "EMAIL" ],
  "ORDER_STATUS" : [ "SOC_VK" ]
}

by using the above spec.

but it would yield

{
  "FEEDBACK" : {
    "0" : "PUSH",
    "1" : "SMS"
  },
  "MARKETING" : {
    "0" : "PUSH",
    "1" : "EMAIL"
  },
  "ORDER_STATUS" : {
    "0" : "SOC_VK"
  }
}
  • if [&2] is replaced with &2. Since, both reaches to the same level(the level of the indices of "transports" through counting 0->[traversing single colon], 1->[traversing opening curly brace], 2->[traversing opening curly brace] ), one makes array while the other reveals the values of the indices as 0,1. Think a reciprocally changing for the represented values by "@1" and "@(4,eventTypeCode)"(right hand side one goes left while left hand side one goes right) -> @1 picks the values of the indices of "transports"(traversing opening curly brace twice by counting 0,1[if it was @[meaning @0] would traverse { once by counting only 0).

  • if [&2] is replaced with &, then the output would be replicating the values coming from @1; while if [&2] is replaced with [&], then there would yield only a null because the innermost node has only nulls.

  • Related