I use app script and try to get value from return array by API, this is return array :
this is the full JSON:
{data=[{first=[[10, 1], [14, 0], [13, 5]}]}
I use this code to get value but not woeking:
var request = UrlFetchApp.fetch(url);
var json = JSON.parse(request);
// this is the code to get value
const value= json.data.first.map(obj => [obj[0]]);
The error is : TypeError: Cannot read property 'map' of undefined
CodePudding user response:
The full json's array you included is not properly closed.
modified it to this instead
{data=[{first=[[10, 1], [14, 0], [13, 5]]}]}
Upon doing that, this code should work.
Code:
function myFunction() {
var json = {data: [{first: [[10, 1], [14, 0], [13, 5]]}]};
Logger.log(json)
// access first array element of data
var value= json.data[0].first.map(obj => [obj[0]]);
Logger.log(value)
}
Using data[0]:
Using data:
CodePudding user response:
Your data doesn't look like JSON to me:
This works:
function myfunk() {
const resp = '{"time":1632835594443, "first":[[10, 1], [14, 0.16306, 0], [13, 5]]}';
const obj = JSON.parse(resp);
Logger.log(JSON.stringify(obj.first));
}
8:26:36 AM Notice Execution started
8:26:37 AM Info [[10,1],[14,0.16306,0],[13,5]]
8:26:37 AM Notice Execution completed
CodePudding user response:
Show your response
But maybe just
const [first] = json.data || []; // get 0 from array
const values = first.map(obj => [obj[0]])
Or maybe get object property
const { first } = json.data || {}; // get propetry first
const values = first.map(obj => [obj[0]])