how can we perform json to json transformation using apigee similar to xslt policy for xml?

I wish to transform a json input to a json output in another format.Is there any default policy in apigee for doing this? If not,do we need to write custom code to achieve this purpose. Does apigee have support for jolt-transform.??

0 2 1,947
2 REPLIES 2

We don't have an OOTB policy for JSON-to-JSON transformation. But we can achieve that in multiple ways by using Assign Message Policy or Javascript(custom coding).

Example Input JSON -

{
  "employees": [
    {
      "mail": "john@gmail.com",
      "displayName": "John",
      "Id": "111"
    },
    {
      "mail": "jack@gmail.com",
      "displayName": "Jack",
      "Id": "222"
    }
  ]
}

Sample Assign Message Policy -

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<AssignMessage name="AM-Response">
    <AssignVariable>
        <Name>json_path_name1</Name>
        <Value>$.employees[0].displayName</Value>
    </AssignVariable>
    <AssignVariable>
        <Name>json_path_name2</Name>
        <Value>$.employees[1].displayName</Value>
    </AssignVariable>
    <AssignVariable>
        <Name>json_path_id</Name>
        <Value>$.employees[1].Id</Value>
    </AssignVariable>
    <Set>
        <Payload contentType="application/json">
            {
              "name1":"{jsonPath(json_path_name1,request.content)}",
              "name2":"{jsonPath(json_path_name2,request.content)}",
              "ids":"{jsonPath(json_path_id,request.content)}"
            }
        </Payload>
    </Set>
    <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
    <AssignTo createNew="false" transport="http" type="response"/>
</AssignMessage>

Output JSON -

{
    "name1": "John",
    "name2": "Jack",
    "ids": "222"
}

AFAIK Jolt transformation is not supported in APigee.