cancel
Showing results for 
Search instead for 
Did you mean: 

Remove double from the value of a string in multiple fields

MDThouheed
Discoverer
0 Kudos

Dear Experts,

I have transformed the xml to JSON and got the below output. It is possible to have a single record or multiple records, and the fields are not present in the array.

I will receive three input files. 

1st input file:

 

{
	"timestamp": "2024-03-14T00:00:00.000",
	"productID": "FG_APC_2",
	"unitOfMeasureQuantity": "1",
	"unitOfMeasure": "PC",
	"dimensionUnitOfMeasure": "IN",
	"weightUnitOfMeasure": "KG",
	"description": "Desc_FG_APC_2",
	"length": "100.000",
	"width": "12.000",
	"height": "45.000",
	"weight": "200.000",
	"identifiers": [
		{
			"label": "GTIN",
			"allowedValues": [
				{
					"value": ""
				}
			]
		}
	],
	"alternateUOM": [
		{
			"productID": "FG_APC_2",
			"unitOfMeasureQuantity": "500",
			"unitOfMeasure": "CAR",
			"length": "500.000",
			"width": "60.000",
			"height": "225.000",
			"weight": "1000.000",
			"identifiers": [
				{
					"label": "GTIN",
					"allowedValues": [
						{
							"value": ""
						}
					]
				}
			]
		},
		{
			"productID": "FG_APC_2",
			"unitOfMeasureQuantity": "5000",
			"unitOfMeasure": "PAL",
			"length": "5000.000",
			"width": "600.000",
			"height": "2250.000",
			"weight": "10000.000",
			"identifiers": [
				{
					"label": "GTIN",
					"allowedValues": [
						{
							"value": ""
						}
					]
				}
			]
		}
	],
	"messageType": "product",
	"mode": "MANAGE"
}

 

2nd input file:

 

{
    "timestamp": "2024-03-11T11:07:09-04:00",
    "productID": "LU-T05-SW",
    "unitOfMeasureQuantity": "1",
    "unitOfMeasure": "EA",
    "dimensionUnitOfMeasure": "IN",
    "weightUnitOfMeasure": "LB",
    "description": "LUMARIS TW TAPE 5M SW",
    "length": "9.0",
    "width": "6.5",
    "height": "1.25",
    "weight": "0.2",
    "identifiers": [
        {
            "label": "GTIN",
            "allowedValues": [
                {
                    "value": "0784276311007"
                }
            ]
        }
    ],
    "messageType": "product",
    "mode": "MANAGE"
}

 

3rd input file:

 

{
	"timestamp": "2024-03-14T00:00:00.000",
	"productID": "FG_APC_2",
	"unitOfMeasureQuantity": "1",
	"unitOfMeasure": "PC",
	"dimensionUnitOfMeasure": "IN",
	"weightUnitOfMeasure": "KG",
	"description": "Desc_FG_APC_2",
	"length": "100.000",
	"width": "12.000",
	"height": "45.000",
	"weight": "200.000",
	"identifiers": [
		{
			"label": "GTIN",
			"allowedValues": [
				{
					"value": ""
				}
			]
		}
	],
	"alternateUOM": [
		{
			"productID": "FG_APC_2",
			"unitOfMeasureQuantity": "500",
			"unitOfMeasure": "CAR",
			"length": "500.000",
			"width": "60.000",
			"height": "225.000",
			"weight": "1000.000",
			"identifiers": [
				{
					"label": "GTIN",
					"allowedValues": [
						{
							"value": ""
						}
					]
				}
			]
		}
	],
	"messageType": "product",
	"mode": "MANAGE"
}

 

As per my requirement, I have to remove the double quotes from the fileds UnitOfMeasureQuantity, length, width, height, and weight, value. 

alternateUOM also same from the fileds UnitOfMeasureQuantity, length, width, height, and weight, value. 

Below are the expected fields values, and the 3 input files mentioned above are also accepted at the same.

 

