I have parsed the below JSON object using var json = JSON.parse(ticket_orders)
, and now I'm trying to use Google Sheets script editor to make a table with four columns: event_id , display (from costs.base_price), name (from profile), and status (from barcodes, which is an array unlike the others).
The below JSON object shows only two elements, but I have 40 (but this is always changing). I am having trouble creating a function that iteratively extracts just the values I mentioned above and putting them neatly in a table in Google sheets.
var json = [{
"team": null, //This is the start of element 1
"costs": {
"base_price": {
"display": "CA$0.00",
"currency": "CAD",
"value": 0,
"major_value": "0.00"
},
"eventbrite_fee": {
"display": "CA$0.00",
"currency": "CAD",
"value": 0,
"major_value": "0.00"
},
"gross": {
"display": "CA$0.00",
"currency": "CAD",
"value": 0,
"major_value": "0.00"
},
"payment_fee": {
"display": "CA$0.00",
"currency": "CAD",
"value": 0,
"major_value": "0.00"
},
"tax": {
"display": "CA$0.00",
"currency": "CAD",
"value": 0,
"major_value": "0.00"
}
},
"resource_uri": "https://www.eventbriteapi.com/v3/events/12345/attendees/12345/",
"id": "12345",
"changed": "2018-11-02T22:36:48Z",
"created": "2018-11-02T19:07:24Z",
"quantity": 1,
"variant_id": null,
"profile": {
"first_name": "Kennedy",
"last_name": "Singleton",
"email": "kennedy@gmail.com",
"name": "Kennedy Singleton",
"addresses": {
"home": {},
"ship": {},
"work": {},
"bill": {},
"fulfillment": {}
}
},
"barcodes": [
{
"status": "used",
"barcode": "1234567435234",
"created": "2018-11-02T19:07:24Z",
"changed": "2018-11-02T22:36:48Z",
"checkin_type": 0,
"checkin_method": "search",
"is_printed": false
}
],
"answers": [],
"checked_in": true,
"cancelled": false,
"refunded": false,
"affiliate": null,
"guestlist_id": null,
"invited_by": null,
"status": "Checked In",
"ticket_class_name": "General Admission",
"delivery_method": "electronic",
"event_id": "12345",
"order_id": "123123123",
"ticket_class_id": "123123123"
},
{
"team": null, //This is the start of element 2
"costs": {
"base_price": {
"display": "CA$0.00",
"currency": "CAD",
"value": 0,
"major_value": "0.00"
},
"eventbrite_fee": {
"display": "CA$0.00",
"currency": "CAD",
"value": 0,
"major_value": "0.00"
},
"gross": {
"display": "CA$0.00",
"currency": "CAD",
"value": 0,
"major_value": "0.00"
},
"payment_fee": {
"display": "CA$0.00",
"currency": "CAD",
"value": 0,
"major_value": "0.00"
},
"tax": {
"display": "CA$0.00",
"currency": "CAD",
"value": 0,
"major_value": "0.00"
}
},
"resource_uri": "https://www.eventbriteapi.com/v3/events/12345/attendees/12345/",
"id": "12345",
"changed": "2018-11-02T22:36:48Z",
"created": "2018-11-02T19:07:24Z",
"quantity": 1,
"variant_id": null,
"profile": {
"first_name": "Kennedy",
"last_name": "Singleton",
"email": "kennedy@gmail.com",
"name": "Kennedy Singleton",
"addresses": {
"home": {},
"ship": {},
"work": {},
"bill": {},
"fulfillment": {}
}
},
"barcodes": [
{
"status": "used",
"barcode": "1234567435234",
"created": "2018-11-02T19:07:24Z",
"changed": "2018-11-02T22:36:48Z",
"checkin_type": 0,
"checkin_method": "search",
"is_printed": false
}
],
"answers": [],
"checked_in": true,
"cancelled": false,
"refunded": false,
"affiliate": null,
"guestlist_id": null,
"invited_by": null,
"status": "Checked In",
"ticket_class_name": "General Admission",
"delivery_method": "electronic",
"event_id": "12345",
"order_id": "123123123",
"ticket_class_id": "123123123"
}]
Can you please help me create a function to accomplishes this?
I attempted to Google this but I couldn't find a Q&A that has the three parts of: 1) targeting values in different levels of the JSON object hierarchy, 2) having multiple elements that need to be iterated through, and 3) putting them into a Google Sheets table.
Here's what I've been able to script so far:
function getAndInsertConent(url) {
const makeService = getApiService(); //Authorizes my access to the API via OAuth
const apiFetechedContent = UrlFetchApp.fetch(url,{
headers: {
Authorization: 'Bearer ' + makeService.getAccessToken()
}
});
const content = apiFetechedContent.getContentText(); //Makes the raw, fetched JSON response accessible
const json = JSON.parse(content); //Turns the JSON into a JavaScript object
var rows = [Object.keys(json)]; // Retrieve headers, but this equals ['pagination', 'attendees']
var jsonAttendees = json.attendees //stores just the 'attendees' branch of the JavaScript object
//Store our target spreadsheet into a variable
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheets = ss.getSheets();
var sheet = ss.getActiveSheet();
//code to access the specific values I need
}
I couldn't get farther than than where I made that last comment in my code. I don't know how to access the values and put them neatly into a table.
Any help would be greatly appreciated. Thank you very much.
I believe your goal as follows.
event_id, display (from costs.base_price), name (from profile), and status (from barcodes, which is an array unlike the others)
. from the value of json
. From your sample value of json
, I thought that the structure of each element of JSON data including the array is always same. I thought that this might be able to be used. So, in order to retrieve the how about using map()
and the destructuring assignment? When your script is modified, it becomes as follows.
Please set the sheet name you want to put the values.
function getAndInsertConent(url) {
const sheetName = "Sheet1"; // Please set the sheet name.
const makeService = getApiService();
const apiFetechedContent = UrlFetchApp.fetch(url, { headers: { Authorization: 'Bearer ' + makeService.getAccessToken() } });
const content = apiFetechedContent.getContentText();
const json = JSON.parse(content);
const values = json.map(({ event_id, costs: { base_price: { display } }, profile: { name }, barcodes: [{ status }] }) => [event_id, display, name, status]);
const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName);
sheet.getRange(sheet.getLastRow() + 1, 1, values.length, values[0].length).setValues(values);
}
json
of const json = JSON.parse(content);
is the same with json
of your sample value in your question. Please be careful this.
jsonAttendees
is the same with json
of your sample value in your question, please modify above script. When values
of above script is checked, the following test script can be used.
const json = [{ "team": null, //This is the start of element 1 "costs": { "base_price": { "display": "CA$0.00", "currency": "CAD", "value": 0, "major_value": "0.00" }, "eventbrite_fee": { "display": "CA$0.00", "currency": "CAD", "value": 0, "major_value": "0.00" }, "gross": { "display": "CA$0.00", "currency": "CAD", "value": 0, "major_value": "0.00" }, "payment_fee": { "display": "CA$0.00", "currency": "CAD", "value": 0, "major_value": "0.00" }, "tax": { "display": "CA$0.00", "currency": "CAD", "value": 0, "major_value": "0.00" } }, "resource_uri": "https://www.eventbriteapi.com/v3/events/12345/attendees/12345/", "id": "12345", "changed": "2018-11-02T22:36:48Z", "created": "2018-11-02T19:07:24Z", "quantity": 1, "variant_id": null, "profile": { "first_name": "Kennedy", "last_name": "Singleton", "email": "kennedy@gmail.com", "name": "Kennedy Singleton", "addresses": { "home": {}, "ship": {}, "work": {}, "bill": {}, "fulfillment": {} } }, "barcodes": [ { "status": "used", "barcode": "1234567435234", "created": "2018-11-02T19:07:24Z", "changed": "2018-11-02T22:36:48Z", "checkin_type": 0, "checkin_method": "search", "is_printed": false } ], "answers": [], "checked_in": true, "cancelled": false, "refunded": false, "affiliate": null, "guestlist_id": null, "invited_by": null, "status": "Checked In", "ticket_class_name": "General Admission", "delivery_method": "electronic", "event_id": "12345", "order_id": "123123123", "ticket_class_id": "123123123" }, { "team": null, //This is the start of element 2 "costs": { "base_price": { "display": "CA$0.00", "currency": "CAD", "value": 0, "major_value": "0.00" }, "eventbrite_fee": { "display": "CA$0.00", "currency": "CAD", "value": 0, "major_value": "0.00" }, "gross": { "display": "CA$0.00", "currency": "CAD", "value": 0, "major_value": "0.00" }, "payment_fee": { "display": "CA$0.00", "currency": "CAD", "value": 0, "major_value": "0.00" }, "tax": { "display": "CA$0.00", "currency": "CAD", "value": 0, "major_value": "0.00" } }, "resource_uri": "https://www.eventbriteapi.com/v3/events/12345/attendees/12345/", "id": "12345", "changed": "2018-11-02T22:36:48Z", "created": "2018-11-02T19:07:24Z", "quantity": 1, "variant_id": null, "profile": { "first_name": "Kennedy", "last_name": "Singleton", "email": "kennedy@gmail.com", "name": "Kennedy Singleton", "addresses": { "home": {}, "ship": {}, "work": {}, "bill": {}, "fulfillment": {} } }, "barcodes": [ { "status": "used", "barcode": "1234567435234", "created": "2018-11-02T19:07:24Z", "changed": "2018-11-02T22:36:48Z", "checkin_type": 0, "checkin_method": "search", "is_printed": false } ], "answers": [], "checked_in": true, "cancelled": false, "refunded": false, "affiliate": null, "guestlist_id": null, "invited_by": null, "status": "Checked In", "ticket_class_name": "General Admission", "delivery_method": "electronic", "event_id": "12345", "order_id": "123123123", "ticket_class_id": "123123123" }]; const values = json.map(({ event_id, costs: { base_price: { display } }, profile: { name }, barcodes: [{ status }] }) => [event_id, display, name, status]); console.log(values);
If above sample script was not useful and the structure of each element of JSON data including the array is NOT always same, can you provide more sample values?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.