const Ditto = require('json-ditto');
// This is valid JSON mapping file that maps to the mapping rules below
const myCustomMappings = {
"name" : "firstName",
"default_name" : "nonExistingProperty||>>this_should_be_the_firstName",
"nickname" : "nickname||>>nickname_not_found",
"isNickNameFound" : "nickname||>>%false",
"isDynamicDefault" : "nickname||firstName",
"fullName" : "@concatName(firstName|lastName)",
"fullNameDefault" : "@concatName(firstName|*!fullName)",
"fullNameDefaultHardcoded": "@concatName(firstName|lastName|*>>default)",
"completeName" : "@concatName(firstName|!fullName)",
"displayName" : "!fullName",
"email": {
"value": "email"
},
"links": "links",
"social_links": [{
"output" : [],
"innerDocument": "!links",
"required": ["value"],
"mappings" : {
"value": "$value",
"type": ">>test",
"order": "$key",
"social": ">>%true"
}
},{
"output" : [],
"innerDocument": "social",
"required": ["value"],
"mappings" : {
"value" : "value",
"service": "service",
"type": ">>social"
}
}],
"website_addresses_keyless": {
"output": [],
"innerDocument": "linksv2.values",
"prerequisite": "!!innerResult.value",
"mappings": {
"value": "value??type#==#>>website",
"type": ">>other",
}
},
"website_addresses": {
"output": {},
"innerDocument": "linksv2.values",
"key": "id",
"prerequisite": "!!innerResult.value && !!innerResult.keys && !!innerResult.keys.length",
"mappings": {
"value": "value??keys[0]#==#>>f5e32a6faaa7ead6ba201e8fa25733ee",
"type": ">>other",
"keys": "keys"
}
},
"social_media_addresses": {
"output": [],
"innerDocument": "linksv2.values",
"prerequisite": "!!innerResult.value",
"mappings": {
"value": "value??type#==#>>social"
}
},
"messaging": {
"output": [],
"innerDocument": "linksv2.values",
"prerequisite": "!!innerResult.value",
"mappings": {
"service": "@getLinkService(value|service)",
"type" : "@getLinkType(value|@getLinkService(value,service))",
"value" : "@cleanURI(value|@getLinkType(value,@getLinkService(value,service)))??@getLinkType(value|@getLinkService(value,service))#==#>>messaging"
}
},
"social_links_objects": {
"output" : {},
"innerDocument": "!links",
"key": "@generateId($value)",
"mappings" : {
"value": "$value"
}
},
"experience_primary": {
"values": {
"output" : {},
"innerDocument": "!",
"key" : "@generateId(title|company)",
"mappings" : {
"id" : "@generateId(title|company)",
"role" : "title",
"organisationName": "company"
}
}
},
"experience": {
"output": [],
"innerDocument": "work",
"mappings": {
"name" : "companyName",
"role" : "title",
"startDate": "@parseDate(startDate)",
"current" : "current"
}
},
"primaryExperience": "!experience[0]",
"primaryRole": "!experience[0].role",
"experiences": {
"output": [],
"innerDocument": "work",
"value": "companyName"
},
"experience_object": {
"values": {
"output": {},
"innerDocument": "work",
"key": "@generateId(companyName|title)",
"mappings": {
"id": "@generateId(companyName|title)",
"name": "companyName",
"role": "title",
"startDate": "startDate",
"current": "current"
}
}
},
"education": {
"output": [],
"innerDocument": "json.education",
"mappings": {
"universityName": "$key",
"degree": "degree",
"location": "location"
}
},
"education_object": {
"output": {},
"key": "@generateId($key|degree)",
"innerDocument": "json.education",
"mappings": {
"degree": "degree",
"location": "location",
"universityName": "universityName"
}
},
"primaryPhoto": "@createURL(>>http://photo.com/|!fullName)"
};
const sample = {
"firstName" : "Ahmad",
"lastName": "Assaf",
"email" : "ahmad.a.assaf@gmail.com",
"title": "Data Scientist",
"company": "Beamery",
"links": [
"http://a.com",
"http://b.com",
"http://c.com"
],
"linksv2" : {
"values" : {
"54b931981f03e4416a53c3096a32b134" : {
"id" : "54b931981f03e4416a53c3096a32b134",
"value" : "https://twitter.com/ahmadaassaf",
"service" : "twitter",
"keys": ["54b931981f03e4416a53c3096a32b134", "13efd8b0-4a7e-4201-8b6f-ceff2599117a"],
"type" : "social"
},
"54b931981f03e4416a53c3096a32b135" : {
"id" : "54b931981f03e4416a53c3096a32b134",
"value" : "https://twitter.com/ahmadaassaf",
"service" : "twitter",
"keys": ["54b931981f03e4416a53c3096a32b135", "13efd8b0-4a7e-4201-8b6f-ceff2599117a"],
"type" : "social"
},
"e9a5737987d042c6da01f3e740f3f7f7" : {
"id" : "e9a5737987d042c6da01f3e740f3f7f7",
"value" : "https://gravatar.com/ahmadassaf",
"service" : "gravatar",
"keys": ["e9a5737987d042c6da01f3e740f3f7f7", "edc1e66e-6b63-417f-9cdb-ba94bc3ff011"],
"type" : "website"
},
"f5e32a6faaa7ead6ba201e8fa25733ee" : {
"id" : "f5e32a6faaa7ead6ba201e8fa25733ee",
"value" : "http://klout.com/ahmadassaf",
"service" : "klout",
"keys": ["f5e32a6faaa7ead6ba201e8fa25733ee"],
"type" : "website"
},
"e960af6e-c99f-4981-81b3-0b09d99d7354" : {
"id" : "1d87333c-5c60-4c5b-bd3f-611fc38db771",
"value" : "ahmad.a.assaf",
"service" : "skype",
"keys": ["2e3a0ccf-1b27-4435-bfc8-1f751a7be6bd"]
},
"ce621e6d5d201a8212c3684b2a40c813" : {
"id" : "ce621e6d5d201a8212c3684b2a40c813",
"value" : "http://ahmadassaf.com",
"service" : "seed",
"type" : "website",
"deleted" : true
}
},
"keys" : [
"54b931981f03e4416a53c3096a32b134",
"e9a5737987d042c6da01f3e740f3f7f7",
"f5e32a6faaa7ead6ba201e8fa25733ee",
"ce621e6d5d201a8212c3684b2a40c813"
]
},
"social": [{
"value": "http://github.com/ahmadassaf",
"service": "github"
},{
"value": "http://twitter.com/ahmadaassaf",
"service": "twitter"
},{
"service": "google",
"value": null
}],
"work": [
{
"companyName": "beamery",
"title": "Data Scientist",
"startDate": "2015-08-28",
"current": true
},
{
"companyName": "SAP",
"title": "Associate Researcher",
"current": false
}
],
"json": {
"education" : {
"telecomParisTech" : {
"degree" : "phd",
"location": "france"
},
"st-andrews" : {
"degree": "masters",
"universityName": "University of St.Andrews"
}
}
},
"photo" : "http://a photo .com"
};
// Create a new mapper that will always use the "myCustomMappings" file
const myCustomMapper = new Ditto(myCustomMappings);
// Call the unify function that will read the "documentToBeUnified" and transforms it via the mapping file
myCustomMapper.unify(sample).then((result) => {
console.log(result);
});