Home > Enterprise >  Search an XML attrbiute and Group it
Search an XML attrbiute and Group it

Time:03-24

I need to search for an XML Attribute Value, group it and form a corresponding JSON element.

Now for example: Label is my AttributeID and I have 2 attributes with Label. (In realtime - it can be 'n' number of times.) Need to group 2 Labels as a single JSON element.

VendorClass, VendorDivision and VendorDept should all come inside Vendor Json attribute. VendorClass attributeValue should be mapped to classCode inside Vendor and other 2 codes should be null. Likewise, VendorDept attributeValue should be mapped to DeptCode., VedorDivision attrbiuteValue mapped to DivisionCode.

O/P JSON (mapping): type - should always be in upper characters - mapped to attributeID, name - in lower character - mapped to "attributeId_attributeValue", id/code - is attribute_value

Below is my i/p XML

<CurrentRule Currency="USD"
    CurrentStatus="ACTIVE"
    Priority="0"
    RuleCategory="Current"
    RuleType="COMBINATION"
    >
    <CurrentRuleTargetAttributeValueList/>
    <CurrentRuleAttributeValueList>
        <CurrentRuleAttributeValue 
            TriggerAttributeID="Label"
            TriggerAttributeValue="10"/>
        <CurrentRuleAttributeValue 
            TriggerAttributeID="Label"
            TriggerAttributeValue="1003"/>
        <CurrentRuleAttributeValue 
        TriggerAttributeID="ABCDCode"
            TriggerAttributeValue="AC"/>
        <CurrentRuleAttributeValue 
            TriggerAttributeID="ABCDCode"
            TriggerAttributeValue="FD"/>
        <CurrentRuleAttributeValue 
            TriggerAttributeID="VendorClass"
            TriggerAttributeValue="00N"/>
        <CurrentRuleAttributeValue
            TriggerAttributeID="VendorDept"
            TriggerAttributeValue="100"/>
        <CurrentRuleAttributeValue
            TriggerAttributeID="VendorDivision"
            TriggerAttributeValue="10"/>
        <CurrentRuleAttributeValue 
            TriggerAttributeID="VendorMarket"
            TriggerAttributeValue="QVC"/>
        <CurrentRuleAttributeValue
            TriggerAttributeID="PriceCode"
            TriggerAttributeValue="FP"/>
        <CurrentRuleAttributeValue 
            TriggerAttributeID="ProductNumber"
            TriggerAttributeValue="A0000"/>
        <CurrentRuleAttributeValue
            TriggerAttributeID="Trader"
            TriggerAttributeValue="1010"/>
        <CurrentRuleAttributeValue
            TriggerAttributeID="Trader"
            TriggerAttributeValue="1046"/>
    </CurrentRuleAttributeValueList>
</CurrentRule>

Expected JSON o/p:

{
    "header": {
        "type": "ORDER",
        "name": "merch-attribute-example",
        "createUser": "admin"
    },
    "filters": {
        "Labels": [
            {
                "type": "LABEL",
                "name": "Label_10",
                "LabelId": 10
            },
            {
                "type": "LABEL",
                "name": "Label_1003",
                "LabelId": 1003
            }
        ],
        "vendor": [
            {
                "type": "VENDOR",
                "name": "vendorclass_00n",
                "divisionCode": null,
                "departmentCode": null,
                "classCode": "00N"
            },
            {
                "type": "VENDOR",
                "name": "vendordept_100",
                "divisionCode": null,
                "departmentCode": "100",
                "classCode": null
            },
            {
                "type": "VENDOR",
                "name": "vendordivision_10",
                "divisionCode": "10",
                "departmentCode": null,
                "classCode": null
            }
        ],
        "abcd": [
            {
                "type": "ABCD",
                "name": "abcdcode_ac",
                "abcdCode": "AC"
            },
            {
                "type": "ABCD",
                "name": "abcdcode_fd",
                "abcdCode": "FD"
            }
        ],
        "priceCodes": [
            {
                "type": "PRICE_CODE",
                "name": "pricecode_fp",
                "code": "FP"
            }
        ],
        "products": [
            {
                "type": "PRODUCT",
                "name": "productnumber_a0000",
                "productNumbers": [
                    "A0000"
                ]
            }
        ],
        "traders": [
            {
                "type": "TRADER",
                "name": "trader_1010",
                "vendorCode": "1010"
            },
            {
                "type": "TRADER",
                "name": "trader_1046",
                "vendorCode": "1046"
            }
        ]
    }
}
enter code here

CodePudding user response:

Based on the rules described this is the best approximation. I'll leave to you how to decide if it is an id or code and the rest of the output that is not clearly explained.

%dw 2.0
output application/json
---
{
    header: {
        "type": "ORDER",
        "name": "merch-attribute-example",
        "createUser": "admin"
    },
    filters: payload.CurrentRule.CurrentRuleAttributeValueList 
        mapObject ((value, key, index) -> 
            {
                attr: key.@
            }
        ) 
        pluck (($$):$)
        groupBy ((item, index) -> item.attr.TriggerAttributeID)

        mapObject ((value1, key1, index1) -> 
            (key1): value1 map 
                {
                    "type": upper(key1), 
                    name: lower(key1)    "_"    $.attr.TriggerAttributeValue,
                    id: $.attr.TriggerAttributeValue
                }
        ) 

}

Output:

{
  "header": {
    "type": "ORDER",
    "name": "merch-attribute-example",
    "createUser": "admin"
  },
  "filters": {
    "Label": [
      {
        "type": "LABEL",
        "name": "label_10",
        "id": "10"
      },
      {
        "type": "LABEL",
        "name": "label_1003",
        "id": "1003"
      }
    ],
    "ABCDCode": [
      {
        "type": "ABCDCODE",
        "name": "abcdcode_AC",
        "id": "AC"
      },
      {
        "type": "ABCDCODE",
        "name": "abcdcode_FD",
        "id": "FD"
      }
    ],
    "VendorClass": [
      {
        "type": "VENDORCLASS",
        "name": "vendorclass_00N",
        "id": "00N"
      }
    ],
    "VendorDept": [
      {
        "type": "VENDORDEPT",
        "name": "vendordept_100",
        "id": "100"
      }
    ],
    "VendorDivision": [
      {
        "type": "VENDORDIVISION",
        "name": "vendordivision_10",
        "id": "10"
      }
    ],
    "VendorMarket": [
      {
        "type": "VENDORMARKET",
        "name": "vendormarket_QVC",
        "id": "QVC"
      }
    ],
    "PriceCode": [
      {
        "type": "PRICECODE",
        "name": "pricecode_FP",
        "id": "FP"
      }
    ],
    "ProductNumber": [
      {
        "type": "PRODUCTNUMBER",
        "name": "productnumber_A0000",
        "id": "A0000"
      }
    ],
    "Trader": [
      {
        "type": "TRADER",
        "name": "trader_1010",
        "id": "1010"
      },
      {
        "type": "TRADER",
        "name": "trader_1046",
        "id": "1046"
      }
    ]
  }
}
  • Related