1

I have a JSON Object that looks like this:

{"firstName":"Todd","lastName":"Jones","age":32}

My question is how can I remove the age field? I was looking in the Enrich Mediator documentation and noticed an example to remove selected parts from a payload:

Sample 7 - Remove selected parts from the payload ( this feature is available from EI 6.6.0 WUM level 1595516738094 )

    <target>
       <inSequence>
         <enrich>
            <source clone="true" xpath="json-eval($.store.book[*].author,$.store.book[0])"/>
            <target type="body" action="remove"/>
         </enrich>
         <respond/>
      </inSequence>
      <outSequence/>
    </target>

This would not work for me however because it is available on EI 6.6.0 and I am on EI 6.1.1. How else could I go about achieving the intended result?

mortimerfreeze
  • 199
  • 1
  • 1
  • 9

2 Answers2

1

As an alternate approach to Enrich Mediator and PayloadFactory Mediator, you can use the Script Mediator to remove the property. Given is a sample, deleting the age property

<property name="JSONPayload" expression="json-eval($.)" />
<script language="js">
    <![CDATA[
        // var log = mc.getServiceLog();
        
        var response = mc.getProperty("JSONPayload");
        var jsonPayload = JSON.parse(response);
        delete jsonPayload['age'];

        // log.info(JSON.stringify(jsonPayload));

        mc.setPayloadJSON(jsonPayload);
    ]]>
</script>
Athiththan
  • 1,874
  • 8
  • 18
  • 1
    I honestly can't recommend using the Script mediator unless you really have no other choice. Aside from using a lot more resources it can lead to other issues as well as the error handling not being very great. But It is a legit option true. – Novren Jul 06 '21 at 13:34
  • This is what I ended up going with. I made sure to only run the script if the value is there beforehand as to handle some of the error potential. – mortimerfreeze Jul 06 '21 at 15:11
0

If your payload is only 3 elements I reckon you could just go with the payloadfactory.

           <payloadFactory media-type="json">
                <format>
                    {"firstName":"$1","lastName":"$2"}

                </format>
                <args>
                    <arg evaluator="json" expression="$.firstName"/>
                    <arg evaluator="json" expression="$.lastName"/>
                </args>
            </payloadFactory>

You might have to enable the JSON stream formatter/builder for this as described here. https://docs.wso2.com/display/EI611/PayloadFactory+Mediator

If the above doesn't work you could also first put the firstName and lastName into separate properties and then reference it as a context property.

I.e. <arg evaluator="xml" expression="$ctx:firstName"/>

Novren
  • 69
  • 6