![Greenkeeper badge](https://camo.githubusercontent.com/7fdff73eb2250fdc8fcd1190092c12a8c9fd131ba83d5355895c160a0015b93c/68747470733a2f2f6261646765732e677265656e6b65657065722e696f2f76696e63656e746d6f726e6561752f617065782d7075626c6973682d7374617469632d66696c65732e737667)
Change the structure of an existing JSON object with this mapping module.
const mapping = require('json-mapping');
let json = {
"appURL": "localhost",
"object1": {
"bool": true,
"name": "app"
}
};
json = mapping.map(json, [
{
oldKey: "appURL",
newKey: "url"
},
{
oldKey: "object1.bool",
newKey: "object1.enabled"
}
]);
/*
{
"url": "localhost",
"object1": {
"enabled": true,
"name": "app"
}
}
*/
Name |
Type |
Description |
json |
object |
The initial JSON object to be mapped |
mapping* |
array |
An array containing the mapping options |
Name |
Type |
Description |
oldKey |
string |
The old property name to be mapped |
newKey |
string |
The new property name to be mapped |
values** |
array |
An array of mapped values for this property mapping |
dependsOn** |
object |
Determines the value for the newKey based on a condition |
Name |
Type |
Description |
oldValue |
any |
The old value of the property to be mapped |
newValue |
any |
The new value of the property to be mapped |
Name |
Type |
Description |
key |
string |
The key to look for |
if |
any |
The value to evaluate for the key |
ifValue |
any |
When key === if, this will be the value of the newKey |
elseValue |
any |
When key !== if, this will be the value of the newKey |
Name |
Type |
Return |
Description |
map |
function |
object |
Maps a JSON object using mapping options |
See changelog.
MIT © Vincent Morneau