Carlos Tse
Carlos Tse

Reputation: 1

A JOLT transformation question about mapping reference

This is an input that needs to be transformed using Jolt Transformation to obtain the expected output.

I am attempting to create a jolt transformation for the below input:

{
  "flights": [
    {
      "id": "123",
      "route": "BJS-SIN"
    },
    {
      "id": "456",
      "route": "SIN-PEK"
    },
    {
      "id": "789",
      "route": "SIN-BJS"
    }
  ],
  "prices": [
    {
      "id": "abc",
      "amount": 560
    },
    {
      "id": "def",
      "amount": 780
    }
  ],
  "solutions": [
    {
      "price-ref": "abc",
      "flights-ref": [
        "123",
        "456"
      ]
    },
    {
      "price-ref": "def",
      "flights-ref": [
        "123",
        "789"
      ]
    }
  ]
}

Desired output would like :

{
  "solutions": [
    {
      "flights": [
        {
          "id": "123",
          "route": "BJS-SIN"
        },
        {
          "id": "456",
          "route": "SIN-PEK"
        }
      ],
      "price": {
        "id": "abc",
        "amount": 560
      }
    },
    {
      "flights": [
        {
          "id": "123",
          "route": "BJS-SIN"
        },
        {
          "id": "789",
          "route": "SIN-BJS"
        }
      ],
      "price": {
        "id": "def",
        "amount": 780
      }
    }
  ]
}

As the output, all the data should be constructed refer to the solutions node. How about the mapping expression in JOLT? all the values from the source json are generated randomly and the keys names are fixed.

I had tried many times but could not find the right spec, so please help.

Upvotes: 0

Views: 85

Answers (1)

Mohammadreza Khedri
Mohammadreza Khedri

Reputation: 2691

You can use this spec:

[
  {
    "operation": "shift",
    "spec": {
      "*": "&",
      "flights|prices": {
        "*": {
          "*": "&2.@(1,id).&"
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "solutions": {
        "*": {
          "flights*": {
            "*": {
              "*": {
                "@(6,flights.&)": "&5[&4].flights[&2]"
              }
            }
          },
          "price*": {
            "*": {
              "@(5,prices.&)": "&4[&3].price"
            }
          }
        }
      }
    }
  }
]

Upvotes: 1

Related Questions