Home > Software engineering >  JOLT Specification for converting filed value to field name
JOLT Specification for converting filed value to field name

Time:12-22

I am trying to convert the below JSON payload into a JSON which 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.

CodePudding user response:

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)

CodePudding user response:

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

  • Related