Parse output in code service error

(Optim Birou) #1

hi there,
i tried to use the code service for a query in google sheet with multiple columns.
I found a script in flowxo community which i changed for my purpose and it looks like this:


var resultCount = {{search_rows.results_count}};
var row = inputs.row.split("\n");
var i;
var searchTermColor = inputs.searchTermColor;
var searchTermTipar = inputs.searchTermTipar;
var searchTermCantitate = inputs.searchTermCantitate;
var searchTermFormat = inputs.searchTermFormat;
var regExColor = new RegExp(searchTermColor, “gi”);
var regExTipar = new RegExp(searchTermTipar, “gi”);
var regExFormat = new RegExp(searchTermFormat, “gi”);
var regExCantitate = new RegExp(searchTermCantitate, “gi”);
var searchColor = inputs.searchColumnColor.split("\n");
var searchFormat = inputs.searchColumnFormat.split("\n");
var searchTipar = inputs.searchColumnTipar.split("\n");
var searchCantitate = inputs.searchColumnCantitate.split("\n");
var maxCantitate = 100000;
var data = [];

for(i = 0; i <resultCount; i++) {
if (Number(maxCantitate) >= Number(searchCantitate[i]) && Number(searchTermCantitate) <= Number(searchCantitate[i])) {
var maxCantitate = searchCantitate[i];
}
}

for(i = 0; i <resultCount; i++) {

if (searchColor[i].match(regExColor) && searchFormat[i].match(regExFormat) && searchTipar[i].match(regExTipar) && Number(searchCantitate[i]) == Number(maxCantitate)) {
data.push(row[i]);
}
}

resolve(data);


the output is like this:


[
“serviciu: carti de vizita, cantitate: 2000, tipar: fataverso, format: 90x50cm, color: color, pret: 0.35, reducere: 0.28”
]


(including straight brachets).

how can i get the data parsed?

i tried the formula {{execute_code.result__pret}}, as all they say, but is null the result.

think is something because of the json format
i tried in the scrpt to change

resolve(data);

to

resolve(JSON.parse(data));

but it returns errror. “Script Error: Unexpected token s in JSON at position 0”

can you help me?

i think is because of the definition: “var data[];” but i dont know how to change it in a pure json output.
can you help me? pls.

(Karen Barker) #2

Hi there,

Could you please email us at support@flowxo.com including a screen shot of your interaction logs and also sharing your flow so we can try to recreate the problem. To share a flow please edit the flow, click the private button at the top of the flow, make the flow public and then share the public URL with me.

Thanks.

(Optim Birou) #3

Thank you very much for your answer.
It works.
Here it it the answer provided for anyone interested in future:
“var resultCount = {{search_rows.results_count}};
var row = inputs.row.split(”\n");
var i;
var searchTermColor = inputs.searchTermColor;
var searchTermTipar = inputs.searchTermTipar;
var searchTermCantitate = inputs.searchTermCantitate;
var searchTermFormat = inputs.searchTermFormat;
var regExColor = new RegExp(searchTermColor, “gi”);
var regExTipar = new RegExp(searchTermTipar, “gi”);
var regExFormat = new RegExp(searchTermFormat, “gi”);
var regExCantitate = new RegExp(searchTermCantitate, “gi”);
var searchColor = inputs.searchColumnColor.split("\n");
var searchFormat = inputs.searchColumnFormat.split("\n");
var searchTipar = inputs.searchColumnTipar.split("\n");
var searchCantitate = inputs.searchColumnCantitate.split("\n");
var maxCantitate = 100000;
var data = [];

for(i = 0; i <resultCount; i++) {
if (Number(maxCantitate) >= Number(searchCantitate[i]) && Number(searchTermCantitate) <= Number(searchCantitate[i])) {
var maxCantitate = searchCantitate[i];

}
}

for(i = 0; i <resultCount; i++) {

if (searchColor[i].match(regExColor) && searchFormat[i].match(regExFormat) && searchTipar[i].match(regExTipar) && Number(searchCantitate[i]) == Number(maxCantitate)) {

var obj = new Object();
obj.color = searchColor[i];
obj.format = searchFormat[i];
obj.tipar = searchTipar[i];
obj.cantitate = searchCantitate[i];
var jsonString= JSON.stringify(obj);
data.push(obj);
}
}

resolve(data);"

the catch is to use the var obj.
the outout is somethng like this:
“Result
[
{
“color”: “albnegru”,
“format”: “A4”,
“cantitate”: “20”,
“pret”: “1”
}
]”
but WATCH OUT:
the correct data output isas collection:
“{{execute_code.result_+_pret}}”

dont use:
“{{execute_code.result__pret}}”!!!
it returns null.
thx again, flowxo team!

1 Like