Home > OS >  using JSON.parse in DocumentApp
using JSON.parse in DocumentApp

Time:05-14

I want to adapt in my document UI a piece of code that works well in Driveapp.

Here is my code in Driveapp :

const FICHIER_ID = "12fhPfjQOOCwzGniO8KFrh8TR4kcOyjlq"
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)
}

This works as intended. So I want to extract the same information via my Document UI (my final goal is to insertText or appendText, using alert is just a debug step here) :

const UI = DocumentApp.getUi();

function onOpen() {
  UI.createMenu('DD')
    .addItem("preuve par Json", "prpjsn")
    .addToUi();
}

function prpjsn() {
  const ficID = "12fhPfjQOOCwzGniO8KFrh8TR4kcOyjlq";
  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 content

  UI.alert(json); // [object Object]
  UI.alert(prompts); // [object Object]
  
  UI.alert(String(json)); // [object Object]
  UI.alert(String(prompts)); // [object Object]
}

I'm confused, what did I miss ?

CodePudding user response:

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

CodePudding user response:

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("preuve par 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 content

  UI.alert(json); // [object Object]
  UI.alert(prompts); // [object Object]
  
  UI.alert(String(json)); // [object Object]
  UI.alert(String(prompts)); // [object Object]
}

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 ?

  • Related