I need to parse an array of objects to a Google sheet.
I am using code from here
What I get is
| affiliations | corresponding | current_organization_id | first_name | last_name | orcid | raw_affiliation | researcher_id | state_code | corresponding | current_organization_id | first_name | last_name | orcid | raw_affiliation | researcher_id |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| {city=My City, state_code=, city_id=123456.0, state=, country_code=XX, name=University of Bob, country=Country1, id=d.11111.0, raw_affiliation=This That, AA} | d.11111.0 | Joe | Aaaa | xxx-yyy-vvv-0000 | This That, AA | id.18738465.55 | TRUE | d.11111.0 | Joe | Aaaa | xxx-yyy-vvv-0000 | This That, AA | id.18738465.55 | ||
| {city_id=7891011.0, raw_affiliation=Something Something, BB, country=Country2, state_code=, name=University of Sally, city=Other Cite, state=, country_code=TT, id=d.22222.0} | Sally | Zzzz | Something Something, BB | Sally | Zzzz | Something Something, BB |
What I need
| city | city_id | country | country_code | id | name | raw_affiliation | state | state_code | corresponding | current_organization_id | first_name | last_name | orcid | raw_affiliation | researcher_id |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| My City | 123456 | Country1 | XX | d.11111.0 | University of Bob | This That, AA | d.11111.0 | Joe | Aaaa | xxx-yyy-vvv-0000 | This That, AA | id.18738465.55 | |||
| Other Cite | 7891011 | Country2 | TT | d.22222.0 | University of Sally | Something Something, BB | Sally | Zzzz | Something Something, BB |
affiliations is not parsing
Everything after corresponding parses correctly
How to parse the affiliations array?
Google sheet https://docs.google.com/spreadsheets/d/1jXqlv2Jupdk0dTw0PSlDpGs7BlMVvGt6TjWSzIAlphI/edit?usp=sharing
function main() {
var data = [{'affiliations': [{'city': 'My City', 'city_id': 123456, 'country': 'Country1',
'country_code': 'XX', 'id': 'd.11111.0', 'name': 'University of Bob',
'raw_affiliation': 'This That, AA', 'state': '', 'state_code': ''}],
'corresponding': '', 'current_organization_id': 'd.11111.0',
'first_name': 'Joe', 'last_name': 'Aaaa', 'orcid': ['xxx-yyy-vvv-0000'],
'raw_affiliation': ['This That, AA'], 'researcher_id': 'id.18738465.55'},
{'affiliations': [{'city': 'Other Cite', 'city_id': 7891011, 'country': 'Country2', 'country_code': 'TT',
'id': 'd.22222.0', 'name': 'University of Sally',
'raw_affiliation': 'Something Something, BB', 'state': '', 'state_code': ''}],
'corresponding': '', 'current_organization_id': '', 'first_name': 'Sally',
'last_name': 'Zzzz', 'orcid': [], 'raw_affiliation': ['Something Something, BB'],
'researcher_id': ''}];
var sheet = SpreadsheetApp.getActiveSheet();
var result = pattern1(data);
var result = pattern2(data);
sheet.getRange('a1').offset(0, 0, result.length, result[0].length).setValues(result);
}
function pattern1(data){
var ar = [];
for (var i in data){
for (var key in data[i]){
ar.push([key, data[i][key]]);
}
}
return ar;
}
function pattern2(data){
var ar = [];
var keys = [];
var values = [];
for (var i in data){
for (var key in data[i]){
if (i == 0) keys.push(key);
values.push(data[i][key]);
}
if (i == 0){
ar.push(keys);
keys = [];
}
ar.push(values);
values = [];
}
return ar;
}