1

i'm trying to transform the following JSON in input:

{
            "operation": "create",
            "id": "$1",
            "name": "esempio create",
            "type": "CAR",
            "status": 0,
            "country": "JAP",
        } 

in this new format:

{
  "operations": [
    {
      "operation": "C",
      "element": {
        "type": "CAR",
        "other_data": {"id":$1, "name": "example", "status":0, "country":"JAP"}
      }
    }
  ]
}

i'm using the following method, where element is the JSON mentioned above:

var js=JSON.stringify({"operation":"C", "element":{"type": element.type , "other_data":{element}}});

in every javascript compiler it works correctly, when i try to apply it on WSO2 i'm not able to save the page because the IDE (Integration Studio) detect an error on javascript. Do you know any other way to do it or tell me why i'm not able to save it?

Thanks

Marco
  • 85
  • 6

1 Answers1

1

Your syntax is correct but Integration Studio doesn't seem to like inline access of JSON elements when creating the JSON. Also you don't need JSON.stringify here. Following is a workaround with the Script Mediator. I assume you need t delete type elemt from other_data as well.

var element = mc.getPayloadJSON();
var js = {"operation":"C", "element":{"type": "x" , "other_data":"x"}};
js.element.type = element.type;
delete element.type;
js.element.other_data = element;
mc.setPayloadJSON(js);

Alternatively you can use the Payloadfactory Mediator along with Enrich mediator if you are using the latest Micro Integrator.

<payloadFactory media-type="json">
    <format>{
      "operations": [
        {
          "operation": "C",
          "element": {
            "type": $1,
            "other_data": $2
          }
        }
      ]
    }</format>
    <args>
        <arg evaluator="json" expression="$.type"/>
        <arg evaluator="json" expression="$"/>
    </args>
</payloadFactory>
<enrich>
    <source clone="false" xpath="json-eval($.operations[0].element.type)"/>
    <target action="remove" type="body"/>
</enrich>
ycr
  • 12,828
  • 2
  • 25
  • 45