问题
I have the following folder tree:
base
├── aaa
│ ├── aaa 111
│ ├── aaa 222
│ └── aaa 333
├── bbb
│ ├── bbb 111
│ ├── bbb 222
│ └── bbb 333
│ ├── yyy 111
│ ├── yyy 222
│ └── yyy 333
└── ccc
├── ccc 111
├── ccc 222
└── ccc 333
The JSON file document.json.txt for the tree above, which is uploaded to my Google Drive, is like so:
[
{"type":"directory","name":"BASE","contents":[
{"type":"directory","name":"AAA","contents":[
{"type":"directory","name":"aaa 111","contents":[
]},
{"type":"directory","name":"aaa 222","contents":[
]},
{"type":"directory","name":"aaa 333","contents":[
]}
]},
{"type":"directory","name":"BBB","contents":[
{"type":"directory","name":"bbb 111","contents":[
]},
{"type":"directory","name":"bbb 222","contents":[
]},
{"type":"directory","name":"bbb 333","contents":[
{"type":"directory","name":"yyy 111","contents":[
]},
{"type":"directory","name":"yyy 222","contents":[
]},
{"type":"directory","name":"yyy 333","contents":[
]}
]}
]},
{"type":"directory","name":"CCC","contents":[
{"type":"directory","name":"ccc 111","contents":[
]},
{"type":"directory","name":"ccc 222","contents":[
]},
{"type":"directory","name":"ccc 333","contents":[
]}
]}
]}
]
The following script (Load or Read a JSON from local in Google AppScript) works fine to retrieve the whole contents of the above JSON file from Google Drive:
function getFileContent() {
var fileName = "document.json.txt";
var files = DriveApp.getFilesByName(fileName);
if (files.hasNext()) {
var file = files.next();
var content = file.getBlob().getDataAsString();
var json = JSON.parse(content);
Logger.log(json);
}
}
How shoud I modify the script to get the contents of bbb 333 directory only?
回答1:
- You want to retrieve the value of
contentsin the object withnameofbbb 333from the file ofdocument.json.txtwhich is the JSON object.- You want to retrieve the following values.
[[{"type":"directory","name":"yyy 111","contents":[]},{"type":"directory","name":"yyy 222","contents":[]},{"type":"directory","name":"yyy 333","contents":[]}]]
- You want to achieve this by modifying your script of Google Apps Script.
If my understanding is correct, how about this answer? Please think of this as just one of several possible answers.
Modification points:
- In this answer, the retrieved JSON object is traversed and the expected values are retrieved. For this, I added the function of
getContents.
Modified script:
function getFileContent() {
// I added below function
var getContents = function getContents(data, contents) {
for (var key in data) {
if (data[key].name === "bbb 333") contents.push(data[key].contents);
if (typeof data[key] === "object") getContents(data[key], contents);
}
return contents;
}
var fileName = "document.json.txt";
var files = DriveApp.getFilesByName(fileName);
if (files.hasNext()) {
var file = files.next();
var content = file.getBlob().getDataAsString();
var json = JSON.parse(content);
// Logger.log(json);
var res = getContents(json, []); // Added
Logger.log(res) // Added
}
}
Result:
When above script is run, the following result can be seen at the log.
[
[
{
"type": "directory",
"name": "yyy 111",
"contents": []
},
{
"type": "directory",
"name": "yyy 222",
"contents": []
},
{
"type": "directory",
"name": "yyy 333",
"contents": []
}
]
]
If I misunderstood your question and this was not the result you want, I apologize.
来源:https://stackoverflow.com/questions/59325466/how-to-retrieve-json-specific-content-from-google-drive-with-google-apps-script