With Jolt json transformation, is it possible to copy a value into two different attributes?
Asked Answered
S

2

7

Am trying something very simple with Jolt transformation but struggling to get it to work.

If I have an input like:

{
  "id": "54436001"
}

I want output to be:

{
  "mediaId" : "54436001",
  "events" : {
    "mediaId" : "54436001"
  }
}

Which is copying a value to two different attributes. I would be tempted to try spec like this to work, but obviously it doesn't because of duplicate key.

[
  {
    "operation": "shift",
    "spec": {
      "id": "mediaId",
      "id": "events.mediaId"
    }
  }
]

Is this possible with Jolt transformation ?

Sherbet answered 12/1, 2017 at 16:52 Comment(0)
B
16

Yes

Spec

[
  {
    "operation": "shift",
    "spec": {
      "id": ["mediaId", "events.mediaId"]
    }
  }
]

The idea is if you want shift to write a value to two locations in the output, use an array on the right hand side of the spec.

Brashear answered 12/1, 2017 at 20:9 Comment(1)
Is there any good jolt transformation tutorial you know? Most of the tutorial I found seems bit complex for a quick read. I using it along with Apache Nifi, which comes very handy, but am using a very light form of it.Sherbet
R
0

Yet, there are some other alternatives such as

[
  {
    "operation": "shift",
    "spec": {
      "id": "mediaId",
      "@id": "events.mediaId"
    }
  }
]
[
  {
    "operation": "shift",
    "spec": {
      "id": "events.mediaId",
      "@0,id": "mediaId"
    }
  }
]
[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "@": "mediaId",
        "@0": "events.mediaId"
      }
    }
  }
]
[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "*": {
          "$": "mediaId",
          "@1": "events.mediaId"
        }
      }
    }
  }
]

all of them will return the same desired result

Rota answered 13/3, 2023 at 12:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.