ECMA Sample script to search string and copy value for KVP.

Hi,

Need help to build ECMA script for searching the string and picking up the value against Key.

Example - abc:123, xyz:345,acd:4578,acq:785.

Looking for a script which can search the above string and store the output of the key “xyz” into a variable. Any help would be appreciated.

Thanks

Hi,

You can convert your string into KVP list format and using built-in irdGetStringKey function. This is based on assumption that format of your string is :,:


var inputStr = "abc:123, xyz:345,acd:4578,acq:785";

var kvpList = inputStr.replace(/,/g,"|");

var valueOfXyz = irdGetStringKey("xyz",kvpList);

R.

Thank you. I have tried amended the script to below.

var bn_key = AppState.bn_key_script
var kvpList = bn_key.replace(/,/g,“|”);
AppState.Test_Name = irdGetStringKey(“b.agentNme”,kvpList);

In logs, I see below error.

2020-02-26 09:42:05.425 DBUG 008301E5-1000AD1B 5480 0C000000 SemanticError.cxx:22 SemanticError() Created error.semantic from element: [Script] at line number: [975] with message: [TypeError: bn_key.replace is not a function. Line 1]
2020-02-26 09:42:05.425 DBUG 008301E5-1000AD1B 5480 0C000000 ScriptError.cxx:9 Created error.semantic from element [Script] at line number [975] from script [var bn_key = AppState.bn_key_script
var kvpList = bn_key.replace(/,/g,“|”);
AppState.Broker_Name = irdGetStringKey(“b.agentNme”,kvpList);]

P.S. bn_key_script:b.statusFlg:HS,b.statusMsg:Host Successful,b.searchKey:Test,b.searchValue:1234,b.agentNme:Test1

Any Suggestions?

Issue is caused by the fact that bn_key is NOT a string.

Please check type of variable using typeof bn_key

You can try converting the variable to string using one of suggested options below but I cannot guarantee these will work as I don’t know object type.


// Option #1
var kvpList = (''+bn_key).replace(/,/g,"|");

// Option #2
var kvpList = String(bn_key).replace(/,/g,"|");

R.