skip to Main Content

I am trying to convert the below JSON payload into a JSON that has the field name as the value of the field:

The jolt file that I have is working if field values are different. But if field values are the same then it is giving an array in the response.

Can you please help to provide jolt specifications for this?

Input JSON Payload:

{
  "action": {
    "Success": true,
    "records": [
      {
        "Id": "Test_abc",
        "SubscriptionID": "ID_1"
      },
      {
        "Id": "Test_abc",
        "SubscriptionID": "ID_2"
      },
      {
        "Id": "Test_xyz",
        "SubscriptionID": "ID_3"
      }
    ],
    "type": "update"
  }
}

Expected output:

{
  "action": {
    "Success": true,
    "records": {
      "Test_abc": {
        "SubscriptionID": "ID_1"
      },
      "Test_abc": {
        "SubscriptionID": "ID_2"
      },
      "Test_xyz": {
        "SubscriptionID": "ID_3"
      }
    },
    "type": "update"
  }
}

Solution not found yet.

2

Answers


  1. You can use the following shift transformation spec

    [
      {
        "operation": "shift",
        "spec": {
          "*": {
            "*": "&1.&", // &1 replicates the literal "action"
            "records": {
              "*": {
                "S*": "&3.&2.@(1,Id).&" // &3 replicates the literal "action" (by going three level up the tree), &2 for "records", & replicates the current level attribute
              }
            }
          }
        }
      }
    ]
    

    presumingly converting one of the Id value from Test_abc to another one such as Test_def, since the desired output is wrong as a JSON value(There cannot be more than one object with the same tag at the same level)

    Login or Signup to reply.
  2. Your output is wrong. You can’t have multiple objects with the same keys.

    If You want to support all objects of records array, You should bring them into an array like this spec:

    [
      {
        "operation": "shift",
        "spec": {
          "*": {
            "*": "&1.&",
            "records": {
              "*": {
                "S*ID": "&3.&2[].@(1,Id).&"
              }
            }
          }
        }
      }
    ]
    

    enter image description here

    But, if you want to have an object as your records in the output, You should remove the duplicate ID like this spec:

    [
      {
        "operation": "shift",
        "spec": {
          "*": {
            "*": "&1.&",
            "records": {
              "*": {
                "S*ID": "&3.&2.@(1,Id).&"
              }
            }
          }
        }
      },
      {
        "operation": "cardinality",
        "spec": {
          "*": {
            "records": {
              "*": {
                "*": "ONE"
              }
            }
          }
        }
      }
    ]
    

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search