A javascript library to map JSON objects.
npm install json-mapping-jsA useful helper to map data from a json object on Node.jS environments.
- Installation
- Node.js
- Usage
- Output types
- Object
- XML
- Mapping
- Mapping samples
1. Install module:
npm install json-mapping --save
2. Then try it.
javascript
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⬆
$3
Runs the json mapper function passing the type 'object' as an argument to map your json data.
`javascript
import jsonMapping from 'json-mapping';const jsonMapper = jsonMapping(someJsonInput);
const object = jsonMapper({ type: 'object' });
`$3
Runs the json mapper function passing the type 'xml' and a mapping array as arguments to map your json data.
`javascript
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.
`json
{
"origin": "!!extension_attributes.bold_order_comment",
"target": "CommentLine.CustomerPoLine"
}
`The 'x' character assumes the origin 'items' will be a iterable array.
`json
{
"origin": "items.x.name",
"target": "Customers.CustomerName"
}
`The convert_function property will be executed as a javascript function receiving the value as a parameter.
`json
{
"origin": "customer.born_date",
"target": "Header.CustomerAge",
"convert_function": "return new Date().getFullYear() - new Date(value).getFullYear()"
}
``