Author Topic: ECMA Sample script to search string and copy value for KVP.  (Read 263 times)

Offline kamalnba

  • Newbie
  • *
  • Posts: 34
  • Karma: 0
ECMA Sample script to search string and copy value for KVP.
« on: February 26, 2020, 06:53:41 AM »
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

Offline René

  • Administrator
  • Hero Member
  • *****
  • Posts: 1803
  • Karma: 59
Re: ECMA Sample script to search string and copy value for KVP.
« Reply #1 on: February 26, 2020, 06:31:23 PM »
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 <key>:<value>,<key>:<value>

Code: [Select]
var inputStr = "abc:123, xyz:345,acd:4578,acq:785";

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

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

R.

Offline kamalnba

  • Newbie
  • *
  • Posts: 34
  • Karma: 0
Re: ECMA Sample script to search string and copy value for KVP.
« Reply #2 on: February 27, 2020, 02:51:23 AM »
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 <key>:<value>,<key>:<value>

Code: [Select]
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?


Offline René

  • Administrator
  • Hero Member
  • *****
  • Posts: 1803
  • Karma: 59
Re: ECMA Sample script to search string and copy value for KVP.
« Reply #3 on: February 27, 2020, 05:00:41 AM »
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.

Code: [Select]
// Option #1
var kvpList = (''+bn_key).replace(/,/g,"|");

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

R.