0

Edit: using google apps script, these are objects that are passed back from their functions. When I say logged I mean the result of the return function is logged in GAS.

I have objects that serve as profiles for a larger script, and I was trying to generate a larger profile programmatically.

When called and logged:

[ { name: "a1",
        functionName:"functionA",
        options:{something:"a1run"}
      },
      { name: "a2",
        functionName:"functionA",
        options:{something:"a2run"}
      },
      { name: "a3",
        functionName:"functionA",
        options:{something:"a3run"}
      }
    ]

Shows up in the log as this:

[{
        functionName = functionA,
        name = a1,
        options = {
            something = a1run
        }
    },
    }, {
        functionName = functionA,
        name = a2,
        options = {
            something = a2run
        }
    }, {
        functionName = functionA,
        name = a3,
        options = {
            something = a3run
        }
    }]

you'll note that all of the quotation marks disappeared.

Yet when I call an almost identical function where I generated each part of the object with a for loop (this)

  var s1 = "";
  for (var i=0; i<5;i++)
    {
      var newString = '';
      newString += '{ name: "a'+i+'",';
      newString +=   'functionName: "functionA",';
      newString +=   'options:{something: "a'+i+'run"} },';
      s1+= newString;

    }//for loop

The logged result of the function is this:

[{
        name: "a0",
        functionName: "functionA",
        options: {
            something: "a0run"
        }
    }, {
        name: "a1",
        functionName: "functionA",
        options: {
            something: "a1run"
        }
    }, {
        name: "a2",
        functionName: "functionA",
        options: {
            something: "a2run"
        }
    }, {
        name: "a3",
        functionName: "functionA",
        options: {
            something: "a3run"
        }
    }, {
        name: "a4",
        functionName: "functionA",
        options: {
            something: "a4run"
        }
    }, ]

This is a problem because the initial formatting does work as a profile, and the second one does not. What aspect of JavaScript objects do I need to understand? I didn't think it would make a difference because this object goes through a JSON.stringify when it is used but I was wrong.

My question isn't just how I change it so that it is processed the same way, but why one is being treated differently from the other.

J. G.
  • 1,922
  • 1
  • 11
  • 21
  • 2
    You have an extra comma in the for-loop generated string. – VLAZ May 07 '19 at 19:13
  • Please post the code that's producing the undesired output. You say it's showing up in the log, how are you logging it? – Barmar May 07 '19 at 19:15
  • dota2pro the expected output is that they should be the same (no quotation marks) as function exports, the part that explained that these are function returns was deleted incorrectly – J. G. May 07 '19 at 19:15
  • `{ something = a1run }` is not valid JavaScript syntax for an object. – Barmar May 07 '19 at 19:16
  • 2
    I guess the log is intended to be informative to a human, not something that can be parsed as a JavaScript literal. – Barmar May 07 '19 at 19:17
  • 1
    If the log which is using in your question is [``Logger.log()``](https://developers.google.com/apps-script/reference/base/logger), the object like ``{"key": "value"}`` is displayed as ``{key=value}``. Namely, it indicates that the value is an object. This is the specification of Google Apps Script. From this situation, I think that @Rubén 's answer is useful for your question. By the way, in your script, when ``s1 = s1.slice(0, -1);`` is put after the for loop and ``s1`` is put in ``[]`` as ``[s1]``, the string can be parsed by ``JSON.parse(s1)`` and it can be used as an object. – Tanaike May 07 '19 at 23:26

2 Answers2

2

This is not the correct way of creating a JSON array

you should do something like this and forget about creating a string of JSON array

let output = [];
for (var i = 0; i < 5; i++) {
  output.push({
    name: "a" + i,
    functionName: "functionA",
    options: {
      something: "a" + i + "run"
    }
  });

}

console.log(output);
dota2pro
  • 7,220
  • 7
  • 44
  • 79
1

Instead of using Logger or the Google Apps Script built-id debugger to "print" your JSON in order to debug it if you are able to use Stackdriver use it or use the HTML Service to print the JSON object to your web browser console instead.

The above becase the Log view (View > Logs), as you already found, not always print JSON objects correctly.

If you want to use Logger, first you should convert to JSON object to string. In most cases using JSON.stringify(...) will work fine.

References

Rubén
  • 34,714
  • 9
  • 70
  • 166