Need help with parsing XML to JSON

Hi, This is the first time I am connecting with AspDotNetStoreFron portal using WSI. They send the xml in the following format and I cannot use the Celigo XML to JSON parser as it says format not correct. Talked with Celigo support person also and they said it needs to be clean and below are his suggestions

1. &lt and &amp need to be decoded properly.

2. "False" or "True" and need to be lowercase "false" or "true" to work correctly

3. All fields need to be properly enclosed to allow for parsing to JSON without any missing fields

So looks like with this format I cannot convert to JSON with the Celigo's built in parser. I tried to do a Javascript code to clean up and convert. I was able to do the clean up but for converting libraries needs to be imported which I can't do in Celigo. Any help would be greatly appreciated to convert this xml to jSON

"<?xml version=\"1.0\" encoding=\"utf-8\"?><AspDotNetStorefrontImportResult Version=\"7.1\" DateTime=\"8/20/2024 10:18:34 PM\"><GetProduct ID=\"11735\" GUID=\"\"><Product Action=\"Update\" ID=\"11735\" Name=\"OFF-SITE WORK KIT\" SKU=\"MPC1908\" GUID=\"1d920cfc-fcd8-45b0-b025-dc3e774a99f7\"><Name>OFF-SITE WORK KIT</Name><Summary></Summary><Description>Set Includes #2953 Wireless Charging Mouse Pad With Phone Stand, #1343 Jotter With Sticky Notes And Flags, #1333 - 3-Tier Stationary Set, #2496 3-In-1 Xoopar Octo-Charging Cables, #224 .5 Oz. Screen Cleaner And Spray\n&lt;br /&gt;\n&lt;br /&gt;\n&lt;br /&gt;\n&amp;nbsp;As low as 35.49 each @450 qty.&lt;br /&gt;</Description><MiscText></MiscText><Notes></Notes><FroogleDescription></FroogleDescription><SKU>MPC1908</SKU><ManufacturerPartNumber></ManufacturerPartNumber><SE><SEName>off-site-work-kit</SEName><SETitle></SETitle><SEKeywords></SEKeywords><SEDescription></SEDescription><SEAltText></SEAltText></SE><SizeOptionPrompt></SizeOptionPrompt><ColorOptionPrompt></ColorOptionPrompt><ProductType Name=\"Generic Product\" ID=\"1\" GUID=\"b60e6a65-ec2a-4fca-8f16-029bb03729cc\" /><TaxClass Name=\"Goods\" ID=\"1\" GUID=\"ff20a290-6af2-4bf9-a550-217e4b7e342b\" /><SalesPrompt Name=\"On Sale For\" ID=\"1\" GUID=\"511a94c2-a260-4a0a-bd21-42f257da5563\" /><Display><XmlPackage>product.simpleproduct.xml.config</XmlPackage><ColWidth>4</ColWidth><SkinID>0</SkinID><TemplateName></TemplateName></Display><Images><ImageFilenameOverride></ImageFilenameOverride></Images><RelatedProducts></RelatedProducts><UpsellProducts DiscountPercentage=\"0.0000\"></UpsellProducts><RequiresProducts></RequiresProducts><InventoryType><TrackInventoryBySizeAndColor>False</TrackInventoryBySizeAndColor><WarehouseLocation></WarehouseLocation></InventoryType><IsAKit>False</IsAKit><IsSystem>False</IsSystem><ShowBuyButton>False</ShowBuyButton><Published>True</Published><Wholesale>False</Wholesale><RequiresRegistration>False</RequiresRegistration><HidePriceUntilCart>False</HidePriceUntilCart><IsCallToOrder>True</IsCallToOrder><ExcludeFromPriceFeeds>False</ExcludeFromPriceFeeds><RequiresTextOption>False</RequiresTextOption><TextOptionMaxLength>0</TextOptionMaxLength><TextOptionPrompt></TextOptionPrompt><StoreMappings AutoCleanup=\"true\"><Store StoreId=\"1\" /></StoreMappings><Mappings AutoCleanup=\"true\"><Entity EntityType=\"Category\" Name=\"MPC Essentials\" XPath=\"/MPC Essentials\" ID=\"520\" GUID=\"289d47c8-21de-402d-9fe8-8accd58e9976\" DisplayOrder=\"1\" /><Entity EntityType=\"Category\" Name=\"Work from Home\" XPath=\"/MPC Essentials/Work from Home\" ID=\"522\" GUID=\"6f1ceea0-5972-4e68-9174-d6319aea954f\" DisplayOrder=\"1\" /><Entity EntityType=\"Manufacturer\" Name=\"MPC Promotions\" XPath=\"/\" ID=\"1\" GUID=\"3a351ca9-92b2-4c26-8666-d14886321918\" DisplayOrder=\"1\" /></Mappings><ExtensionData></ExtensionData><ExtensionData2></ExtensionData2><ExtensionData3></ExtensionData3><ExtensionData4></ExtensionData4><ExtensionData5></ExtensionData5><Variants AutoCleanup=\"true\"><Variant <Name></Name><Description>&lt;span&gt;\n&lt;h2&gt;&lt;/h2&gt;\n&lt;/span&gt;</Description><SE><SEName></SEName><SEKeywords></SEKeywords><SEDescription></SEDescription></SE><FroogleDescription></FroogleDescription><ProductID>11735</ProductID><SKUSuffix></SKUSuffix><ManufacturerPartNumber></ManufacturerPartNumber><GTIN></GTIN><Price>35.4900</Price><SalePrice>0</SalePrice><Weight>0.5000</Weight><MSRP>0</MSRP><Cost>0</Cost><Points>0</Points><Dimensions></Dimensions><Inventory>9999</Inventory><DisplayOrder>1</DisplayOrder><Notes></Notes><IsTaxable>True</IsTaxable><IsShipSeparately>False</IsShipSeparately><IsDownload>False</IsDownload><DownloadLocation></DownloadLocation><FreeShipping>0</FreeShipping><Published>True</Published><Wholesale>False</Wholesale><IsRecurring>False</IsRecurring><RecurringInterval>1</RecurringInterval><RecurringIntervalType>-1</RecurringIntervalType><RestrictedQuantities></RestrictedQuantities><MinimumQuantity>0</MinimumQuantity><Images><ImageFilenameOverride></ImageFilenameOverride></Images><CustomerEntersPrice>False</CustomerEntersPrice><CustomerEntersPricePrompt></CustomerEntersPricePrompt><ExtensionData></ExtensionData><ExtensionData2></ExtensionData2><ExtensionData3></ExtensionData3><ExtensionData4></ExtensionData4><ExtensionData5></ExtensionData5><Sizes></Sizes><Colors></Colors></Variant></Variants></Product></GetProduct></AspDotNetStorefrontImportResult>"

