简体   繁体   中英

Dynamically add new element to json payload in WSO2 EI by using enrich mediator

I need to enrich existing json payload with new elements which is dynamically passed to existing payload instead of static value . Can anyone please help me?

Existing payload:

{
  "Type":"CAR",
  "Identifier":"2db23c39-9d3f-4e61-b3c5-e8725a2f1b90",
  "ListingType":"New",
  "SaleStatus":"For Sale"
}

Expected:

{
  "Type":"CAR",
  "Identifier":"2db23c39-9d3f-4e61-b3c5-e8725a2f1b90",
  "ListingType":"New",
  "SaleStatus":"For Sale",
  "messageId":"urn:uuid:ccdafb72-c4"
}

Here messageId is ESB generated MessageID automatically.

 <!-- ************API Request set to incomingRequest property************ -->
 <property description="incomingRequest" expression="json-eval($.)" name="incomingRequest" 
 scope="default" type="STRING"/>
<payloadFactory media-type="json">
    <format>$1</format>
    <args>
        <arg evaluator="xml" expression="get-property('incomingRequest')"/>
    </args>
</payloadFactory>
<enrich description="">
    <source type="inline" clone="true">
        <messageId xmlns="">evaluate(get-property('operation','messageId')) 
  </messageId>
    </source>
    <target action="child" xpath="//jsonObject" />
</enrich>
<enrich>
    <source clone="true" xpath="//jsonObject" />
    <target type="body" />
</enrich>
 <log level="full"/>

**Getting Wrong output** 

{
  "Type":"CAR",
  "Identifier":"2db23c39-9d3f-4e61-b3c5-e8725a2f1b90",
  "ListingType":"New",
  "SaleStatus":"For Sale",
  "messageId": "evaluate(get-property('operation','messageId'))"
}

Note: I have followed this

Inline content on the enrich mediator is not evaluated. (considered as string) So, we can use the property mediator to evaluate the expression.

<property name="prop1" expression="get-property('operation','messageId')"/>

Then we can use the above property in enrich mediator.

<enrich description="">
    <source type="property" property="prop1"></source>
    <target action="child" xpath="//jsonObject" />
</enrich>

BTW, In latest product versions, the Enrich mediator has the native JSON support. More details in Here

I had similar scenario where i wanted to append some elements to a received json response:

  • First i got the response received into a property: property expression="json-eval($)" name="RESPONSE" scope="default" type="STRING"

  • Then i created a payload and added the new properties:

 <payloadFactory media-type="json">
                           <format>
                                {"details": $1,
                                "timestamp":"$2",
                                "value":"$3"}
                            </format>
                            <args>
                                <arg evaluator="xml"
                                    expression="get-property('RESPONSE')" />
                                <arg evaluator="xml"
                                    expression="get-property('REQUEST_TIMESTAMP')" />
                                <arg evaluator="xml"
                                    expression="get-property('VALUE')" />
                            </args>
                        </payloadFactory>

Then I returned the response using loopback. Note that REQUEST_TIMESTAMP and VALUE are defined as properties.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM