Skip to main content

Hello All,

 

I want to remove empty array objects from my response and remove the key completely if it has got no value(key:{})

Here is my Dataweave:

 

%dw 2.0

output application/json skipNullOn = "everywhere"

fun objectfield(aValue) =

{

"en": aValue,

"fr-CA":null,

"nl":null,

"zh-Hans":null

}

---

payload map

{

ItemInfo:{

Language: "EN",

ItemNumber: $.ITM_ITEM_SKU_ID,

ItemName: objectfield($.ITM_NAME_OR_TITLE_TXT),

ItemStatus: $.ITM_PMM_STATUS_TXT,

ItemUPC: $.ITM_UPC_TXT,

ItemCtrlUPC: $.ITM_G_TIN_TXT,

ItemCtrlEAN: $.ITM_EAN_TXT,

ItemShortDescription: objectfield($.ITM_SHORT_DESCRIPTION_DSC),

ItemLongDescription: objectfield($.ITM_LONG_DESCRIPTION_DSC),

ItemSEO: objectfield($.ITM_PMM_SEO_KEYWORDS_TXT),

ItemSEOTitle: objectfield($.ITM_PMM_SEO_TITLE_TXT),

ItemSEODescription: objectfield($.ITM_PMM_SEO_DESCRIPTION_DSC)

},

Specifications:

[

{

SpecificationId: if ($.DS_SST_AFUE_TXT != null )"510AFUE" else null,

SpecificationValue: $.DS_SST_AFUE_TXT

},

{

SpecificationId: if ($.DS_SST_AIR_FLOW_TXT != null )"510AirFlow" else null,

SpecificationValue: $.DS_SST_AIR_FLOW_TXT

},

{

SpecificationId: if ($.DS_SST_AIR_HANDLER_TXT != null )"510AirHandler" else null,

SpecificationValue: $.DS_SST_AIR_HANDLER_TXT

},

{

SpecificationId: if ($.DS_SST_BOXED_HEIGHT_TXT != null )"509BoxedHeight" else null,

SpecificationValue: $.DS_SST_BOXED_HEIGHT_TXT

},

{

SpecificationId: if ($.DS_SST_BOXED_LENGTH_TXT != null )"509BoxedLength" else null,

SpecificationValue: $.DS_SST_BOXED_LENGTH_TXT

},

{

SpecificationId: if ($.DS_SST_BOXED_WEIGHT_TXT != null )"509BoxedWeight" else null,

SpecificationValue: $.DS_SST_BOXED_WEIGHT_TXT

},

{

SpecificationId: if ($.DS_SST_BOXED_WIDTH_TXT != null )"509BoxedWidth" else null,

SpecificationValue: $.DS_SST_BOXED_WIDTH_TXT

}

]

}

 

And here is my response:

 

[

{

"ItemInfo": {

"Language": "EN",

"ItemNumber": "VG7841ET+3008D",

"ItemName": {

"en": "3W1/2NPT1.8 W/3008 4-8#  "

},

"ItemUPC": "78502860999",

"ItemShortDescription": {},

"ItemLongDescription": {},

"ItemSEO": {},

"ItemSEOTitle": {},

"ItemSEODescription": {}

},

"Specifications": [

{},

{},

{},

{},

{

"SpecificationId": "509BoxedHeight",

"SpecificationValue": "50"

},

{},

{

"SpecificationId": "509BoxedWeight",

"SpecificationValue": "325"

},

{

"SpecificationId": "509BoxedWidth",

"SpecificationValue": "36"

}

]

}

]

What i actually want is to remove the keys where the value is just an empty bracket({}) for example: "ItemLongDescription": {}, "ItemSEO": {} from my response,

Also I want the empty brackets to be skipped from Specifications object array. Only the key value pair which as values should appear in inside Specifications, and if all the objects inside Specifications is empty, like this:

Specifications:

[

{},{},{}], then Specifications should also be skipped from the response.

 

Can someone please pitch in some ideas here, I tried few things, but I haven't succeeded so far in this.

1 Antwort
  1. 12. Aug. 2019, 08:26

    Hi @Tanmay Sarkar​ . This is example how you should add conditions:

     

    %dw 2.0

     

    output application/json

     

    var payload = {

    DS_SST_AFUE_TXT : null,

    DS_SST_AIR_FLOW_TXT: "test"

    }

     

    ---

     

    (Specifications:[

    ({

     

    SpecificationId: "510AFUE" ,

    SpecificationValue: "DS_SST_AFUE_TXT"

    }) if (payload.DS_SST_AFUE_TXT != null) ,

     

     

    ({

    SpecificationId: "510AirFlow" ,

    SpecificationValue: "DS_SST_AFUE_TXT"

    }) if (payload.DS_SST_AIR_FLOW_TXT != null)

     

    ]) if (payload.DS_SST_AFUE_TXT != null or payload.DS_SST_AIR_FLOW_TXT != null)

0/9000