how to extract multi layered array from json
I have a JSON response resembling the following schema...
"resultCode": 200,
"message": "Success",
"generated": "2018-11-14T18:42:02",
"expires": "2018-11-14T18:50:00",
"data":
"generationByFuel": [
"date": "2018-11-14T00:00:00",
"period": 37,
"generation": [
"fuel": "Hydro",
"generation": 1011.298,
"capacity": 2000
,
"fuel": "Wind",
"generation": 2544.788,
"capacity": 3500
]
,
"date": "2018-11-14T00:00:00",
"period": 36,
"generation": [
"fuel": "Hydro",
"generation": 1100,
"capacity": 2000
,
"fuel": "Wind",
"generation": 2500,
"capacity": 3500
]
]
From this, I need to extract multiple arrays that will be inserted into a googlesheet ("shtGen") filling the following columns:
| Date | Period | Fuel | Generation | Capacity | Runtime |
I'm guessing what I need to do is write something that outputs (generationByFuel[i](generation[r]))
but I'm struggling to figure out how to do that.
I've tried a number of options but below is my last effort where I think I'm on the right track. That said, the function runs without returning an error but doesn't insert any info into the google sheet. Same thing happens when I run the function with the debugger.
// define output to paste into sheet
for (var i in genData)
var iInput = ;
iInput.push(genData[i].date);
iInput.push(genData[i].period);
for (var r in fuelData)
var rInput = ;
rInput.push(fuelData[r].fuel);
rInput.push(fuelData[r].capacity);
rInput.push(fuelData[r].generation);
rInput.push(now = new Date());
shtGen.appendRow(iInput + rInput);
Any help or suggestions here would be greatly appreciated. Thanks in advance!
arrays json google-apps-script
add a comment |
I have a JSON response resembling the following schema...
"resultCode": 200,
"message": "Success",
"generated": "2018-11-14T18:42:02",
"expires": "2018-11-14T18:50:00",
"data":
"generationByFuel": [
"date": "2018-11-14T00:00:00",
"period": 37,
"generation": [
"fuel": "Hydro",
"generation": 1011.298,
"capacity": 2000
,
"fuel": "Wind",
"generation": 2544.788,
"capacity": 3500
]
,
"date": "2018-11-14T00:00:00",
"period": 36,
"generation": [
"fuel": "Hydro",
"generation": 1100,
"capacity": 2000
,
"fuel": "Wind",
"generation": 2500,
"capacity": 3500
]
]
From this, I need to extract multiple arrays that will be inserted into a googlesheet ("shtGen") filling the following columns:
| Date | Period | Fuel | Generation | Capacity | Runtime |
I'm guessing what I need to do is write something that outputs (generationByFuel[i](generation[r]))
but I'm struggling to figure out how to do that.
I've tried a number of options but below is my last effort where I think I'm on the right track. That said, the function runs without returning an error but doesn't insert any info into the google sheet. Same thing happens when I run the function with the debugger.
// define output to paste into sheet
for (var i in genData)
var iInput = ;
iInput.push(genData[i].date);
iInput.push(genData[i].period);
for (var r in fuelData)
var rInput = ;
rInput.push(fuelData[r].fuel);
rInput.push(fuelData[r].capacity);
rInput.push(fuelData[r].generation);
rInput.push(now = new Date());
shtGen.appendRow(iInput + rInput);
Any help or suggestions here would be greatly appreciated. Thanks in advance!
arrays json google-apps-script
fuelData
should be assigned before the inner loopfuelData = genData[i].generation
– Slai
Nov 14 '18 at 7:01
@Slai my apologies, i do declareiInput
andrInput
but was getting red flags saying that i was posting more code than text, so removed. these are my declarations...var genData = json['data']['generationByFuel'];
var fuelData = json['data']['generationByFuel']['generation'];
– mitch-NZ
Nov 15 '18 at 1:57
add a comment |
I have a JSON response resembling the following schema...
"resultCode": 200,
"message": "Success",
"generated": "2018-11-14T18:42:02",
"expires": "2018-11-14T18:50:00",
"data":
"generationByFuel": [
"date": "2018-11-14T00:00:00",
"period": 37,
"generation": [
"fuel": "Hydro",
"generation": 1011.298,
"capacity": 2000
,
"fuel": "Wind",
"generation": 2544.788,
"capacity": 3500
]
,
"date": "2018-11-14T00:00:00",
"period": 36,
"generation": [
"fuel": "Hydro",
"generation": 1100,
"capacity": 2000
,
"fuel": "Wind",
"generation": 2500,
"capacity": 3500
]
]
From this, I need to extract multiple arrays that will be inserted into a googlesheet ("shtGen") filling the following columns:
| Date | Period | Fuel | Generation | Capacity | Runtime |
I'm guessing what I need to do is write something that outputs (generationByFuel[i](generation[r]))
but I'm struggling to figure out how to do that.
I've tried a number of options but below is my last effort where I think I'm on the right track. That said, the function runs without returning an error but doesn't insert any info into the google sheet. Same thing happens when I run the function with the debugger.
// define output to paste into sheet
for (var i in genData)
var iInput = ;
iInput.push(genData[i].date);
iInput.push(genData[i].period);
for (var r in fuelData)
var rInput = ;
rInput.push(fuelData[r].fuel);
rInput.push(fuelData[r].capacity);
rInput.push(fuelData[r].generation);
rInput.push(now = new Date());
shtGen.appendRow(iInput + rInput);
Any help or suggestions here would be greatly appreciated. Thanks in advance!
arrays json google-apps-script
I have a JSON response resembling the following schema...
"resultCode": 200,
"message": "Success",
"generated": "2018-11-14T18:42:02",
"expires": "2018-11-14T18:50:00",
"data":
"generationByFuel": [
"date": "2018-11-14T00:00:00",
"period": 37,
"generation": [
"fuel": "Hydro",
"generation": 1011.298,
"capacity": 2000
,
"fuel": "Wind",
"generation": 2544.788,
"capacity": 3500
]
,
"date": "2018-11-14T00:00:00",
"period": 36,
"generation": [
"fuel": "Hydro",
"generation": 1100,
"capacity": 2000
,
"fuel": "Wind",
"generation": 2500,
"capacity": 3500
]
]
From this, I need to extract multiple arrays that will be inserted into a googlesheet ("shtGen") filling the following columns:
| Date | Period | Fuel | Generation | Capacity | Runtime |
I'm guessing what I need to do is write something that outputs (generationByFuel[i](generation[r]))
but I'm struggling to figure out how to do that.
I've tried a number of options but below is my last effort where I think I'm on the right track. That said, the function runs without returning an error but doesn't insert any info into the google sheet. Same thing happens when I run the function with the debugger.
// define output to paste into sheet
for (var i in genData)
var iInput = ;
iInput.push(genData[i].date);
iInput.push(genData[i].period);
for (var r in fuelData)
var rInput = ;
rInput.push(fuelData[r].fuel);
rInput.push(fuelData[r].capacity);
rInput.push(fuelData[r].generation);
rInput.push(now = new Date());
shtGen.appendRow(iInput + rInput);
Any help or suggestions here would be greatly appreciated. Thanks in advance!
arrays json google-apps-script
arrays json google-apps-script
asked Nov 14 '18 at 6:32
mitch-NZmitch-NZ
258
258
fuelData
should be assigned before the inner loopfuelData = genData[i].generation
– Slai
Nov 14 '18 at 7:01
@Slai my apologies, i do declareiInput
andrInput
but was getting red flags saying that i was posting more code than text, so removed. these are my declarations...var genData = json['data']['generationByFuel'];
var fuelData = json['data']['generationByFuel']['generation'];
– mitch-NZ
Nov 15 '18 at 1:57
add a comment |
fuelData
should be assigned before the inner loopfuelData = genData[i].generation
– Slai
Nov 14 '18 at 7:01
@Slai my apologies, i do declareiInput
andrInput
but was getting red flags saying that i was posting more code than text, so removed. these are my declarations...var genData = json['data']['generationByFuel'];
var fuelData = json['data']['generationByFuel']['generation'];
– mitch-NZ
Nov 15 '18 at 1:57
fuelData
should be assigned before the inner loop fuelData = genData[i].generation
– Slai
Nov 14 '18 at 7:01
fuelData
should be assigned before the inner loop fuelData = genData[i].generation
– Slai
Nov 14 '18 at 7:01
@Slai my apologies, i do declare
iInput
and rInput
but was getting red flags saying that i was posting more code than text, so removed. these are my declarations... var genData = json['data']['generationByFuel'];
var fuelData = json['data']['generationByFuel']['generation'];
– mitch-NZ
Nov 15 '18 at 1:57
@Slai my apologies, i do declare
iInput
and rInput
but was getting red flags saying that i was posting more code than text, so removed. these are my declarations... var genData = json['data']['generationByFuel'];
var fuelData = json['data']['generationByFuel']['generation'];
– mitch-NZ
Nov 15 '18 at 1:57
add a comment |
3 Answers
3
active
oldest
votes
How about this modification? I think that there are several solutions for your situation. So please think of this as one of them.
Modification points:
fuelData
is required to be declared in the 1st for loop.- This is mentioned by @Slai.
iInput + rInput
becomes the string type. By this, an error occurs atappendRow()
.iInput.concat(rInput)
is used for this.
- In order to arrange to
| Date | Period | Fuel | Generation | Capacity | Runtime |
, the order for pushing the value is required to berInput.push(fuelData[r].fuel)
rInput.push(fuelData[r].generation)
rInput.push(fuelData[r].capacity)
In this modified script, obj
is your json object in your question. And then shtGen
supposes the sheet object.
Modified script:
var obj = ### your json object ###;
var genData = obj.data.generationByFuel;
for (var i in genData)
var iInput = ;
iInput.push(genData[i].date);
iInput.push(genData[i].period);
var fuelData = genData[i].generation; // Added
for (var r in fuelData)
var rInput = ;
rInput.push(fuelData[r].fuel); // Modified
rInput.push(fuelData[r].generation); // Modified
rInput.push(fuelData[r].capacity); // Modified
rInput.push(new Date());
shtGen.appendRow(iInput.concat(rInput)); // Modified
I think that when the object is large, appendRow()
becomes the reason of the increase of the process cost. So I recommend to use setValues()
for putting the values to Spreadsheet as follows.
var obj = ### your json object ###;
var res = ;
var genData = obj.data.generationByFuel;
for (var i in genData)
var iInput = ;
iInput.push(genData[i].date);
iInput.push(genData[i].period);
var fuelData = genData[i].generation;
for (var r in fuelData)
var rInput = ;
rInput.push(fuelData[r].fuel);
rInput.push(fuelData[r].generation);
rInput.push(fuelData[r].capacity);
rInput.push(new Date());
res.push(iInput.concat(rInput)); // Modified
shtGen.getRange(shtGen.getLastRow() + 1, 1, res.length, res[0].length).setValues(res); // Added
Other pattern:
As one of other modifications, you can also use the following script. The cost of this script is lower than that of above scripts.
var obj = ### your json object ###;
var res = obj.data.generationByFuel.reduce(function(ar, e)
return ar.concat(e.generation.map(function(f)
return [e.date, e.period, f.fuel, f.generation, f.capacity, new Date()];
));
, );
shtGen.getRange(shtGen.getLastRow() + 1, 1, res.length, res[0].length).setValues(res);
References:
- appendRow()
- setValues()
- concat()
- reduce()
- map
If this was not what you want, please tell me. I would like to modify it.
tthanks @Tanaike. i initially went for your 'modified script' approach which worked perfectly. then i had some spare time this morning to take a closer look at what you were doing in the 'other pattern'. that worked just as well, but uses less lines of code... awesome! thanks
– mitch-NZ
Nov 15 '18 at 20:15
@mitch-NZ Thank you for replying. I'm glad your issue was solved. Thank you, too.
– Tanaike
Nov 15 '18 at 22:09
add a comment |
Here's an alternative approach that leverages JSON.parse()
.
Reviver functions are an optional parameter to JSON.parse()
. The cool thing about these functions is that they are invoked with key/value pairs for every node in a JSON object tree using depth-first search.
Run the following snippet to get an idea of how it works:
var jsonString = JSON.stringify(
"resultCode": 200,
"message": "Success",
"generated": "2018-11-14T18:42:02",
"expires": "2018-11-14T18:50:00",
"data":
"generationByFuel": [
"date": "2018-11-14T00:00:00",
"period": 37,
"generation": [
"fuel": "Hydro",
"generation": 1011.298,
"capacity": 2000
,
"fuel": "Wind",
"generation": 2544.788,
"capacity": 3500
]
,
"date": "2018-11-14T00:00:00",
"period": 36,
"generation": [
"fuel": "Hydro",
"generation": 1100,
"capacity": 2000
,
"fuel": "Wind",
"generation": 2500,
"capacity": 3500
]
]
);
JSON.parse(jsonString, function(key, value)
console.log("Key:%s, Value:%o", key, value);
return value;
);
The console log generated by the above script 'walks' the object tree depth-first; it drills down to print out the value of the leaf nodes before it prints the value of the parent nodes. For example: "fuel":"Hydro"
is printed before the "generation":[..., ...,..]
array that contains it.
You can exploit this to generate the requisite data for your spreadsheet as follows:
var jsonString = JSON.stringify(
"resultCode": 200,
"message": "Success",
"generated": "2018-11-14T18:42:02",
"expires": "2018-11-14T18:50:00",
"data":
"generationByFuel": [
"date": "2018-11-14T00:00:00",
"period": 37,
"generation": [
"fuel": "Hydro",
"generation": 1011.298,
"capacity": 2000
,
"fuel": "Wind",
"generation": 2544.788,
"capacity": 3500
]
,
"date": "2018-11-14T00:00:00",
"period": 36,
"generation": [
"fuel": "Hydro",
"generation": 1100,
"capacity": 2000
,
"fuel": "Wind",
"generation": 2500,
"capacity": 3500
]
]
);
var values = ;
var state = ;
JSON.parse(jsonString, function(key, value)
switch(key)
case "generation":
if (Array.isArray(value)) break;
case "date":
case "period":
case "fuel":
case "capacity":
state[key] = value;
break;
if (key === "capacity")
values.push([
state.date,
state.period,
state.fuel,
state.generation,
state.capacity,
new Date()
]);
return value;
);
console.log(values);
The snippet above filters out the desired data as the reviver walks the JSON's object tree and populates the 2D values array in the process. Now all you need to do is call setValues()
on your sheet with the values array.
1
@TheMaster Works for me.
– Dimu Designs
Nov 15 '18 at 0:50
thanks for your answer. whilst i can see that your suggestion works in this instance, i'm not familiar withswitch
,case
, andbreak
statements. until i can research these and understand why this works i've accepted Tanaike's answer to my issue.
– mitch-NZ
Nov 16 '18 at 3:21
@mitch-NZ Here's a good place to start : developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
– Dimu Designs
Nov 16 '18 at 12:40
add a comment |
I think you missed the "genData[i]" within the for loop for var 'r'. Try this.
for (var i in genData)
var iInput = ;
iInput.push(genData[i].date);
iInput.push(genData[i].period);
for (var r in fuelData)
var rInput = ;
rInput.push(genData[i].fuelData[r].fuel);
rInput.push(genData[i].fuelData[r].capacity);
rInput.push(genData[i].fuelData[r].generation);
rInput.push(now = new Date());
shtGen.appendRow(iInput + rInput);
Also it can be how you are storing the response in your model class.
thanks for your answer. before submitting my question i had tried this as well as numerous variations of this. however the best result i could get was for the script to complete without rerturning an error, but it would only outputdate
andperiod
.
– mitch-NZ
Nov 15 '18 at 21:53
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53294328%2fhow-to-extract-multi-layered-array-from-json%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
How about this modification? I think that there are several solutions for your situation. So please think of this as one of them.
Modification points:
fuelData
is required to be declared in the 1st for loop.- This is mentioned by @Slai.
iInput + rInput
becomes the string type. By this, an error occurs atappendRow()
.iInput.concat(rInput)
is used for this.
- In order to arrange to
| Date | Period | Fuel | Generation | Capacity | Runtime |
, the order for pushing the value is required to berInput.push(fuelData[r].fuel)
rInput.push(fuelData[r].generation)
rInput.push(fuelData[r].capacity)
In this modified script, obj
is your json object in your question. And then shtGen
supposes the sheet object.
Modified script:
var obj = ### your json object ###;
var genData = obj.data.generationByFuel;
for (var i in genData)
var iInput = ;
iInput.push(genData[i].date);
iInput.push(genData[i].period);
var fuelData = genData[i].generation; // Added
for (var r in fuelData)
var rInput = ;
rInput.push(fuelData[r].fuel); // Modified
rInput.push(fuelData[r].generation); // Modified
rInput.push(fuelData[r].capacity); // Modified
rInput.push(new Date());
shtGen.appendRow(iInput.concat(rInput)); // Modified
I think that when the object is large, appendRow()
becomes the reason of the increase of the process cost. So I recommend to use setValues()
for putting the values to Spreadsheet as follows.
var obj = ### your json object ###;
var res = ;
var genData = obj.data.generationByFuel;
for (var i in genData)
var iInput = ;
iInput.push(genData[i].date);
iInput.push(genData[i].period);
var fuelData = genData[i].generation;
for (var r in fuelData)
var rInput = ;
rInput.push(fuelData[r].fuel);
rInput.push(fuelData[r].generation);
rInput.push(fuelData[r].capacity);
rInput.push(new Date());
res.push(iInput.concat(rInput)); // Modified
shtGen.getRange(shtGen.getLastRow() + 1, 1, res.length, res[0].length).setValues(res); // Added
Other pattern:
As one of other modifications, you can also use the following script. The cost of this script is lower than that of above scripts.
var obj = ### your json object ###;
var res = obj.data.generationByFuel.reduce(function(ar, e)
return ar.concat(e.generation.map(function(f)
return [e.date, e.period, f.fuel, f.generation, f.capacity, new Date()];
));
, );
shtGen.getRange(shtGen.getLastRow() + 1, 1, res.length, res[0].length).setValues(res);
References:
- appendRow()
- setValues()
- concat()
- reduce()
- map
If this was not what you want, please tell me. I would like to modify it.
tthanks @Tanaike. i initially went for your 'modified script' approach which worked perfectly. then i had some spare time this morning to take a closer look at what you were doing in the 'other pattern'. that worked just as well, but uses less lines of code... awesome! thanks
– mitch-NZ
Nov 15 '18 at 20:15
@mitch-NZ Thank you for replying. I'm glad your issue was solved. Thank you, too.
– Tanaike
Nov 15 '18 at 22:09
add a comment |
How about this modification? I think that there are several solutions for your situation. So please think of this as one of them.
Modification points:
fuelData
is required to be declared in the 1st for loop.- This is mentioned by @Slai.
iInput + rInput
becomes the string type. By this, an error occurs atappendRow()
.iInput.concat(rInput)
is used for this.
- In order to arrange to
| Date | Period | Fuel | Generation | Capacity | Runtime |
, the order for pushing the value is required to berInput.push(fuelData[r].fuel)
rInput.push(fuelData[r].generation)
rInput.push(fuelData[r].capacity)
In this modified script, obj
is your json object in your question. And then shtGen
supposes the sheet object.
Modified script:
var obj = ### your json object ###;
var genData = obj.data.generationByFuel;
for (var i in genData)
var iInput = ;
iInput.push(genData[i].date);
iInput.push(genData[i].period);
var fuelData = genData[i].generation; // Added
for (var r in fuelData)
var rInput = ;
rInput.push(fuelData[r].fuel); // Modified
rInput.push(fuelData[r].generation); // Modified
rInput.push(fuelData[r].capacity); // Modified
rInput.push(new Date());
shtGen.appendRow(iInput.concat(rInput)); // Modified
I think that when the object is large, appendRow()
becomes the reason of the increase of the process cost. So I recommend to use setValues()
for putting the values to Spreadsheet as follows.
var obj = ### your json object ###;
var res = ;
var genData = obj.data.generationByFuel;
for (var i in genData)
var iInput = ;
iInput.push(genData[i].date);
iInput.push(genData[i].period);
var fuelData = genData[i].generation;
for (var r in fuelData)
var rInput = ;
rInput.push(fuelData[r].fuel);
rInput.push(fuelData[r].generation);
rInput.push(fuelData[r].capacity);
rInput.push(new Date());
res.push(iInput.concat(rInput)); // Modified
shtGen.getRange(shtGen.getLastRow() + 1, 1, res.length, res[0].length).setValues(res); // Added
Other pattern:
As one of other modifications, you can also use the following script. The cost of this script is lower than that of above scripts.
var obj = ### your json object ###;
var res = obj.data.generationByFuel.reduce(function(ar, e)
return ar.concat(e.generation.map(function(f)
return [e.date, e.period, f.fuel, f.generation, f.capacity, new Date()];
));
, );
shtGen.getRange(shtGen.getLastRow() + 1, 1, res.length, res[0].length).setValues(res);
References:
- appendRow()
- setValues()
- concat()
- reduce()
- map
If this was not what you want, please tell me. I would like to modify it.
tthanks @Tanaike. i initially went for your 'modified script' approach which worked perfectly. then i had some spare time this morning to take a closer look at what you were doing in the 'other pattern'. that worked just as well, but uses less lines of code... awesome! thanks
– mitch-NZ
Nov 15 '18 at 20:15
@mitch-NZ Thank you for replying. I'm glad your issue was solved. Thank you, too.
– Tanaike
Nov 15 '18 at 22:09
add a comment |
How about this modification? I think that there are several solutions for your situation. So please think of this as one of them.
Modification points:
fuelData
is required to be declared in the 1st for loop.- This is mentioned by @Slai.
iInput + rInput
becomes the string type. By this, an error occurs atappendRow()
.iInput.concat(rInput)
is used for this.
- In order to arrange to
| Date | Period | Fuel | Generation | Capacity | Runtime |
, the order for pushing the value is required to berInput.push(fuelData[r].fuel)
rInput.push(fuelData[r].generation)
rInput.push(fuelData[r].capacity)
In this modified script, obj
is your json object in your question. And then shtGen
supposes the sheet object.
Modified script:
var obj = ### your json object ###;
var genData = obj.data.generationByFuel;
for (var i in genData)
var iInput = ;
iInput.push(genData[i].date);
iInput.push(genData[i].period);
var fuelData = genData[i].generation; // Added
for (var r in fuelData)
var rInput = ;
rInput.push(fuelData[r].fuel); // Modified
rInput.push(fuelData[r].generation); // Modified
rInput.push(fuelData[r].capacity); // Modified
rInput.push(new Date());
shtGen.appendRow(iInput.concat(rInput)); // Modified
I think that when the object is large, appendRow()
becomes the reason of the increase of the process cost. So I recommend to use setValues()
for putting the values to Spreadsheet as follows.
var obj = ### your json object ###;
var res = ;
var genData = obj.data.generationByFuel;
for (var i in genData)
var iInput = ;
iInput.push(genData[i].date);
iInput.push(genData[i].period);
var fuelData = genData[i].generation;
for (var r in fuelData)
var rInput = ;
rInput.push(fuelData[r].fuel);
rInput.push(fuelData[r].generation);
rInput.push(fuelData[r].capacity);
rInput.push(new Date());
res.push(iInput.concat(rInput)); // Modified
shtGen.getRange(shtGen.getLastRow() + 1, 1, res.length, res[0].length).setValues(res); // Added
Other pattern:
As one of other modifications, you can also use the following script. The cost of this script is lower than that of above scripts.
var obj = ### your json object ###;
var res = obj.data.generationByFuel.reduce(function(ar, e)
return ar.concat(e.generation.map(function(f)
return [e.date, e.period, f.fuel, f.generation, f.capacity, new Date()];
));
, );
shtGen.getRange(shtGen.getLastRow() + 1, 1, res.length, res[0].length).setValues(res);
References:
- appendRow()
- setValues()
- concat()
- reduce()
- map
If this was not what you want, please tell me. I would like to modify it.
How about this modification? I think that there are several solutions for your situation. So please think of this as one of them.
Modification points:
fuelData
is required to be declared in the 1st for loop.- This is mentioned by @Slai.
iInput + rInput
becomes the string type. By this, an error occurs atappendRow()
.iInput.concat(rInput)
is used for this.
- In order to arrange to
| Date | Period | Fuel | Generation | Capacity | Runtime |
, the order for pushing the value is required to berInput.push(fuelData[r].fuel)
rInput.push(fuelData[r].generation)
rInput.push(fuelData[r].capacity)
In this modified script, obj
is your json object in your question. And then shtGen
supposes the sheet object.
Modified script:
var obj = ### your json object ###;
var genData = obj.data.generationByFuel;
for (var i in genData)
var iInput = ;
iInput.push(genData[i].date);
iInput.push(genData[i].period);
var fuelData = genData[i].generation; // Added
for (var r in fuelData)
var rInput = ;
rInput.push(fuelData[r].fuel); // Modified
rInput.push(fuelData[r].generation); // Modified
rInput.push(fuelData[r].capacity); // Modified
rInput.push(new Date());
shtGen.appendRow(iInput.concat(rInput)); // Modified
I think that when the object is large, appendRow()
becomes the reason of the increase of the process cost. So I recommend to use setValues()
for putting the values to Spreadsheet as follows.
var obj = ### your json object ###;
var res = ;
var genData = obj.data.generationByFuel;
for (var i in genData)
var iInput = ;
iInput.push(genData[i].date);
iInput.push(genData[i].period);
var fuelData = genData[i].generation;
for (var r in fuelData)
var rInput = ;
rInput.push(fuelData[r].fuel);
rInput.push(fuelData[r].generation);
rInput.push(fuelData[r].capacity);
rInput.push(new Date());
res.push(iInput.concat(rInput)); // Modified
shtGen.getRange(shtGen.getLastRow() + 1, 1, res.length, res[0].length).setValues(res); // Added
Other pattern:
As one of other modifications, you can also use the following script. The cost of this script is lower than that of above scripts.
var obj = ### your json object ###;
var res = obj.data.generationByFuel.reduce(function(ar, e)
return ar.concat(e.generation.map(function(f)
return [e.date, e.period, f.fuel, f.generation, f.capacity, new Date()];
));
, );
shtGen.getRange(shtGen.getLastRow() + 1, 1, res.length, res[0].length).setValues(res);
References:
- appendRow()
- setValues()
- concat()
- reduce()
- map
If this was not what you want, please tell me. I would like to modify it.
answered Nov 14 '18 at 8:08
TanaikeTanaike
21.1k21123
21.1k21123
tthanks @Tanaike. i initially went for your 'modified script' approach which worked perfectly. then i had some spare time this morning to take a closer look at what you were doing in the 'other pattern'. that worked just as well, but uses less lines of code... awesome! thanks
– mitch-NZ
Nov 15 '18 at 20:15
@mitch-NZ Thank you for replying. I'm glad your issue was solved. Thank you, too.
– Tanaike
Nov 15 '18 at 22:09
add a comment |
tthanks @Tanaike. i initially went for your 'modified script' approach which worked perfectly. then i had some spare time this morning to take a closer look at what you were doing in the 'other pattern'. that worked just as well, but uses less lines of code... awesome! thanks
– mitch-NZ
Nov 15 '18 at 20:15
@mitch-NZ Thank you for replying. I'm glad your issue was solved. Thank you, too.
– Tanaike
Nov 15 '18 at 22:09
tthanks @Tanaike. i initially went for your 'modified script' approach which worked perfectly. then i had some spare time this morning to take a closer look at what you were doing in the 'other pattern'. that worked just as well, but uses less lines of code... awesome! thanks
– mitch-NZ
Nov 15 '18 at 20:15
tthanks @Tanaike. i initially went for your 'modified script' approach which worked perfectly. then i had some spare time this morning to take a closer look at what you were doing in the 'other pattern'. that worked just as well, but uses less lines of code... awesome! thanks
– mitch-NZ
Nov 15 '18 at 20:15
@mitch-NZ Thank you for replying. I'm glad your issue was solved. Thank you, too.
– Tanaike
Nov 15 '18 at 22:09
@mitch-NZ Thank you for replying. I'm glad your issue was solved. Thank you, too.
– Tanaike
Nov 15 '18 at 22:09
add a comment |
Here's an alternative approach that leverages JSON.parse()
.
Reviver functions are an optional parameter to JSON.parse()
. The cool thing about these functions is that they are invoked with key/value pairs for every node in a JSON object tree using depth-first search.
Run the following snippet to get an idea of how it works:
var jsonString = JSON.stringify(
"resultCode": 200,
"message": "Success",
"generated": "2018-11-14T18:42:02",
"expires": "2018-11-14T18:50:00",
"data":
"generationByFuel": [
"date": "2018-11-14T00:00:00",
"period": 37,
"generation": [
"fuel": "Hydro",
"generation": 1011.298,
"capacity": 2000
,
"fuel": "Wind",
"generation": 2544.788,
"capacity": 3500
]
,
"date": "2018-11-14T00:00:00",
"period": 36,
"generation": [
"fuel": "Hydro",
"generation": 1100,
"capacity": 2000
,
"fuel": "Wind",
"generation": 2500,
"capacity": 3500
]
]
);
JSON.parse(jsonString, function(key, value)
console.log("Key:%s, Value:%o", key, value);
return value;
);
The console log generated by the above script 'walks' the object tree depth-first; it drills down to print out the value of the leaf nodes before it prints the value of the parent nodes. For example: "fuel":"Hydro"
is printed before the "generation":[..., ...,..]
array that contains it.
You can exploit this to generate the requisite data for your spreadsheet as follows:
var jsonString = JSON.stringify(
"resultCode": 200,
"message": "Success",
"generated": "2018-11-14T18:42:02",
"expires": "2018-11-14T18:50:00",
"data":
"generationByFuel": [
"date": "2018-11-14T00:00:00",
"period": 37,
"generation": [
"fuel": "Hydro",
"generation": 1011.298,
"capacity": 2000
,
"fuel": "Wind",
"generation": 2544.788,
"capacity": 3500
]
,
"date": "2018-11-14T00:00:00",
"period": 36,
"generation": [
"fuel": "Hydro",
"generation": 1100,
"capacity": 2000
,
"fuel": "Wind",
"generation": 2500,
"capacity": 3500
]
]
);
var values = ;
var state = ;
JSON.parse(jsonString, function(key, value)
switch(key)
case "generation":
if (Array.isArray(value)) break;
case "date":
case "period":
case "fuel":
case "capacity":
state[key] = value;
break;
if (key === "capacity")
values.push([
state.date,
state.period,
state.fuel,
state.generation,
state.capacity,
new Date()
]);
return value;
);
console.log(values);
The snippet above filters out the desired data as the reviver walks the JSON's object tree and populates the 2D values array in the process. Now all you need to do is call setValues()
on your sheet with the values array.
1
@TheMaster Works for me.
– Dimu Designs
Nov 15 '18 at 0:50
thanks for your answer. whilst i can see that your suggestion works in this instance, i'm not familiar withswitch
,case
, andbreak
statements. until i can research these and understand why this works i've accepted Tanaike's answer to my issue.
– mitch-NZ
Nov 16 '18 at 3:21
@mitch-NZ Here's a good place to start : developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
– Dimu Designs
Nov 16 '18 at 12:40
add a comment |
Here's an alternative approach that leverages JSON.parse()
.
Reviver functions are an optional parameter to JSON.parse()
. The cool thing about these functions is that they are invoked with key/value pairs for every node in a JSON object tree using depth-first search.
Run the following snippet to get an idea of how it works:
var jsonString = JSON.stringify(
"resultCode": 200,
"message": "Success",
"generated": "2018-11-14T18:42:02",
"expires": "2018-11-14T18:50:00",
"data":
"generationByFuel": [
"date": "2018-11-14T00:00:00",
"period": 37,
"generation": [
"fuel": "Hydro",
"generation": 1011.298,
"capacity": 2000
,
"fuel": "Wind",
"generation": 2544.788,
"capacity": 3500
]
,
"date": "2018-11-14T00:00:00",
"period": 36,
"generation": [
"fuel": "Hydro",
"generation": 1100,
"capacity": 2000
,
"fuel": "Wind",
"generation": 2500,
"capacity": 3500
]
]
);
JSON.parse(jsonString, function(key, value)
console.log("Key:%s, Value:%o", key, value);
return value;
);
The console log generated by the above script 'walks' the object tree depth-first; it drills down to print out the value of the leaf nodes before it prints the value of the parent nodes. For example: "fuel":"Hydro"
is printed before the "generation":[..., ...,..]
array that contains it.
You can exploit this to generate the requisite data for your spreadsheet as follows:
var jsonString = JSON.stringify(
"resultCode": 200,
"message": "Success",
"generated": "2018-11-14T18:42:02",
"expires": "2018-11-14T18:50:00",
"data":
"generationByFuel": [
"date": "2018-11-14T00:00:00",
"period": 37,
"generation": [
"fuel": "Hydro",
"generation": 1011.298,
"capacity": 2000
,
"fuel": "Wind",
"generation": 2544.788,
"capacity": 3500
]
,
"date": "2018-11-14T00:00:00",
"period": 36,
"generation": [
"fuel": "Hydro",
"generation": 1100,
"capacity": 2000
,
"fuel": "Wind",
"generation": 2500,
"capacity": 3500
]
]
);
var values = ;
var state = ;
JSON.parse(jsonString, function(key, value)
switch(key)
case "generation":
if (Array.isArray(value)) break;
case "date":
case "period":
case "fuel":
case "capacity":
state[key] = value;
break;
if (key === "capacity")
values.push([
state.date,
state.period,
state.fuel,
state.generation,
state.capacity,
new Date()
]);
return value;
);
console.log(values);
The snippet above filters out the desired data as the reviver walks the JSON's object tree and populates the 2D values array in the process. Now all you need to do is call setValues()
on your sheet with the values array.
1
@TheMaster Works for me.
– Dimu Designs
Nov 15 '18 at 0:50
thanks for your answer. whilst i can see that your suggestion works in this instance, i'm not familiar withswitch
,case
, andbreak
statements. until i can research these and understand why this works i've accepted Tanaike's answer to my issue.
– mitch-NZ
Nov 16 '18 at 3:21
@mitch-NZ Here's a good place to start : developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
– Dimu Designs
Nov 16 '18 at 12:40
add a comment |
Here's an alternative approach that leverages JSON.parse()
.
Reviver functions are an optional parameter to JSON.parse()
. The cool thing about these functions is that they are invoked with key/value pairs for every node in a JSON object tree using depth-first search.
Run the following snippet to get an idea of how it works:
var jsonString = JSON.stringify(
"resultCode": 200,
"message": "Success",
"generated": "2018-11-14T18:42:02",
"expires": "2018-11-14T18:50:00",
"data":
"generationByFuel": [
"date": "2018-11-14T00:00:00",
"period": 37,
"generation": [
"fuel": "Hydro",
"generation": 1011.298,
"capacity": 2000
,
"fuel": "Wind",
"generation": 2544.788,
"capacity": 3500
]
,
"date": "2018-11-14T00:00:00",
"period": 36,
"generation": [
"fuel": "Hydro",
"generation": 1100,
"capacity": 2000
,
"fuel": "Wind",
"generation": 2500,
"capacity": 3500
]
]
);
JSON.parse(jsonString, function(key, value)
console.log("Key:%s, Value:%o", key, value);
return value;
);
The console log generated by the above script 'walks' the object tree depth-first; it drills down to print out the value of the leaf nodes before it prints the value of the parent nodes. For example: "fuel":"Hydro"
is printed before the "generation":[..., ...,..]
array that contains it.
You can exploit this to generate the requisite data for your spreadsheet as follows:
var jsonString = JSON.stringify(
"resultCode": 200,
"message": "Success",
"generated": "2018-11-14T18:42:02",
"expires": "2018-11-14T18:50:00",
"data":
"generationByFuel": [
"date": "2018-11-14T00:00:00",
"period": 37,
"generation": [
"fuel": "Hydro",
"generation": 1011.298,
"capacity": 2000
,
"fuel": "Wind",
"generation": 2544.788,
"capacity": 3500
]
,
"date": "2018-11-14T00:00:00",
"period": 36,
"generation": [
"fuel": "Hydro",
"generation": 1100,
"capacity": 2000
,
"fuel": "Wind",
"generation": 2500,
"capacity": 3500
]
]
);
var values = ;
var state = ;
JSON.parse(jsonString, function(key, value)
switch(key)
case "generation":
if (Array.isArray(value)) break;
case "date":
case "period":
case "fuel":
case "capacity":
state[key] = value;
break;
if (key === "capacity")
values.push([
state.date,
state.period,
state.fuel,
state.generation,
state.capacity,
new Date()
]);
return value;
);
console.log(values);
The snippet above filters out the desired data as the reviver walks the JSON's object tree and populates the 2D values array in the process. Now all you need to do is call setValues()
on your sheet with the values array.
Here's an alternative approach that leverages JSON.parse()
.
Reviver functions are an optional parameter to JSON.parse()
. The cool thing about these functions is that they are invoked with key/value pairs for every node in a JSON object tree using depth-first search.
Run the following snippet to get an idea of how it works:
var jsonString = JSON.stringify(
"resultCode": 200,
"message": "Success",
"generated": "2018-11-14T18:42:02",
"expires": "2018-11-14T18:50:00",
"data":
"generationByFuel": [
"date": "2018-11-14T00:00:00",
"period": 37,
"generation": [
"fuel": "Hydro",
"generation": 1011.298,
"capacity": 2000
,
"fuel": "Wind",
"generation": 2544.788,
"capacity": 3500
]
,
"date": "2018-11-14T00:00:00",
"period": 36,
"generation": [
"fuel": "Hydro",
"generation": 1100,
"capacity": 2000
,
"fuel": "Wind",
"generation": 2500,
"capacity": 3500
]
]
);
JSON.parse(jsonString, function(key, value)
console.log("Key:%s, Value:%o", key, value);
return value;
);
The console log generated by the above script 'walks' the object tree depth-first; it drills down to print out the value of the leaf nodes before it prints the value of the parent nodes. For example: "fuel":"Hydro"
is printed before the "generation":[..., ...,..]
array that contains it.
You can exploit this to generate the requisite data for your spreadsheet as follows:
var jsonString = JSON.stringify(
"resultCode": 200,
"message": "Success",
"generated": "2018-11-14T18:42:02",
"expires": "2018-11-14T18:50:00",
"data":
"generationByFuel": [
"date": "2018-11-14T00:00:00",
"period": 37,
"generation": [
"fuel": "Hydro",
"generation": 1011.298,
"capacity": 2000
,
"fuel": "Wind",
"generation": 2544.788,
"capacity": 3500
]
,
"date": "2018-11-14T00:00:00",
"period": 36,
"generation": [
"fuel": "Hydro",
"generation": 1100,
"capacity": 2000
,
"fuel": "Wind",
"generation": 2500,
"capacity": 3500
]
]
);
var values = ;
var state = ;
JSON.parse(jsonString, function(key, value)
switch(key)
case "generation":
if (Array.isArray(value)) break;
case "date":
case "period":
case "fuel":
case "capacity":
state[key] = value;
break;
if (key === "capacity")
values.push([
state.date,
state.period,
state.fuel,
state.generation,
state.capacity,
new Date()
]);
return value;
);
console.log(values);
The snippet above filters out the desired data as the reviver walks the JSON's object tree and populates the 2D values array in the process. Now all you need to do is call setValues()
on your sheet with the values array.
var jsonString = JSON.stringify(
"resultCode": 200,
"message": "Success",
"generated": "2018-11-14T18:42:02",
"expires": "2018-11-14T18:50:00",
"data":
"generationByFuel": [
"date": "2018-11-14T00:00:00",
"period": 37,
"generation": [
"fuel": "Hydro",
"generation": 1011.298,
"capacity": 2000
,
"fuel": "Wind",
"generation": 2544.788,
"capacity": 3500
]
,
"date": "2018-11-14T00:00:00",
"period": 36,
"generation": [
"fuel": "Hydro",
"generation": 1100,
"capacity": 2000
,
"fuel": "Wind",
"generation": 2500,
"capacity": 3500
]
]
);
JSON.parse(jsonString, function(key, value)
console.log("Key:%s, Value:%o", key, value);
return value;
);
var jsonString = JSON.stringify(
"resultCode": 200,
"message": "Success",
"generated": "2018-11-14T18:42:02",
"expires": "2018-11-14T18:50:00",
"data":
"generationByFuel": [
"date": "2018-11-14T00:00:00",
"period": 37,
"generation": [
"fuel": "Hydro",
"generation": 1011.298,
"capacity": 2000
,
"fuel": "Wind",
"generation": 2544.788,
"capacity": 3500
]
,
"date": "2018-11-14T00:00:00",
"period": 36,
"generation": [
"fuel": "Hydro",
"generation": 1100,
"capacity": 2000
,
"fuel": "Wind",
"generation": 2500,
"capacity": 3500
]
]
);
JSON.parse(jsonString, function(key, value)
console.log("Key:%s, Value:%o", key, value);
return value;
);
var jsonString = JSON.stringify(
"resultCode": 200,
"message": "Success",
"generated": "2018-11-14T18:42:02",
"expires": "2018-11-14T18:50:00",
"data":
"generationByFuel": [
"date": "2018-11-14T00:00:00",
"period": 37,
"generation": [
"fuel": "Hydro",
"generation": 1011.298,
"capacity": 2000
,
"fuel": "Wind",
"generation": 2544.788,
"capacity": 3500
]
,
"date": "2018-11-14T00:00:00",
"period": 36,
"generation": [
"fuel": "Hydro",
"generation": 1100,
"capacity": 2000
,
"fuel": "Wind",
"generation": 2500,
"capacity": 3500
]
]
);
var values = ;
var state = ;
JSON.parse(jsonString, function(key, value)
switch(key)
case "generation":
if (Array.isArray(value)) break;
case "date":
case "period":
case "fuel":
case "capacity":
state[key] = value;
break;
if (key === "capacity")
values.push([
state.date,
state.period,
state.fuel,
state.generation,
state.capacity,
new Date()
]);
return value;
);
console.log(values);
var jsonString = JSON.stringify(
"resultCode": 200,
"message": "Success",
"generated": "2018-11-14T18:42:02",
"expires": "2018-11-14T18:50:00",
"data":
"generationByFuel": [
"date": "2018-11-14T00:00:00",
"period": 37,
"generation": [
"fuel": "Hydro",
"generation": 1011.298,
"capacity": 2000
,
"fuel": "Wind",
"generation": 2544.788,
"capacity": 3500
]
,
"date": "2018-11-14T00:00:00",
"period": 36,
"generation": [
"fuel": "Hydro",
"generation": 1100,
"capacity": 2000
,
"fuel": "Wind",
"generation": 2500,
"capacity": 3500
]
]
);
var values = ;
var state = ;
JSON.parse(jsonString, function(key, value)
switch(key)
case "generation":
if (Array.isArray(value)) break;
case "date":
case "period":
case "fuel":
case "capacity":
state[key] = value;
break;
if (key === "capacity")
values.push([
state.date,
state.period,
state.fuel,
state.generation,
state.capacity,
new Date()
]);
return value;
);
console.log(values);
edited Nov 15 '18 at 0:51
answered Nov 14 '18 at 16:46
Dimu DesignsDimu Designs
2,7032411
2,7032411
1
@TheMaster Works for me.
– Dimu Designs
Nov 15 '18 at 0:50
thanks for your answer. whilst i can see that your suggestion works in this instance, i'm not familiar withswitch
,case
, andbreak
statements. until i can research these and understand why this works i've accepted Tanaike's answer to my issue.
– mitch-NZ
Nov 16 '18 at 3:21
@mitch-NZ Here's a good place to start : developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
– Dimu Designs
Nov 16 '18 at 12:40
add a comment |
1
@TheMaster Works for me.
– Dimu Designs
Nov 15 '18 at 0:50
thanks for your answer. whilst i can see that your suggestion works in this instance, i'm not familiar withswitch
,case
, andbreak
statements. until i can research these and understand why this works i've accepted Tanaike's answer to my issue.
– mitch-NZ
Nov 16 '18 at 3:21
@mitch-NZ Here's a good place to start : developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
– Dimu Designs
Nov 16 '18 at 12:40
1
1
@TheMaster Works for me.
– Dimu Designs
Nov 15 '18 at 0:50
@TheMaster Works for me.
– Dimu Designs
Nov 15 '18 at 0:50
thanks for your answer. whilst i can see that your suggestion works in this instance, i'm not familiar with
switch
, case
, and break
statements. until i can research these and understand why this works i've accepted Tanaike's answer to my issue.– mitch-NZ
Nov 16 '18 at 3:21
thanks for your answer. whilst i can see that your suggestion works in this instance, i'm not familiar with
switch
, case
, and break
statements. until i can research these and understand why this works i've accepted Tanaike's answer to my issue.– mitch-NZ
Nov 16 '18 at 3:21
@mitch-NZ Here's a good place to start : developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
– Dimu Designs
Nov 16 '18 at 12:40
@mitch-NZ Here's a good place to start : developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
– Dimu Designs
Nov 16 '18 at 12:40
add a comment |
I think you missed the "genData[i]" within the for loop for var 'r'. Try this.
for (var i in genData)
var iInput = ;
iInput.push(genData[i].date);
iInput.push(genData[i].period);
for (var r in fuelData)
var rInput = ;
rInput.push(genData[i].fuelData[r].fuel);
rInput.push(genData[i].fuelData[r].capacity);
rInput.push(genData[i].fuelData[r].generation);
rInput.push(now = new Date());
shtGen.appendRow(iInput + rInput);
Also it can be how you are storing the response in your model class.
thanks for your answer. before submitting my question i had tried this as well as numerous variations of this. however the best result i could get was for the script to complete without rerturning an error, but it would only outputdate
andperiod
.
– mitch-NZ
Nov 15 '18 at 21:53
add a comment |
I think you missed the "genData[i]" within the for loop for var 'r'. Try this.
for (var i in genData)
var iInput = ;
iInput.push(genData[i].date);
iInput.push(genData[i].period);
for (var r in fuelData)
var rInput = ;
rInput.push(genData[i].fuelData[r].fuel);
rInput.push(genData[i].fuelData[r].capacity);
rInput.push(genData[i].fuelData[r].generation);
rInput.push(now = new Date());
shtGen.appendRow(iInput + rInput);
Also it can be how you are storing the response in your model class.
thanks for your answer. before submitting my question i had tried this as well as numerous variations of this. however the best result i could get was for the script to complete without rerturning an error, but it would only outputdate
andperiod
.
– mitch-NZ
Nov 15 '18 at 21:53
add a comment |
I think you missed the "genData[i]" within the for loop for var 'r'. Try this.
for (var i in genData)
var iInput = ;
iInput.push(genData[i].date);
iInput.push(genData[i].period);
for (var r in fuelData)
var rInput = ;
rInput.push(genData[i].fuelData[r].fuel);
rInput.push(genData[i].fuelData[r].capacity);
rInput.push(genData[i].fuelData[r].generation);
rInput.push(now = new Date());
shtGen.appendRow(iInput + rInput);
Also it can be how you are storing the response in your model class.
I think you missed the "genData[i]" within the for loop for var 'r'. Try this.
for (var i in genData)
var iInput = ;
iInput.push(genData[i].date);
iInput.push(genData[i].period);
for (var r in fuelData)
var rInput = ;
rInput.push(genData[i].fuelData[r].fuel);
rInput.push(genData[i].fuelData[r].capacity);
rInput.push(genData[i].fuelData[r].generation);
rInput.push(now = new Date());
shtGen.appendRow(iInput + rInput);
Also it can be how you are storing the response in your model class.
answered Nov 14 '18 at 6:50
truespantruespan
3118
3118
thanks for your answer. before submitting my question i had tried this as well as numerous variations of this. however the best result i could get was for the script to complete without rerturning an error, but it would only outputdate
andperiod
.
– mitch-NZ
Nov 15 '18 at 21:53
add a comment |
thanks for your answer. before submitting my question i had tried this as well as numerous variations of this. however the best result i could get was for the script to complete without rerturning an error, but it would only outputdate
andperiod
.
– mitch-NZ
Nov 15 '18 at 21:53
thanks for your answer. before submitting my question i had tried this as well as numerous variations of this. however the best result i could get was for the script to complete without rerturning an error, but it would only output
date
and period
.– mitch-NZ
Nov 15 '18 at 21:53
thanks for your answer. before submitting my question i had tried this as well as numerous variations of this. however the best result i could get was for the script to complete without rerturning an error, but it would only output
date
and period
.– mitch-NZ
Nov 15 '18 at 21:53
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53294328%2fhow-to-extract-multi-layered-array-from-json%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
fuelData
should be assigned before the inner loopfuelData = genData[i].generation
– Slai
Nov 14 '18 at 7:01
@Slai my apologies, i do declare
iInput
andrInput
but was getting red flags saying that i was posting more code than text, so removed. these are my declarations...var genData = json['data']['generationByFuel'];
var fuelData = json['data']['generationByFuel']['generation'];
– mitch-NZ
Nov 15 '18 at 1:57