Группировка данных JSON по нескольким ключам и сохранение ключей - PullRequest
0 голосов
/ 26 апреля 2019

У меня есть плоский 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.

Ответы [ 2 ]

2 голосов
/ 26 апреля 2019

Вы можете взять вспомогательный массив для требуемых свойств для каждого уровня и последний элемент для другого именованного массива потомков, а также метод для извлечения использованных свойств объекта для получения очищенного объекта для окончательного нажатия.

var data = { 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" }] }] } },
    props = [["pagenumber", "versions"], ["pageversion", "category", "pagetype", "partners"], ["PtrChild", "allocations"], ["Allocation", "ads"]],
    result = data.dataSet.dataTable.reduce((r, { row }) => {
        row.forEach(o => props.reduce((a, [...keys]) => {
            var children = keys.pop(),
                temp = a.find(p => p[keys[0]] === o[keys[0]]);
            if (!temp) a.push(temp = Object.assign(...keys.map(k => ({ [k]: o[k] })), { [children]: [] }));
            o = keys.reduce((p, k) => ({ [k]: k, ...p } = p, p), o);
            return temp[children];
        }, r).push(o));
        return r;
    }, []);

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
0 голосов
/ 26 апреля 2019

Это решение работает с ES5 и lodash 3.10.1. Он основан на идее опоры в ответе Нины Шольц .

Идея здесь, однако, состоит не в том, чтобы удалить свойства из конечного объекта, а в том, чтобы каждый уровень выбирал свои свойства после группы, а если это последний уровень, избегайте группировки и создайте массив из свойств.

function nest(collection, props) { 
  var currentProps = _.first(props);
  var restProps = _.rest(props);
  
  if(_.size(props) === 1) {
    return _.map(collection, _.partialRight(_.pick, currentProps));
  }  

  return _(collection)
    .groupBy(_.first(currentProps))
    .map(function(values) {
      var item = _.first(values);
      var fields = _.pick(item, _.initial(currentProps));
      var collectionKey = _.last(currentProps);
      fields[collectionKey] = nest(values, restProps);
    
      return fields;
    })
    .value();
}

var myCatalog = {"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"}]}]}};

var props = [["pagenumber", "versions"], ["pageversion", "category", "pagetype", "partners"], ["PtrChild", "allocations"], ["Allocation", "ads"], ["catext"]];

var myGroupedData = nest(_.flatten(_.map(myCatalog.dataSet.dataTable, 'row')), props);

console.log(myGroupedData);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/3.10.1/lodash.js"></script>
...