Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
589 views
in Technique[技术] by (71.8m points)

How to replace specific value in Json based on condition in Mule(3) Dataweave 1.0

I have a Json Input like below, where if doa is null or "" it needs to replace with the value test if it contains Value then no change required. I have achieved in dwl 2 version using update. Using mabObject in dataweave 1.0 version, but it is not working out. Looking for your expertise. Thanks

Input:

 {
    "sets": {
        "exd": {
            "cse": {
                "doa": null
            }
        }
    }
}

Achieved in dwl 2 :

%dw 2.0
output application/json skipNullOn="everywhere"
import * from dw::util::Values
---
 if 
 (!isEmpty (payload.sets.exd.cse.doa )) payload
 else
 payload  update
  {
    case .sets.exd.cse.doa! -> "test"
}
 

Response

     {
     "sets": {
        "exd": {
            "cse": {
                "doa": "test"
            }
        }
    }
}

Looking for Mule 3 dataweave 1.0 version of it??

question from:https://stackoverflow.com/questions/65946999/how-to-replace-specific-value-in-json-based-on-condition-in-mule3-dataweave-1

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

If / else statements work considerably differently in dataweave 1.0, update doesn't exist, and I don't know if isEmpty exists as part of core - don't think so. If you know ahead of time the structure of the object, you can simply build a new object like this:

%dw 1.0
%input payload application/json
%output application/json
---
payload when (payload.sets.exd.cse.doa != '' and payload.sets.exd.cse.doa != null) 
otherwise {
  sets: {
    exd: {
      cse: {
        data: "test"
      }
    }
  }
}


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...