Aditya
Aditya

Reputation: 1058

Jolt transformation to append a json to existing array

Success Response Case

Failure Response Case

I have a written a jolt transformation to transform an API response.

The Input is

{
  "sample": {
    "numbers": [
      {
        "id1": "number1",
        "id2": "1",
        "levels": {
          "id3": "level3"
        }
      },
      {
        "id1": "number2",
        "id2": "2",
        "levels": {
          "id3": "level-test"
        }
      }
    ],
    "extra": [
      {
        "id1": "extra1",
        "id2": "extra2",
        "plans": {
          "id3": "first-plan",
          "unit": "2"
        }
      }
    ]
  }
}

For Failure scenario input is

    {
    "code": "NOT_VALID",
    "message": "Invalid Request",
    "httpStatus": "400"
    }

The jolt transformation is

[
  {
    "operation": "shift",
    "spec": {
      "sample": {
        "numbers": {
          "*": {
            "id1": "numbers[&1].id1",
            "id2": "numbers[&1].id2",
            "levels": {
              "id3": "numbers[&2].id3"
            }
          }
        },
        "#true": "success"
      },
      "message": "errorMessage",
      "code": {
        "#false": "success"
      }
    }
  }
]

The expected Output is

{
  "success": "true",
  "numbers": [
    {
      "id1": "number1",
      "id2": "1",
      "id3": "level3"
    },
    {
      "id1": "number2",
      "id2": "2",
      "id3": "level-test"
    },
    {
      "id1": "extra1",
      "id2": "extra2",
      "id3": "first-plan"
    }
  ]
}

The output which I am able to get right now is

{
  "success": "true",
  "numbers": [
    {
      "id1": "number1",
      "id2": "1",
      "id3": "level3"
    },
    {
      "id1": "number2",
      "id2": "2",
      "id3": "level-test"
    }
  ]
}

I am not able to figure out how extra array fields (id1, id2, plans.id3) will be appended to numbers array after all the elements of that array are transversed.

The Desired failure response should be

{
"errorMessage": "Invalid Request",
"success": "false"
}

Any Idea how it can be achieved.

Upvotes: 1

Views: 737

Answers (1)

Barbaros Özhan
Barbaros Özhan

Reputation: 65228

You can use following shift transformation specs

[
  {
    "operation": "shift",
    "spec": {
      "*": { //represents top level object's key name("sample")
        "*": { //represents both "numbers" and "extra"
          "*": {
            "id*": "&2[&1].&", // all keys starting with "id", and their values(".&") nested under common key and sub-key pairs such as "numbers[0]", "numbers[1]", "extra[0]"
            "levels|plans": {
              "id*": "&3[&2].&"
            }
          }
        }
      }
    }
  },
  {
   // remove object key names while adding extra attribute "success"
    "operation": "shift",
    "spec": {
      "#true": "success",
      "*": {
        "*": "numbers"
      }
    }
  }
]

the demo is on the site http://jolt-demo.appspot.com/ :

enter image description here

Upvotes: 1

Related Questions