I need help to access these nodes from given JSON:
( all the values which are true or false)
- billingAddress
- shippingAddress
- duplicate
{
"active": "true",
"groups": {
"uid": "zzzz",
"integrationKey": " "
},
"name": "ancd",
"addresses": [
{
"town": "ewqe",
"shippingAddress": "true",
"postalcode": "1234",
"region": {
"isocode": "US-MI",
"country": {
"isocode": "US",
"integrationKey": ""
}
},
"district": "",
"streetnumber": "",
"billingAddress": "false",
"duplicate": "false",
"company": "dasdsfsdf",
"fax": "",
"pobox": "",
"phone1": "",
"cellphone": "",
},
{
"town": "d",
"shippingAddress": "true",
"postalcode": "1234",
"region": {
"isocode": "US-MI",
"country": {
"isocode": "US",
"integrationKey": ""
}
},
"district": "",
"streetname": "absnadbasnd",
"streetnumber": "",
"billingAddress": "false",
"duplicate": "false",
"company": "dmnsmnmnfmfn",
"fax": "",
"pobox": "",
"phone1": "",
"cellphone": "",
"publicKey": "zxzx_3390_00_00|zxzx|KNA1|WE",
}
]
}
I tried replce all to replce all "true" with true but that is not working.
I also tried this code but its giving error ,kindly help : error -"java.lang.Exception: java.lang.IllegalArgumentException: argument type mismatch@ line 18 in script2.groovy"
import java.util.HashMap;
import groovy.json.JsonSlurper;
import groovy.json.*
def Message processData(Message message) {
def body = message.getBody(java.lang.String) as String ;
def data = new JsonSlurper().parseText(body);
data.active = convertToBoolean(data.active);
data.buyer = convertToBoolean(data.buyer);
data.addresses.shippingAddress = convertToBoolean(data.addresses.shippingAddress);
// data.replaceAll("false", false);
// data.replaceAll("true", true);
// data.replaceAll[:"true", :true] ;
// data.replaceAll[:"false", :false];
message.setBody(new JsonBuilder(data).toPrettyString());
return message;
}
//Boolean Conversion
static Object convertToBoolean(Object inputValue){
if(inputValue.equals("true")){
return true;
}else{
return false;
}
}
tried attached code
The simplest way is to replace the
"true"|"false"values in original JSON-String.Having said that, you don't have to parse/build your JSON at all, the replacement will do.