Structure flattening/consolidation
matbrady opened this issue · comments
Was wondering if you knew of a way to express flattening and consolidating a JSON data structure with how your script is currently implemented. Instead of mapping actual data, the goal is to essentially map schema. For example, I have a current schema which includes keys to express the types of nested data.
original schema
{
history: {
approved: {
modification: [
{
dateTime: "",
userDisplayName: "",
userDn: ""
}
]
},
broughtUnderEdit: {
modification: [
{
dateTime: "",
userDisplayName: "",
userDn: ""
}
]
},
changed: {
modification: [
{
dateTime: "",
userDisplayName: "",
userDn: ""
}
]
},
created: {
dateTime: "",
userDisplayName: "",
userDn: ""
},
projectId: "",
projectName: "",
projectType: ""
}
}
The goal is to reduce the levels of data hierarchy and create a result like this:
{
"history": [
{
"projectId": "",
"projectName": "",
"projectType": "",
"dateTime":"",
"type": ["approved", "broughtUnderEdit", "changed", "created"],
"userDisplayName": "",
"userDn": ""
}
]
}
The closest thing I can create relies heavily on rewriting the value and doesn't explicitly define the mapping of all the keys other than approved
. Here's the example:
const flattened = mapping.map(orig, [
{
oldKey: "history.approved.modification[0]",
newKey: "history",
values: [
{
oldValue: orig.history.approved.modification[0],
newValue: [
_.merge(orig.history.approved.modification[0], {
type: ["approved", "changed", "broughtUnderEdit", "created"],
projectId: orig.history.projectId,
projectName: orig.history.projectName,
projectType: orig.history.projectType
})
]
}
]
}
]);
I'm thinking the answer is no but was curious if you knew a way to make it work without modifying the code. Thanks for any input!
There's also a sandbox here: https://codesandbox.io/embed/k9rmnlwmm7