'using JSON.parse in DocumentApp
It seems I can't parse a json file in a Document script despite it's possible via a DriveApp script.
Here is a reproducible example :
edit a json valid file and save it as a .txt file in your google drive :
{
"text_prompts": {
"0": [
"beautiful painting of front head of a woman, renaissance style:9",
"colorful:10"
]
},
"image_prompts": {},
"clip_guidance_scale": 1000,
"tv_scale": 0,
"range_scale": 150,
"sat_scale": 0,
"cutn_batches": 4,
"max_frames": 10000,
"interp_spline": "Linear",
"init_image": "drive/MyDrive/AI/Disco_Diffusion/init_images/patricienne.png",
"init_scale": 15000
}
Notice the first "text_prompts" record. This is what I want to extract from the json file. Obtain the hash ID of this file via its link and note it.
Now create a Google Apps Script :
const FICHIER_ID = HASH_ID_OF_THE_JSON_FILE
function statsfichier() {
const fichier = DriveApp.getFileById(FICHIER_ID);
let content = fichier.getBlob().getDataAsString();
let json = JSON.parse(content);
Logger.log(fichier.getName());
Logger.log(content)
Logger.log(json.text_prompts)
}
Validate it, and launch it. You should obtain the "text_prompts" record of the json file in the logs, like this :
10:12:08 Infos {0=[beautiful painting of front head of a woman, renaissance style:9, colorful:10]}
Now, create an empty document in your drive. Create an Apps Script in it as follows :
const UI = DocumentApp.getUi();
function onOpen() {
UI.createMenu('DD')
.addItem("proof by json", "prpjsn")
.addToUi();
}
function prpjsn() {
const ficID = HASH_ID_OF_THE_JSON_FILE;
const fichier = DriveApp.getFileById(ficID);
let content = fichier.getBlob().getDataAsString();
let json = JSON.parse(content);
let prompts = json.text_prompts;
UI.alert(content); // this is ok, I obtain the json file content
UI.alert(json); // [object Object]
UI.alert(prompts); // [object Object]
UI.alert(String(json)); // [object Object]
UI.alert(String(prompts)); // [object Object]
}
Reload the document, launch the script via the document menu, validate the script, launch it again.
The first alert shows that the file is appropriately read, but no json extraction follows.
How to do it ?
Solution 1:[1]
I'm getting SyntaxError: Unexpected token % in JSON at position 0. So it's not returning valid JSON don't think it's a good idea
Solution 2:[2]
I believe your current issue and your goal is as follows.
- You want to show the value of
text_prompts
in your JSON data. But, when your bottom script is used,[object Object]
is shown. This is your current issue. - In your goal, you want to show the value of
text_prompts
instead of[object Object]
.
If my understanding is correct, how about the following modification?
I think that in your bottom script, content
is the string value. But, json
and prompts
are an object. I think that this is the reason for your issue. In order to show the value with UI.alert(value)
, how about the following modification? In this modification, the object is converted to the string value using JSON.stringify()
.
From:
UI.alert(json); // [object Object]
UI.alert(prompts); // [object Object]
To:
UI.alert(JSON.stringify(json));
UI.alert(JSON.stringify(prompts));
Note:
- For example, if you want to show the value of
beautiful painting of front head of a woman, renaissance style:9
, you can also useUI.alert(json.text_prompts["0"][0]);
.
Reference:
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Cooper |
Solution 2 | Tanaike |