0

Comments

7 comments
Date Votes
  • This looks like how xml looks when placed inside of a json/js variable. (everything is escaped) 

    AspDotNetStoreFront API should respond with regular xml, can you post a screenshot of your export?

    0
  • Here is what I get(Cannot upload file for some reason). Here is part of the data

    <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"><s:Body><DoItUsernamePwdResponse xmlns="http://www.aspdotnetstorefront.com/"><DoItUsernamePwdResult>&lt;?xml version="1.0" encoding="utf-8"?&gt;&lt;AspDotNetStorefrontImportResult Version="7.1" DateTime="8/23/2024 12:29:39 PM"&gt;&lt;GetProduct ID="11735" GUID=""&gt;&lt;Product Action="Update" ID="11735" Name="OFF-SITE WORK KIT" SKU="MPC1908" GUID="1d920cfc-fcd8-45b0-b025-dc3e774a99f7"&gt;&lt;Name&gt;OFF-SITE WORK KIT&lt;/Name&gt;&lt;Summary&gt;&lt;/Summary&gt;&lt;Description&gt;Set Includes #2953 Wireless Charging Mouse Pad With Phone Stand, #1343 Jotter With Sticky Notes And Flags, #1333 - 3-Tier Stationary Set, #2496 3-In-1 Xoopar Octo-Charging Cables, #224 .5 Oz. Screen Cleaner And Spray&#xD;
    &amp;lt;br /&amp;gt;&#xD;
    &amp;lt;br /&amp;gt;&#xD;
    &amp;lt;br /&amp;gt;&#xD;
    &amp;amp;nbsp;As low as 35.49 each @450 qty.&amp;lt;br /&amp;gt;&#xD;

     

    0
  • Bindu Joseph this is a pretty ugly response from an api. I would suggest you don't accept it as XML and instead accept as "Plain text" so that we don't put it through any parser. From there you have a couple options. 

    1. You could have a preSavePage script that parses it. This would be a bit heavy as you'd essentially be recreating the XML parser in a script.
    2. You could clean the data with a preSavePage script so that it becomes valid XML. After cleaning it, you can send it to a webhook sitting on another flow or drop it to a file storage where the next flow picks up from that storage.
    0
  • Thank you Tyler. So you are suggesting to create a parser in the script so that I don't need to use any library? 

    0
  • Bindu Joseph yeah something like this. I just used AI for this and didn't change much, but it highlights what you could do. You could also have a more simple script that just runs the replace statements, where that gives you valid XML, then you send that valid XML to another flow.

    Input:

    {
      "data": [
        {
          "text": "<?xml version=\"1.0\" encoding=\"utf-8\"?><AspDotNetStorefrontImportResult Version=\"7.1\" DateTime=\"8/20/2024 10:18:34 PM\"><GetProduct ID=\"11735\" GUID=\"\"><Product Action=\"Update\" ID=\"11735\" Name=\"OFF-SITE WORK KIT\" SKU=\"MPC1908\" GUID=\"1d920cfc-fcd8-45b0-b025-dc3e774a99f7\"><Name>OFF-SITE WORK KIT</Name><Summary></Summary><Description>Set Includes #2953 Wireless Charging Mouse Pad With Phone Stand, #1343 Jotter With Sticky Notes And Flags, #1333 - 3-Tier Stationary Set, #2496 3-In-1 Xoopar Octo-Charging Cables, #224 .5 Oz. Screen Cleaner And Spray\n&lt;br /&gt;\n&lt;br /&gt;\n&lt;br /&gt;\n&amp;nbsp;As low as 35.49 each @450 qty.&lt;br /&gt;</Description><MiscText></MiscText><Notes></Notes><FroogleDescription></FroogleDescription><SKU>MPC1908</SKU><ManufacturerPartNumber></ManufacturerPartNumber><SE><SEName>off-site-work-kit</SEName><SETitle></SETitle><SEKeywords></SEKeywords><SEDescription></SEDescription><SEAltText></SEAltText></SE><SizeOptionPrompt></SizeOptionPrompt><ColorOptionPrompt></ColorOptionPrompt><ProductType Name=\"Generic Product\" ID=\"1\" GUID=\"b60e6a65-ec2a-4fca-8f16-029bb03729cc\" /><TaxClass Name=\"Goods\" ID=\"1\" GUID=\"ff20a290-6af2-4bf9-a550-217e4b7e342b\" /><SalesPrompt Name=\"On Sale For\" ID=\"1\" GUID=\"511a94c2-a260-4a0a-bd21-42f257da5563\" /><Display><XmlPackage>product.simpleproduct.xml.config</XmlPackage><ColWidth>4</ColWidth><SkinID>0</SkinID><TemplateName></TemplateName></Display><Images><ImageFilenameOverride></ImageFilenameOverride></Images><RelatedProducts></RelatedProducts><UpsellProducts DiscountPercentage=\"0.0000\"></UpsellProducts><RequiresProducts></RequiresProducts><InventoryType><TrackInventoryBySizeAndColor>False</TrackInventoryBySizeAndColor><WarehouseLocation></WarehouseLocation></InventoryType><IsAKit>False</IsAKit><IsSystem>False</IsSystem><ShowBuyButton>False</ShowBuyButton><Published>True</Published><Wholesale>False</Wholesale><RequiresRegistration>False</RequiresRegistration><HidePriceUntilCart>False</HidePriceUntilCart><IsCallToOrder>True</IsCallToOrder><ExcludeFromPriceFeeds>False</ExcludeFromPriceFeeds><RequiresTextOption>False</RequiresTextOption><TextOptionMaxLength>0</TextOptionMaxLength><TextOptionPrompt></TextOptionPrompt><StoreMappings AutoCleanup=\"true\"><Store StoreId=\"1\" /></StoreMappings><Mappings AutoCleanup=\"true\"><Entity EntityType=\"Category\" Name=\"MPC Essentials\" XPath=\"/MPC Essentials\" ID=\"520\" GUID=\"289d47c8-21de-402d-9fe8-8accd58e9976\" DisplayOrder=\"1\" /><Entity EntityType=\"Category\" Name=\"Work from Home\" XPath=\"/MPC Essentials/Work from Home\" ID=\"522\" GUID=\"6f1ceea0-5972-4e68-9174-d6319aea954f\" DisplayOrder=\"1\" /><Entity EntityType=\"Manufacturer\" Name=\"MPC Promotions\" XPath=\"/\" ID=\"1\" GUID=\"3a351ca9-92b2-4c26-8666-d14886321918\" DisplayOrder=\"1\" /></Mappings><ExtensionData></ExtensionData><ExtensionData2></ExtensionData2><ExtensionData3></ExtensionData3><ExtensionData4></ExtensionData4><ExtensionData5></ExtensionData5><Variants AutoCleanup=\"true\"><Variant <Name></Name><Description>&lt;span&gt;\n&lt;h2&gt;&lt;/h2&gt;\n&lt;/span&gt;</Description><SE><SEName></SEName><SEKeywords></SEKeywords><SEDescription></SEDescription></SE><FroogleDescription></FroogleDescription><ProductID>11735</ProductID><SKUSuffix></SKUSuffix><ManufacturerPartNumber></ManufacturerPartNumber><GTIN></GTIN><Price>35.4900</Price><SalePrice>0</SalePrice><Weight>0.5000</Weight><MSRP>0</MSRP><Cost>0</Cost><Points>0</Points><Dimensions></Dimensions><Inventory>9999</Inventory><DisplayOrder>1</DisplayOrder><Notes></Notes><IsTaxable>True</IsTaxable><IsShipSeparately>False</IsShipSeparately><IsDownload>False</IsDownload><DownloadLocation></DownloadLocation><FreeShipping>0</FreeShipping><Published>True</Published><Wholesale>False</Wholesale><IsRecurring>False</IsRecurring><RecurringInterval>1</RecurringInterval><RecurringIntervalType>-1</RecurringIntervalType><RestrictedQuantities></RestrictedQuantities><MinimumQuantity>0</MinimumQuantity><Images><ImageFilenameOverride></ImageFilenameOverride></Images><CustomerEntersPrice>False</CustomerEntersPrice><CustomerEntersPricePrompt></CustomerEntersPricePrompt><ExtensionData></ExtensionData><ExtensionData2></ExtensionData2><ExtensionData3></ExtensionData3><ExtensionData4></ExtensionData4><ExtensionData5></ExtensionData5><Sizes></Sizes><Colors></Colors></Variant></Variants></Product></GetProduct></AspDotNetStorefrontImportResult>"
        }
      ],
      "errors": [],
      "_exportId": "66ce7e1bf7c3628b407d80fc",
      "_connectionId": "63628238e6dfe03093953a68",
      "_flowId": "66ce7e1b3a88959f61a68f04",
      "pageIndex": 0,
      "settings": {
        "integration": {},
        "flowGrouping": {},
        "flow": {},
        "export": {},
        "connection": {},
        "iClient": {}
      },
      "testMode": true
    }

     

    Output:

    {
      "data": [
        {
          "text": "<?xml version=\"1.0\" encoding=\"utf-8\"?><AspDotNetStorefrontImportResult Version=\"7.1\" DateTime=\"8/20/2024 10:18:34 PM\"><GetProduct ID=\"11735\" GUID=\"\"><Product Action=\"Update\" ID=\"11735\" Name=\"OFF-SITE WORK KIT\" SKU=\"MPC1908\" GUID=\"1d920cfc-fcd8-45b0-b025-dc3e774a99f7\"><Name>OFF-SITE WORK KIT</Name><Summary></Summary><Description>Set Includes #2953 Wireless Charging Mouse Pad With Phone Stand, #1343 Jotter With Sticky Notes And Flags, #1333 - 3-Tier Stationary Set, #2496 3-In-1 Xoopar Octo-Charging Cables, #224 .5 Oz. Screen Cleaner And Spray\n<br />\n<br />\n<br />\n&nbsp;As low as 35.49 each @450 qty.<br /></Description><MiscText></MiscText><Notes></Notes><FroogleDescription></FroogleDescription><SKU>MPC1908</SKU><ManufacturerPartNumber></ManufacturerPartNumber><SE><SEName>off-site-work-kit</SEName><SETitle></SETitle><SEKeywords></SEKeywords><SEDescription></SEDescription><SEAltText></SEAltText></SE><SizeOptionPrompt></SizeOptionPrompt><ColorOptionPrompt></ColorOptionPrompt><ProductType Name=\"Generic Product\" ID=\"1\" GUID=\"b60e6a65-ec2a-4fca-8f16-029bb03729cc\" /><TaxClass Name=\"Goods\" ID=\"1\" GUID=\"ff20a290-6af2-4bf9-a550-217e4b7e342b\" /><SalesPrompt Name=\"On Sale For\" ID=\"1\" GUID=\"511a94c2-a260-4a0a-bd21-42f257da5563\" /><Display><XmlPackage>product.simpleproduct.xml.config</XmlPackage><ColWidth>4</ColWidth><SkinID>0</SkinID><TemplateName></TemplateName></Display><Images><ImageFilenameOverride></ImageFilenameOverride></Images><RelatedProducts></RelatedProducts><UpsellProducts DiscountPercentage=\"0.0000\"></UpsellProducts><RequiresProducts></RequiresProducts><InventoryType><TrackInventoryBySizeAndColor>False</TrackInventoryBySizeAndColor><WarehouseLocation></WarehouseLocation></InventoryType><IsAKit>False</IsAKit><IsSystem>False</IsSystem><ShowBuyButton>False</ShowBuyButton><Published>True</Published><Wholesale>False</Wholesale><RequiresRegistration>False</RequiresRegistration><HidePriceUntilCart>False</HidePriceUntilCart><IsCallToOrder>True</IsCallToOrder><ExcludeFromPriceFeeds>False</ExcludeFromPriceFeeds><RequiresTextOption>False</RequiresTextOption><TextOptionMaxLength>0</TextOptionMaxLength><TextOptionPrompt></TextOptionPrompt><StoreMappings AutoCleanup=\"true\"><Store StoreId=\"1\" /></StoreMappings><Mappings AutoCleanup=\"true\"><Entity EntityType=\"Category\" Name=\"MPC Essentials\" XPath=\"/MPC Essentials\" ID=\"520\" GUID=\"289d47c8-21de-402d-9fe8-8accd58e9976\" DisplayOrder=\"1\" /><Entity EntityType=\"Category\" Name=\"Work from Home\" XPath=\"/MPC Essentials/Work from Home\" ID=\"522\" GUID=\"6f1ceea0-5972-4e68-9174-d6319aea954f\" DisplayOrder=\"1\" /><Entity EntityType=\"Manufacturer\" Name=\"MPC Promotions\" XPath=\"/\" ID=\"1\" GUID=\"3a351ca9-92b2-4c26-8666-d14886321918\" DisplayOrder=\"1\" /></Mappings><ExtensionData></ExtensionData><ExtensionData2></ExtensionData2><ExtensionData3></ExtensionData3><ExtensionData4></ExtensionData4><ExtensionData5></ExtensionData5><Variants AutoCleanup=\"true\"><Variant <Name></Name><Description><span>\n<h2></h2>\n</span></Description><SE><SEName></SEName><SEKeywords></SEKeywords><SEDescription></SEDescription></SE><FroogleDescription></FroogleDescription><ProductID>11735</ProductID><SKUSuffix></SKUSuffix><ManufacturerPartNumber></ManufacturerPartNumber><GTIN></GTIN><Price>35.4900</Price><SalePrice>0</SalePrice><Weight>0.5000</Weight><MSRP>0</MSRP><Cost>0</Cost><Points>0</Points><Dimensions></Dimensions><Inventory>9999</Inventory><DisplayOrder>1</DisplayOrder><Notes></Notes><IsTaxable>True</IsTaxable><IsShipSeparately>False</IsShipSeparately><IsDownload>False</IsDownload><DownloadLocation></DownloadLocation><FreeShipping>0</FreeShipping><Published>True</Published><Wholesale>False</Wholesale><IsRecurring>False</IsRecurring><RecurringInterval>1</RecurringInterval><RecurringIntervalType>-1</RecurringIntervalType><RestrictedQuantities></RestrictedQuantities><MinimumQuantity>0</MinimumQuantity><Images><ImageFilenameOverride></ImageFilenameOverride></Images><CustomerEntersPrice>False</CustomerEntersPrice><CustomerEntersPricePrompt></CustomerEntersPricePrompt><ExtensionData></ExtensionData><ExtensionData2></ExtensionData2><ExtensionData3></ExtensionData3><ExtensionData4></ExtensionData4><ExtensionData5></ExtensionData5><Sizes></Sizes><Colors></Colors></Variant></Variants></Product></GetProduct></AspDotNetStorefrontImportResult>",
          "parsed": {
            "?xml": {
              "@attributes": {
                "version": "1.0",
                "encoding": "utf-8"
              }
            },
            "Description": [
              {},
              {}
            ],
            "SKU": "MPC1908",
            "SE": [
              {},
              {}
            ],
            "ProductType": {
              "@attributes": {
                "Name": "Generic Product",
                "ID": "1",
                "GUID": "b60e6a65-ec2a-4fca-8f16-029bb03729cc"
              }
            },
            "ColWidth": "4",
            "SkinID": "0",
            "Images": [
              {},
              {}
            ],
            "UpsellProducts": {
              "@attributes": {
                "DiscountPercentage": "0.0000"
              }
            },
            "InventoryType": {},
            "IsAKit": "False",
            "IsSystem": "False",
            "ShowBuyButton": "False",
            "Published": [
              "True",
              "True"
            ],
            "Wholesale": [
              "False",
              "False"
            ],
            "RequiresRegistration": "False",
            "HidePriceUntilCart": "False",
            "IsCallToOrder": "True",
            "ExcludeFromPriceFeeds": "False",
            "RequiresTextOption": "False",
            "TextOptionMaxLength": "0",
            "StoreMappings": {
              "@attributes": {
                "AutoCleanup": "true"
              }
            },
            "Mappings": {
              "@attributes": {
                "AutoCleanup": "true"
              }
            },
            "Variants": {
              "@attributes": {
                "AutoCleanup": "true"
              }
            },
            "ProductID": "11735",
            "Price": "35.4900",
            "SalePrice": "0",
            "Weight": "0.5000",
            "MSRP": "0",
            "Cost": "0",
            "Points": "0",
            "Inventory": "9999",
            "DisplayOrder": "1",
            "IsTaxable": "True",
            "IsShipSeparately": "False",
            "IsDownload": "False",
            "FreeShipping": "0",
            "IsRecurring": "False",
            "RecurringInterval": "1",
            "RecurringIntervalType": "-1",
            "MinimumQuantity": "0",
            "CustomerEntersPrice": "False"
          }
        }
      ],
      "errors": [],
      "abort": false,
      "newErrorsAndRetryData": []
    }

     

    Script:

    function preSavePage(options) {
        for (let d of options.data) {
            if (d.text && typeof d.text === 'string') {
                // Replace HTML entities and preserve new lines
                d.text = d.text.replace(/&lt;/g, '<')
                    .replace(/&gt;/g, '>')
                    .replace(/&amp;/g, '&')
                    .replace(/&quot;/g, '"')
                    .replace(/&apos;/g, "'")
                    .replace(/\\n/g, '\n'); // Replace escaped newline characters with actual newlines

                // Convert the decoded XML string to a JSON object
                d.parsed = xmlToJson(d.text);
            } else {
                console.error('Warning: d.text is undefined or not a string.');
            }
        }

        return {
            data: options.data,
            errors: options.errors,
            abort: false,
            newErrorsAndRetryData: []
        };
    }

    function xmlToJson(xml) {
        const parseXmlNode = (node) => {
            let obj = {};
            const nodeNameMatch = node.match(/^<([^\s/>]+)(.*?)>/);
            if (!nodeNameMatch) return obj;

            const tagName = nodeNameMatch[1];
            const attributes = nodeNameMatch[2].trim();
            const attrObj = {};

            attributes.replace(/(\w+)="(.*?)"/g, (_, key, value) => {
                attrObj[key] = value;
            });

            const innerXml = node.substring(nodeNameMatch[0].length, node.lastIndexOf(`</${tagName}>`));
            
            if (innerXml.trim()) {
                obj[tagName] = innerXml.includes('<') ? xmlToJson(innerXml) : innerXml;
            }

            if (Object.keys(attrObj).length > 0) {
                obj[tagName] = { ...obj[tagName], "@attributes": attrObj };
            }

            return obj;
        };

        const result = {};
        const nodes = xml.match(/<[^/][^>]*>.*?<\/[^>]+>/gs) || [];

        nodes.forEach(node => {
            const parsedNode = parseXmlNode(node);
            const key = Object.keys(parsedNode)[0];
            if (result[key]) {
                if (!Array.isArray(result[key])) {
                    result[key] = [result[key]];
                }
                result[key].push(parsedNode[key]);
            } else {
                result[key] = parsedNode[key];
            }
        });

        return result;
    }
    0
  • Bindu Joseph the XML itself is also invalid. I see it's missing an ending character for the Variant tag. So overall you'll need a script to clean this up, then send through a parser or do like above.

    0
  • This is awesome!. Thank you so much Tyler for this. Really appreciate it

    0

Please sign in to leave a comment.

 

Didn't find what you were looking for?

New post