Custom Module (node) XML to JSON conversion with optional field name mapping

XML to JSON conversion

Introduction

This module takes an XML file and converts it to a JSON output.
Optionally you can map names within the XML Structure to new names within the JSON sturcture

The NPM

This module can be installed from here

Installation

Install in line with these instructions

###Module Location
The module can be found in the Data Trasformations Server Connect Group

The interface

The extrension has only 2 inputs

Path to the XML file.

This should be placed somewhere in the publuc folder tree

XML Transformations

This allows the mapping of XML elements names to an alternative name

XML file

This should be a valid XML file.
In this tutorial i use the following sample file

<?xml version="1.0" ?>
<items>
  <item id="0001" type="donut">
    <name>Cake</name>
    <ppu>0.55</ppu>
    <batters>
      <batter id="1001">Regular</batter>
      <batter id="1002">Chocolate</batter>
      <batter id="1003">Blueberry</batter>
    </batters>
    <topping id="5001">None</topping>
    <topping id="5002">Glazed</topping>
    <topping id="5005">Sugar</topping>
    <topping id="5006">Sprinkles</topping>
    <topping id="5003">Chocolate</topping>
    <topping id="5004">Maple</topping>
  </item>
  <item id="0002" type="donut2">
    <name>Cake</name>
    <ppu>0.60</ppu>
    <batters>
      <batter id="1001">Regular</batter>
      <batter id="1002">Chocolate</batter>
      <batter id="1003">Blueberry</batter>
    </batters>
    <topping id="5001">None</topping>
    <topping id="5002">Glazed</topping>
    <topping id="5005">Sugar</topping>
    <topping id="5006">Sprinkles</topping>
    <topping id="5003">Chocolate</topping>
    <topping id="5004">Maple</topping>
  </item>
</items>

Parsing this file with no transformations results in an output of:

{
id: "0001",
type: "donut",
name: "Cake",
ppu: "0.55",
batters: {
batter: [
{
value: "Regular",
id: "1001"
},
{
value: "Chocolate",
id: "1002"
},
{
value: "Blueberry",
id: "1003"
}
]
},
topping: [
{
value: "None",
id: "5001"
},
{
value: "Glazed",
id: "5002"
},
{
value: "Sugar",
id: "5005"
},
{
value: "Sprinkles",
id: "5006"
},
{
value: "Chocolate",
id: "5003"
},
{
value: "Maple",
id: "5004"
}
]
},ple",id: "5004"}
]}]}}

Field name mapping

Fieldname mapping is simple. Just add your mapping to the extension transformations list .

in this case i wnt to change "batter" to "battertype"
i must specify the full xml object name i.e. "batters.batter" and change "batters.batter" to "batters.battertype" like this:

outputs:

{
id: "0001",
type: "donut",
name: "Cake",
ppu: "0.55",
batters: { },
topping: [
{
value: "None",
id: "5001"
},
{
value: "Glazed",
id: "5002"
},
{
value: "Sugar",
id: "5005"
},
{
value: "Sprinkles",
id: "5006"
},
{
value: "Chocolate",
id: "5003"
},
{
value: "Maple",
id: "5004"
}
],
battertype: [
{
value: "Regular",
id: "1001"
},
{
value: "Chocolate",
id: "1002"
},
{
value: "Blueberry",
id: "1003"
}
]
},

As always, any issues or questions, please ask

1 Like