i'm trying to parse a JSON into a Google Sheets. Everything work like a charm except that i figured out JSON has childitems that i need to return as columns.
Json Example
{
"status": "OK",
"items": [
{
"order_id": 1290,
"date": "2019-12-24",
"services": [
{
"start_at": "08:00",
"end_at": "09:00",
"service_id": 121,
"assistant_id": 0
},
{
"start_at": "12:00",
"end_at": "13:00",
"service_id": 122,
"assistant_id": 0
}
],
}
]
}
And what i need to get into Google Sheets is
order_id|date |services.start_at|services.end_at|services.service_id|services.assistant_id|
1290 |2019-12-24 |08:00 |09:00 |121 |0 |
1290 |2019-12-24 |12:00 |13:00 |122 |0 |
I have trying this but i don't figure out how to get child items and repeat values for id and date if more than one service is under the same order_id.
var response = UrlFetchApp.fetch(url, options);
var object = JSON.parse(response.getContentText());
var headers = Object.keys(object.items[0]);
var values = object.items.map(function(e) {return headers.map(function(f) {return e[f]})});
values.unshift(headers);
var sheet = SpreadsheetApp.getActiveSheet();
sheet.getRange(2, 1, values.length, values[0].length).setValues(values);
I hope anyone here can give me a clue because I had tried several days without luck, i'm a noob with JSON and parsing stuff in Apps Script.
Have a nice day :)
servicesagain?