简体   繁体   中英

Replace value in Json in Dataweave

The mule payload is in flowVars (infodata)

[
        {
        "status": "submitted",
        "identity": "",
        "papers": {
            "code1": "12csd3cbsdj",
            "code2": "skd02nd28dn",
            "date": "2016-06-22",
        "party": {
            "due_date": "2016-06-22",
            "personel": {
                "email": "tt@test.com",
                "value": {
                    "amount": "0.10",
                    "inflation": "HIGH"
                }
            }
        }
    }
}
]

Inside Dataweave, (1) how to remove the square brackets? (2) how to replace the value of amount and inflation dynamically (from flowVars)?

Question 2:You can directly use flowVars inside dataweave or if the values are in url you can dynamically set values using inboundProperties . Refer: https://docs.mulesoft.com/mule-user-guide/v/3.7/dataweave-reference-documentation

I have used set variable where you can dynamically exact it.Test Url used in this flow: http://localhost:8083/test?inflation=HIGH

   <flow name="testFlow">
    <http:listener config-ref="HTTP_Listener_Configuration" path="/" doc:name="HTTP">

    </http:listener>
    <logger level="INFO" doc:name="Logger"/>
    <set-variable variableName="dynamicValueAmount" value="#['2']" doc:name="Variable"/>
    <dw:transform-message doc:name="Transform Message">
        <dw:set-payload><![CDATA[%dw 1.0
         %output application/json
         ---
        [
      {
    "status": "submitted",
     "identity": "",
      "papers": {     
  "party": {
    "due_date": "2016-06-22",
    "personel": {
      "email": "tt@test.com",
      "value": {
        "amount": flowVars.dynamicValueAmount,
        "inflation": inboundProperties.'http.query.params'.inflation

      }}}}}
]]]></dw:set-payload>
    </dw:transform-message>
    <object-to-string-transformer doc:name="Object to String"/>
      <set-payload value="#[ message.payload =org.mule.util.StringUtils.replace(message.payload,&quot;[&quot;,&quot;{&quot;);message.payload =org.mule.util.StringUtils.replace(message.payload,&quot;]&quot;,&quot;}&quot;)]" doc:name="Set Payload"/>
           <logger  level="INFO" doc:name="Logger"/>
    </flow>

Regarding Question one, i have externally used replace function in set Payload( Working fine - other way). I believe it can be achieve in standard way by using Dataweave itself. Let wait for the answers.

I have resolved question #1

%dw 1.0
%output application/java
---
{
    data:flowVars.infodata replace /(\[|\])/ with ""
}

I'm still trying to understand how to dynamically change the content of the payload for question #2.

  1. Looking at the sample flowVar content, it looks like an array of json object. Is that correct? If it is array then inside dataweave, you can either iterate over flowVars.infodata map {} or just get the first object data: flowVars.infodata[0] .
  2. Not sure what you really mean by dynamically changing content. @star has shown a way how you can reference any variables inside your dataweave code. May be you can add some of your code that you are looking to edit?

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