Reputation: 81
Enrich Json with new elements not working for me .Please guide.
Input request :
{
"id" : "1",
"make" : "NAHB"
}
I created wso2 flow named eg1.xml . I am trying to enrich the incoming json request with new element "name"
processing xml: (eg1.xml)
<?xml version="1.0" encoding="UTF-8"?>
<api context="/eg1" name="eg1" xmlns="http://ws.apache.org/ns/synapse">
<resource methods="POST GET" uri-template="/hi">
<inSequence>
<log level="full"/>
<log level="custom">
<property expression="//jsonObject" name="msg1"/>
</log>
<enrich description="">
<source clone="true" type="inline">
<name xmlns="">Home</name>
</source>
<target action="child" xpath="//jsonObject"/>
</enrich>
<log level="custom">
<property expression="//jsonObject" name="msg2"/>
</log>
<log level="full"/>
<respond/>
</inSequence>
<outSequence/>
<faultSequence/>
</resource>
</api>
current output :
{
"id" : "1",
"make" : "NAHB"
}
Desired output :
{
"id" : "1",
"make" : "NAHB"
"name" : "Home"
}
logger output :
Logger1 output(log level full) : LogMediator To: /eg1/hi, MessageID: urn:uuid:2f627daf-ac9c-4492-84ea-2736670724e8, Direction: request, Payload:
{
"id": "1",
"make": "NAHB"
}
Logger2 output : LogMediator msg1 =
<jsonObject>
<id>1</id>
<make>NAHB</make>
</jsonObject>
Logger3 output : LogMediator msg2 =
<jsonObject>
<id>1</id>
<make>NAHB</make>
<name>Home</name>
</jsonObject>
Logger4 output(log level full) : LogMediator To: /eg1/hi, MessageID: urn:uuid:2f627daf-ac9c-4492-84ea-2736670724e8, Direction: request, Payload:
{ "id": "1", "make": "NAHB" }
Postman output :
{
"id": "1",
"make": "NAHB"
}
Regards, Aditya
Upvotes: 4
Views: 1499
Reputation: 2013
you have 3 ways to do it:
Enrich mediator: as described in the other answers
Payload Factory mediator: will require adding all the parameters in the arguments unless the new element will be in a separate sub element
Script mediator:
<script language="js">
<![CDATA[
var payload = mc.getProperty('payloadProperty'); // if original json is in property. If received as payload use: mc.getPayloadJSON();
var json = JSON.parse(payload);
var newElement = mc.getProperty('newElement'); // get the value of the new element
json.newElementName = newElement; // define it (newElementName not present in the json in the original payload
mc.setPayloadJSON(json); // put the result back as payload. Optional: read it in a property as done below if needed, otherwise just call the next service
]]>
</script>
<property expression="json-eval($)" name="json" scope="default" type="STRING"/>
Upvotes: 1
Reputation: 389
You have to add the response back to body
<api xmlns="http://ws.apache.org/ns/synapse" name="eg1" context="/eg1">
<resource methods="POST GET" uri-template="/hi">
<inSequence>
<log level="full"/>
<log level="custom">
<property name="msg1" expression="//jsonObject"/>
</log>
<enrich description="">
<source type="inline" clone="true">
<name xmlns="">Home</name>
</source>
<target action="child" xpath="//jsonObject"/>
</enrich>
<log level="full">
<property name="msg2" expression="//jsonObject"/>
</log>
<enrich>
<source clone="true" xpath="//jsonObject"/>
<target type="body"/>
</enrich>
<log level="full"/>
<respond/>
</inSequence>
<outSequence/>
<faultSequence/>
I have tried this API
Request
{
"id" : "1",
"make" : "NAHB"
}
Response
{
"id": 1,
"make": "NAHB",
"name": "Home"
}
Upvotes: 4