У меня есть плоский JSON-файл, который выглядит следующим образом:
{"dataSet": {"dataTable": [{
"id": "List1",
"row": [
{
"pagenumber": "7",
"pageversion": "HE; K12",
"category": "3D Print - K12;HE",
"pagetype": "Product",
"PtrChild": "MakerBot - 10771",
"Allocation": "0.500",
"catext": "Text goes here"
},
{
"pagenumber": "7",
"pageversion": "SL",
"category": "3D Print - SL",
"pagetype": "Product",
"PtrChild": "AUTODESK - 10032",
"Allocation": "0.500",
"catext": "Text goes here"
},
{
"pagenumber": "10",
"pageversion": "Apply to All",
"category": "Secure Printers",
"pagetype": "Brand",
"PtrChild": null,
"Allocation": "1.000",
"catext": "Text goes here"
},
{
"pagenumber": "11",
"pageversion": "Apply to All",
"category": "Secure Printers",
"pagetype": "Product",
"PtrChild": "EPSON INK JET** - 10082",
"Allocation": "0.200",
"catext": "Text goes here"
},
{
"pagenumber": "11",
"pageversion": "Apply to All",
"category": "Secure Printers",
"pagetype": "Product",
"PtrChild": "EPSON INK JET** - 10082",
"Allocation": "0.200",
"catext": "Text goes here"
},
{
"pagenumber": "11",
"pageversion": "Apply to All",
"category": "Secure Printers",
"pagetype": "Product",
"PtrChild": "EPSON INK JET** - 10082",
"Allocation": "0.500",
"catext": "Text goes here"
},
{
"pagenumber": "11",
"pageversion": "Apply to All",
"category": "Secure Printers",
"pagetype": "Product",
"PtrChild": "LEXMARK** - 10151",
"Allocation": "0.200",
"catext": "Text goes here"
}
]
}]}}
Мне нужно сгруппировать данные по номеру страницы, затем по версии страницы, затем по PtrChild, затем по распределению, чтобы по окончании это выглядело следующим образом:
{"pages": [
{
"pagenumber": "7",
"versions": [
{
"pageversion": "HE; K12",
"category": "3D Print - K12;HE",
"pagetype": "Product",
"partners": [{
"PtrChild": "MakerBot - 10771",
"allocations": [{
"Allocation": "0.500",
"ads": [{"catext": "Text goes here"}]
}]
}]
},
{
"pageversion": "SL",
"category": "3D Print - SL",
"pagetype": "Product",
"partners": [{
"PtrChild": "AUTODESK - 10032",
"allocations": [{
"Allocation": "0.500",
"ads": [{"catext": "Text goes here"}]
}]
}]
}
]
},
{
"pagenumber": "10",
"versions": [{
"pageversion": "Apply to All",
"category": "Secure Printers",
"pagetype": "Brand",
"partners": [{
"PtrChild": null,
"allocations": [{
"Allocation": "1.500",
"ads": [{"catext": "Text goes here"}]
}]
}]
}]
},
{
"pagenumber": "11",
"versions": [{
"pageversion": "Apply to All",
"category": "Secure Printers",
"pagetype": "Product",
"partners": [
{
"PtrChild": "EPSON INK JET** - 10082",
"allocations": [
{
"Allocation": "0.250",
"ads": [
{"catext": "Text goes here"},
{"catext": "Text goes here"}
]
},
{
"Allocation": "0.500",
"ads": [{"catext": "Text goes here"}]
}
]
},
{
"PtrChild": "LEXMARK** - 10151",
"allocations": [{
"Allocation": "0.200",
"ads": [{"catext": "Text goes here"}]
}]
}
]
}]
}
]}
Запуск этого:
var myGroupedData = nest(myCatalog, ["pagenumber", "pageversion", "PtrChild", "Allocation"]);
// Reorganize JSON data
function nest(collection, keys) {
if (!keys.length) {
return collection;
}
else {
return _(collection).groupBy(keys[0]).mapValues(function(values) {
return nest(values, keys.slice(1));
}).value();
}
}
... делает группировку правильной, но исключает ключи для каждой группы:
{
"7": {
"HE; K12": {"MakerBot - 10771": {"0.500": [{
"pagenumber": "7",
"pageversion": "HE; K12",
"category": "3D Print - K12;HE",
"pagetype": "Product",
"PtrChild": "MakerBot - 10771",
"Allocation": "0.500",
"catext": "Text goes here"
}]}},
"SL": {"AUTODESK - 10032": {"0.500": [{
"pagenumber": "7",
"pageversion": "SL",
"category": "3D Print - SL",
"pagetype": "Product",
"PtrChild": "AUTODESK - 10032",
"Allocation": "0.500",
"catext": "Text goes here"
}]}}
},
"10": {"Apply to All": {"null": {"1.000": [{
"pagenumber": "10",
"pageversion": "Apply to All",
"category": "Secure Printers",
"pagetype": "Brand",
"PtrChild": null,
"Allocation": "1.000",
"catext": "Text goes here"
}]}}},
"11": {"Apply to All": {
"EPSON INK JET** - 10082": {
"0.200": [
{
"pagenumber": "11",
"pageversion": "Apply to All",
"category": "Secure Printers",
"pagetype": "Product",
"PtrChild": "EPSON INK JET** - 10082",
"Allocation": "0.200",
"catext": "Text goes here"
},
{
"pagenumber": "11",
"pageversion": "Apply to All",
"category": "Secure Printers",
"pagetype": "Product",
"PtrChild": "EPSON INK JET** - 10082",
"Allocation": "0.200",
"catext": "Text goes here"
}
],
"0.500": [{
"pagenumber": "11",
"pageversion": "Apply to All",
"category": "Secure Printers",
"pagetype": "Product",
"PtrChild": "EPSON INK JET** - 10082",
"Allocation": "0.500",
"catext": "Text goes here"
}]
},
"LEXMARK** - 10151": {"0.200": [{
"pagenumber": "11",
"pageversion": "Apply to All",
"category": "Secure Printers",
"pagetype": "Product",
"PtrChild": "LEXMARK** - 10151",
"Allocation": "0.200",
"catext": "Text goes here"
}]}
}}
}
Как сохранить ключи и группу на нескольких клавишах?
Я вынужден использовать ExtendScript, поэтому не могу использовать Lodash выше версии 3.10.1.