A useful helper to map data from a json object on Node.jS environments.
Installation⬆
Node.js⬆
-
Install module:
npm install json-mapping --save
-
Then try it.
Usage⬆
import jsonMapping from 'json-mapping';
const jsonMapper = jsonMapping(someJsonInput); // input the json data
const object = jsonMapper({ type: 'object' }); // convert the inputed json data to javascript literal object
const xml = jsonMapper({ type: 'xml', mapping, prettify: true }); // convert the inputed json data to xml based on a mapping array
Output types⬆
Object⬆
Runs the json mapper function passing the type 'object' as an argument to map your json data.
import jsonMapping from 'json-mapping';
const jsonMapper = jsonMapping(someJsonInput);
const object = jsonMapper({ type: 'object' });
XML⬆
Runs the json mapper function passing the type 'xml' and a mapping array as arguments to map your json data.
import jsonMapping from 'json-mapping';
const jsonMapper = jsonMapping(someJsonInput);
const xml = jsonMapper({ type: 'xml', mapping, prettify: true });
Mapping⬆
argument | type | required | description |
---|---|---|---|
target | string | true | XML target tag you want to fill |
origin | string | false | JSON path where you want to get data from |
static_value | any | false | A static value to the XML target tag |
convert_function | string | false | Function to convert the origin data |
Mapping samples⬆
The '!!' characters assumes the target will be setted only if value isn't undefined.
{
"origin": "!!extension_attributes.bold_order_comment",
"target": "CommentLine.CustomerPoLine"
}
The 'x' character assumes the origin 'items' will be a iterable array.
{
"origin": "items.x.name",
"target": "Customers.CustomerName"
}
The convert_function property will be executed as a javascript function receiving the value as a parameter.
{
"origin": "customer.born_date",
"target": "Header.CustomerAge",
"convert_function": "return new Date().getFullYear() - new Date(value).getFullYear()"
}