Home > Enterprise >  Convert JSON nested array to flat array
Convert JSON nested array to flat array

Time:01-16

I have a JSON array which includes a nested array:

{
    "data": [
        {
            "id": 659,
            "created_at": "2023-01-13T06:35:08.000000Z",
            "products": [
                {
                    "name": "532",
                    "properties": [
                        {
                            "name": "color",
                            "value": "blue"
                        },
                        {
                            "name": "size",
                            "value": "1"
                        }
                    ],
                }
            ]
        },
        {
            "id": 658,
            "created_at": "2023-01-12T21:36:06.000000Z",
            "products": [
                {
                    "name": "532",
                    "properties": [
                        {
                            "name": "color",
                            "value": "khaki"
                        },
                        {
                            "name": "size",
                            "value": "2"
                        }
                    ],
                }
            ]
        },
        {
            "id": 656,
            "created_at": "2023-01-11T17:19:42.000000Z",
            "products": [
                {
                    "name": "2245/442",
                    "properties": [
                        {
                            "name": "color",
                            "value": "gray"
                        },
                        {
                            "name": "fabric",
                            "value": "fleece"
                        },
                        {
                            "name": "size",
                            "value": "2"
                        }
                    ],
                }
            ]
        },
        {
            "id": 655,
            "created_at": "2023-01-10T21:56:51.000000Z",
            "products": [
                {
                    "name": "298/426-2",
                    "properties": [
                        {
                            "name": "color",
                            "value": "blue"
                        },
                        {
                            "name": "fabric",
                            "value": "footer"
                        },
                        {
                            "name": "size",
                            "value": "4"
                        }
                    ],
                },
                {
                    "name": "257/426",
                    "properties": [
                        {
                            "name": "color",
                            "value": "mint"
                        },
                        {
                            "name": "fabric",
                            "value": "footer"
                        },
                        {
                            "name": "size",
                            "value": "4"
                        }
                    ],
                }
            ]
        },
    ],
}

I need to read it and write it to another array. All code I have so far:

var responce = UrlFetchApp.fetch(url, options);
var dataAll = JSON.parse(responce.getContentText());
var dataSet = dataAll.data;
var rows = [];

dataSet.forEach((e) => {
  e.products.forEach((product) => {
    product.properties.forEach((property) => {
      rows.push([e.id, e.created_at, product.name, property.value]);
    })
  });
});

console.log(rows);

It writes data to an array in the form, that is, for each "product.value" value, it writes "id", "created_at", "name":

[ 659, '2023-01-13T06:35:08.000000Z', '532', 'blue' ],
[ 659, '2023-01-13T06:35:08.000000Z', '532', '1' ],
[ 658, '2023-01-12T21:36:06.000000Z', '532', 'khaki' ],
[ 658, '2023-01-12T21:36:06.000000Z', '532', '2' ],
[ 656, '2023-01-11T17:19:42.000000Z', '2245/442', 'gray' ],
[ 656, '2023-01-11T17:19:42.000000Z', '2245/442', 'fleece' ],
[ 656, '2023-01-11T17:19:42.000000Z', '2245/442', '2' ],
[ 655, '2023-01-10T21:56:51.000000Z', '298/426-2', 'blue' ],
[ 655, '2023-01-10T21:56:51.000000Z', '298/426-2', 'footer' ],
[ 655, '2023-01-10T21:56:51.000000Z', '298/426-2', '4' ],
[ 655, '2023-01-10T21:56:51.000000Z', '257/426', 'mint' ],
[ 655, '2023-01-10T21:56:51.000000Z', '257/426', 'footer' ],
[ 655, '2023-01-10T21:56:51.000000Z', '257/426', '4' ]

But I need to write like:

[ 659, '2023-01-13T06:35:08.000000Z', '532', 'blue', '1' ],
[ 658, '2023-01-12T21:36:06.000000Z', '532', 'khaki', '2' ],
[ 656, '2023-01-11T17:19:42.000000Z', '2245/442', 'gray', 'fleece', '2' ],
[ 655, '2023-01-10T21:56:51.000000Z', '298/426-2', 'blue', 'footer', '4' ],
[ 655, '2023-01-10T21:56:51.000000Z', '257/426', 'mint', 'footer', '4' ]

That is, I need to read the array "properties" and write the values "values" to a string (maybe I'm not expressing myself correctly). I understand that I need to use the "map" function, but I don't understand how to use it yet ((

Thanks in advance for any advice

CodePudding user response:

dataSet.forEach((e) => {
   e.products.forEach((product) => {
     rows.push([
       e.id, e.created_at, product.name
     ].concat(product.properties.map(p => p.value)));
   });
});

CodePudding user response:

const rows = dataSet.map((item) => {
    const newRows = [item.id, item.created_at];
    item.products.forEach((product) => {
        const properties = product.properties.map((property) => property.value);
        newRows.push(product.name);
        newRows.push(...properties);
    });
    return newRows;
});

console.log(rows);
  • Related