-1

I was trying to use the method found here (see most up-voted answer): Google Apps Script Fastest way to find a row?

I currently use this while it does work I wanted to try the above linked method yet when I replace the below code

function AutoPopulate (evalue)
{
  
 //uses google drive file irretator reads in JSON file and parses it to a Javascript object that we can work with
  
  var iter = DriveApp.getFilesByName("units.json");
  // iterate through all the files named units.json
  while (iter.hasNext()) {
    // define a File object variable and set the Media Tyep
    var file = iter.next();
    var jsonFile = file.getBlob().getDataAsString();
    // log the contents of the file
    //Logger.log(jsonFile);
    
  }
  var UnitDatabase = JSON.parse(jsonFile);
  
//Logger.log(UnitDatabase);
//Logger.log(UnitDatabase[1027]);
return UnitDatabase[evalue];
}

WITH THIS CODE:

function AutoPopulate (evalue)
    {
    //this method did not work for me but should have according to stackflow answer linked above I am trying to understand why or how I can find out why it may have thrown an error
        var  jsonFile = DriveApp.getFilesByName("units.json").next(),
        UnitDatabase = UnitDatabase.getBlob().getDataAsString(); 
         return UnitDatabase[evalue];
}

I get an error in the excecution indicating that there is a % at postion 0 in the JSON, between the methods I dont alter the JSON file in anyway so I dont understand why does the top method work but the bottom one does not?

For further information the idea behind the code is that I have a list of Unit numbers and model numbers that are in a spreadsheet. I then convert this to a JSON file, this however is only done when a new unit is added to the fleet. As I learned one can parse a whole JSON file into a javascript object which makes working with the data set much faster. This javascript object is used so that when a user enters a UNIT# the MODEL# is auto populated based on the JSON file.

I cannot share the JSON file as it contains client information.

MK Spindel
  • 23
  • 7
  • 1
    The question is quite unclear. What is your attempt and where is the error. What works and what doesn't? I've closed the other question as a duplicate. Test and see whether the linked westernm question works out for you. – TheMaster Aug 11 '20 at 20:46
  • Which line throws you the error? What is the content of your json file? – ziganotschka Aug 12 '20 at 10:12
  • @TheMaster I dont see a link? or a comment from westernm? I will edit the question to be more clear then sorry for any confusion – MK Spindel Aug 12 '20 at 12:30
  • That was a typo. See top of your linked question. – TheMaster Aug 12 '20 at 12:37
  • @TheMaster I rephrased the question please have a 2nd look you will see the link doesnt answer my question hence why I asked this one here. Please do advise what I can do to help find an answer – MK Spindel Aug 12 '20 at 12:40
  • @ziganotschka as I state in the question the error states postion 0 of the JSON file has a % yet it works in my top code just not in the code that was suggested via an answer to another stackflow question, therefore I believe am missing something from a syntax or logic perspective and I am looking to get a better idea of what it is I am missing – MK Spindel Aug 12 '20 at 12:43
  • There is a link at the very top of your linked question. Open your linked question > see top header of that question. – TheMaster Aug 12 '20 at 12:47
  • @MKSpindel my quesiton was which code line throws you this error. Is it the line `var UnitDatabase = JSON.parse(jsonFile);`? – ziganotschka Aug 12 '20 at 12:47
  • @TheMaster I see what you mean now sorry I misunderstood the first time checking it out now thank you – MK Spindel Aug 12 '20 at 12:48
  • @ziganotschka it is this line `var jsonFile = DriveApp.getFilesByName("units.json").next(),` – MK Spindel Aug 12 '20 at 12:50
  • @TheMaster while that link does answer regarding what is the fastest method it does not answer my question unfortunately and is not helpful in this instance :(.I have rephrased my question since last night as it needed it as I was not being as clear as I thought I was. That said is it still that unclear what I am asking? should I edit it more? Or should I just delete my question? as I seemed to have caused confusion here which was not intended at all and I apologize I am really new at asking questions so I am terribly sorry for messing it all up. – MK Spindel Aug 12 '20 at 13:16

1 Answers1

0

Your code does not work for two reasons:

  1. You have a typo in the line UnitDatabase = UnitDatabase.getBlob()... - it should be UnitDatabase = jsonFile.getBlob()...
  2. If you want to retrieve a nested object from a json file - you need to parse the JSOn - otherwise it is considered a string and you can not access the nested structure

Modified working code:

function AutoPopulate2 (evalue)
{
  var  jsonFile = DriveApp.getFilesByName("units.json").next();
  var UnitDatabase = JSON.parse(jsonFile.getBlob().getDataAsString()); 
  return UnitDatabase[evalue];
}

Mind that this code will only work if you have a "units.json" file on your drive and if evalue is a valid 1st-level nested object of this json.

ziganotschka
  • 25,866
  • 2
  • 16
  • 33
  • Going to try this right away, thank you for the tip I did not realize the mistake, regarding 1st-level nested object of the json file to better understand you here am I correct in saying if evalue contains the KEY it would spit out its corresponding VALUE pair. Or am I misunderstanding what you mean by 1st-level nested object? – MK Spindel Aug 12 '20 at 13:43
  • 1
    FYI this worked like a charm I knew I just screwed up the syntax logic somehow thank you very much for your explanation! I had tried it with `JSON.parse` as well prior but kept getting an error as well which was because I didnt call `jsonFile` instead i called `UnitDatabase` during the `getBlob()` call and now I know why. Thank you so much for showing me the light as it were hahaha – MK Spindel Aug 12 '20 at 13:48
  • In addition I just figured out that if one opens a .JSON in google drive google drive converts it to a google docs file which while I was troubleshooting I did not realize and it further added to my confusion regarding the odd debug results in my logs. To anyone working with .JSON files in Google drive download them to your desktop and open with a text editor to check on their contents. Avoid opening with googledocs as it will convert your .JSON file to a googledocs file which means the next `JSON.parse` will throw an error. – MK Spindel Aug 12 '20 at 13:56