How to specify array data with GC CLI --transformstr

I have created a batch file that outputs a .csv file from the GC CLI list command.
Most of the data is being output as I expect using the "--transformstr" gotmpl description.

However, I am having trouble retrieving the array specified data contained in the "gc telephony providers edges phones list" output results.

As an example, I have specified "{{(index .lines 0).state}}" as transform for the key "lines[0].state" in the json, but I get the following error

Error Message: Error applying parsed template to data
Error applying parsed template to data object: template: tmpl:1:318: executing "tmpl" at <index .lines 1>: error calling index: reflect: slice index out of range

Am I specifying this incorrectly?
Has anyone been able to successfully retrieve such data?

Hi Yusuke

It looks like your syntax is correct. Perhaps your mistake is not including the -a flag in your command?

For example, the following command works okay for me:

gc telephony providers edges phones list -a --transformstr="{{ range . }} {{ (index .lines 0).state }} {{ end}}"

Hi Charlie

Thanks for your information.

My command has the -a option specification.
I simplified the command and now I have more details on the cause.

It does not seem to be a Syntax problem as you say.
I simplified the command and specified only line[0].state and was able to get the data.
The command that resulted in an error also included data from line[1].state in the command.

line[0].state has data for all phones.
line[1].state has data for only a few phones.

I am assuming that this error occurs when the output result contains data that has no value.

If the key name does not contain an array*, the value <no value> is recorded if no data exists.
Does it not have the same behavior?

*e.g.: defaultScripts.CALL.id in Queue settings

I'm not certain how the go templating language handles null or empty values, but if the problem is coming from some phones not having more than one line, I would recommend looping over the lines array using the {{range}} tags instead of indexing.

Thanks for your advice.
I have implemented it with Range.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.