{
    "timestamp": "2024-03-07T07:04:49-05:00",
    "productID": "DVCL-153PH-WHC",
    "unitOfMeasureQuantity": 1,
    "unitOfMeasure": "EA",
    "dimensionUnitOfMeasure": "IN",
    "weightUnitOfMeasure": "LB",
    "description": "DIVA 150W LED 3WY",
    "length": 2.5,
    "width": 4.0,
    "height": 6.0,
    "weight": 0.27,
    "identifiers": [
        {
            "label": "GTIN",
            "allowedValues": [
                {
                    "value": "0027557003391"
                }
            ]
        }
    ],
    "alternateUOM": [
        {
            "productID": "DVCL-153PH-WHC",
            "unitOfMeasureQuantity": 3,
            "unitOfMeasure": "IP",
            "length": 4.0,
            "width": 7.5,
            "height": 6.5,
            "weight": 1.0,
            "identifiers": [
                {
                    "label": "GTIN",
                    "allowedValues": [
                        {
                            "value": "30027557003392"
                        }
                    ]
                }
            ]
        },
        {
            "productID": "DVCL-153PH-WHC",
            "unitOfMeasureQuantity": 48,
            "unitOfMeasure": "CA",
            "length": 25.5,
            "width": 15.3,
            "height": 8.9,
            "weight": 17.14,
            "identifiers": [
                {
                    "label": "GTIN",
                    "allowedValues": [
                        {
                            "value": "50027557003396"
                        }
                    ]
                }
            ]
        }
    ],
    "messageType": "product",
    "mode": "MANAGE"
}

 

I've taken alook at the blogs, but it doesn't work out for my requirement.

I require your assistance in fixing the issue urgently and archiving the requirement.

Thanks in advance.

Regards,

Thouheed 

 

Accepted Solutions (1)

Accepted Solutions (1)

surender_durgam
Explorer
0 Kudos

Hi MDThouheed,
Please try this groovy code, I hope it's going to help you. 

Code
---- 
import com.sap.gateway.ip.core.customdev.util.Message;
import java.util.HashMap;
import groovy.json.*
def Message processData(Message message) {

def body = message.getBody(java.lang.String);
def object = new JsonSlurper().parseText(body)

object.length = object.length.toDouble()
object.width = object.width.toDouble()
object.height = object.height.toDouble()
object.weight = object.weight.toDouble()


object.alternateUOM.each{alternateUOM->
alternateUOM.unitOfMeasureQuantity = alternateUOM.unitOfMeasureQuantity.toDouble().intValue()
alternateUOM.length = alternateUOM.length.toDouble()
alternateUOM.width = alternateUOM.width.toDouble()
alternateUOM.height = alternateUOM.height.toDouble()
alternateUOM.weight = alternateUOM.weight.toDouble()
}

message.setBody(JsonOutput.toJson(object));
return message;
}

Regards,
Surender D.

MDThouheed
Discoverer
0 Kudos

Hi Surender,

Your quick response is appreciated. The Code is working fine. I've edited and added one more field and archived the requirement.

import com.sap.gateway.ip.core.customdev.util.Message;
import java.util.HashMap;
import groovy.json.*
def Message processData(Message message) {

def body = message.getBody(java.lang.String);
def object = new JsonSlurper().parseText(body)

object.unitOfMeasureQuantity = object.unitOfMeasureQuantity.toDouble().intValue()
object.length = object.length.toDouble()
object.width = object.width.toDouble()
object.height = object.height.toDouble()
object.weight = object.weight.toDouble()


object.alternateUOM.each{alternateUOM->
alternateUOM.unitOfMeasureQuantity = alternateUOM.unitOfMeasureQuantity.toDouble().intValue()
alternateUOM.length = alternateUOM.length.toDouble()
alternateUOM.width = alternateUOM.width.toDouble()
alternateUOM.height = alternateUOM.height.toDouble()
alternateUOM.weight = alternateUOM.weight.toDouble()
}

message.setBody(JsonOutput.toJson(object));
return message;
}

Regards,

Md Thouheed.

Answers (1)

Answers (1)

surender_durgam
Explorer
0 Kudos

Hi MDThouheed,
Please try this groovy code, hope it's going to help you. 

Code
---- 
import com.sap.gateway.ip.core.customdev.util.Message;
import java.util.HashMap;
import groovy.json.*
def Message processData(Message message) {

def body = message.getBody(java.lang.String);
def object = new JsonSlurper().parseText(body)

object.length = object.length.toDouble()
object.width = object.width.toDouble()
object.height = object.height.toDouble()
object.weight = object.weight.toDouble()


object.alternateUOM.each{alternateUOM->
alternateUOM.unitOfMeasureQuantity = alternateUOM.unitOfMeasureQuantity.toDouble().intValue()
alternateUOM.length = alternateUOM.length.toDouble()
alternateUOM.width = alternateUOM.width.toDouble()
alternateUOM.height = alternateUOM.height.toDouble()
alternateUOM.weight = alternateUOM.weight.toDouble()
}

message.setBody(JsonOutput.toJson(object));
return message;
}

Regards,
Surender D.