0

I came across this question regarding replacing a non-existing field with another field's value. It explains the usage of modify-default-beta operation. Example mentioned in the post:

Spec

[
  {
    "operation": "modify-default-beta",
    "spec": {
      "shipping_address": {
        "address": "@(2,payment_address.address)"
      }
    }
  }
]

Input A, where there is not shipping address

{
  "payment_address": {
    "address": "some address"
  },
  "shipping_address": {}
}

Produces output A, where the billing address is copied over

{
  "payment_address" : {
    "address" : "some address"
  },
  "shipping_address" : {
    "address" : "some address"
  }
}

What is the meaning of the "2" in @(2,payment_address.address). I tried this example here and it works even if I replace the "2" with a "3".

ishan3243
  • 1,870
  • 4
  • 30
  • 49

1 Answers1

2

The Jolt operations do a parallel tree walk of the input JSON and the Spec. It starts at the root the Spec & input JSON and then does a depth first traversal.

While it is is doing it's depth first traversal it maintains a "stack" of the data / nodes that it has matched.

Thus in this spec, when you "match" down to "address"

"operation": "modify-default-beta",
"spec": {
  "shipping_address": {
    "address": "@(2,payment_address.address)"
  }
}

The stack looks like :

Stack "pointer"  Matched value       Pointer to Input
0                "address"           Value of Address : String if it exists
1                "shipping_address"  Value of "shipping_address" : Map if it exists
2                "_root_"            A made up entry to point to Input Json (Map or List)
3                "_root_"            Another "root" that points to a Map  
                                       WorkAround to deal with top level List JSON input

So the "2" gets you back up to the "top level" of the input Json, so that you can "navigate" down "payment_address.address".

"3" works because you are now in the wrapper that makes it so that all "top level" input to the Transforms is a Map, to workaround the fact that a top level list / "[]" if valid JSON. This wrapper is special cased to have the same reference as the "2".

"4" does not not exist in the stack, and so it doesn't do anything.

Milo S
  • 4,466
  • 1
  • 19
  • 22