Need to generate an json array, then loop through the values - asp.net

I need to create some sort of a state for a bunch of elements on a page.
The stats can be 1 or -1.
Now on the server side I will generate a JSON array and put it in my .aspx page like this:
var someArray = { 100:-1, 1001:1, 102:1, 103:-1 }
How do I loop through each value now in javascript?
BTW, is my JSON array format correct?

Note that someArray is a misnomer as it is actually an Object. To loop through it, though:
for(key in someArray) {
alert(someArray[key]);
}
As far as whether it is valid, the above works for me but I believe technically keys should be strings:
{
"100": -1,
"1001": 1,
"102": 1,
"103": -1
}
Check out this handy JSON validator.

Related

How to extract a string value from an array using scripted field in kibana?

Is there a way to extract a string value from an array with the use of if statement in scripted field in kibana. I tried the below code, however, I am unable to filter out the correct and incorrect values in discover tab of kibana. This might be due to remark field is an array.
def result_string = "";
if (doc['nac.keyword'].value =="existing_intent" &&doc['remark.keyword'].value != "acceptable") {
result_string = "incorrect";
}
if (doc['nac.keyword'].value =="existing_intent" &&doc['remark.keyword'].value == "acceptable") {
result_string = "correct";
}
return result_string;`
You can use the contains method defined on Array to check for element membership:
!doc['remark.keyword'].value.contains("acceptable") //does not contain
For this, you might want to ensure first that doc['remark.keyword'].value is indeed an Array.

Loadrunner Parameters in JSON String

I'm trying to use a parameter inside of a JSON string, and would like to use an inner parameter to replace an GUID. I've changed the default parameter start and end characters since curly braces are used in JSON.
I've tried to do something like this, where the json param contains my json which is similar to this below.
{"DashboardGUID":"<Dash_GUID>"}
request_json = lr_eval_string("<json>");
lr_save_string(request_json, "request_json_param");
I'm expecting the lr_eval_string to replace the with the GUID that's in this parameter, what's the best why of replacing this ID in my JSON String?
Not sure what you are asking but I will put this here in case someone comes here in the future:
main.c
Action()
{
lr_eval_json("Buffer/File=my_json.json", "JsonObject=MJO",LAST);
lr_json_stringify("JsonObject=MJO","Format=compact", "OutputParam=newJsonBody",LAST);
lr_save_string(lr_eval_string(lr_eval_string("{newJsonBody}")),"tmp");
web_reg_find("Text={mydate}",LAST);
web_rest("POST",
"URL=http://myServer.microfocus.com/url",
"Method=POST",
"EncType=raw",
"Body={tmp}",
HEADERS,
"Name=Content-Type", "Value=application/json", ENDHEADER,
LAST);
return 0;
}
my_json.json
{
"LastActionId": 0,
"Updated": "{mydate}"
}
Okay so instead of doing what I'm thinking above I ended up creating an array of char's with this {"DashboardGUID":"<Dash_GUID>", someotherdata:"123"} in 10 different positions within the array. I then randomly selected an element from this array and when doing the lr_eval_string the parameter was replaced.
Hopefully this makes sense those looking to do something similar.

Lasso 9 Hangs on Inserting Pair with Map Value into Array?

EDIT: I accidentally misrepresented the problem when trying to pare-down the example code. A key part of my code is that I am attempting to sort the array after adding elements to it. The hang appears on sort, not insert. The following abstracted code will consistently hang:
<?=
local('a' = array)
#a->insert('test1' = map('a'='1'))
#a->insert('test2' = map('b'='2')) // comment-out to make work
#a->sort
#a
?>
I have a result set for which I want to insert a pair of values into an array for each unique key, as follows:
resultset(2) => {
records => {
if(!$logTypeClasses->contains(field('logTypeClass'))) => {
local(i) = pair(field('logTypeClass'), map('title' = field('logType'), 'class' = field('logTypeClass')))
log_critical(#i)
$logTypeClasses->insert(#i) // Lasso hangs on this line, will return if commented-out
}
}
}
Strangely, I cannot insert the #i local variable into thread variable without Lasso hanging. I never receive an error, and the page never returns. It just hangs indefinitely.
I do see the pairs logged correctly, which leads me to believe that the pair-generating syntax is correct.
I can make the code work as long as the value side of the pair is not a map with values. In other words, it works when the value side of the pair is a string, or even an empty map. As soon as I add key=value parameters to the map, it fails.
I must be missing something obvious. Any pointers? Thanks in advance for your time and consideration.
I can verify the bug with the basic code you sent with sorting. The question does arise how exactly one sorts pairs. I'm betting you want them sorted by the first element in the pair, but I could also see the claim that they should be sorted by last element in the pair (by values instead of by keys)
One thing that might work better is to keep it as a map of maps. If you need the sorted data for some reason, you could do map->keys->asArray->sort
Ex:
local(data) = map('test1' = map('a'=2,'b'=3))
#data->insert('test2' = map('c'=33, 'd'=42))
local(keys) = #data->keys->asArray
#keys->sort
#keys
Even better, if you're going to just iterate through a sorted set, you can just use a query expression:
local(data) = map('test1' = map('a'=2,'b'=3))
#data->insert('test2' = map('c'=33, 'd'=42))
with elm in #data->eachPair
let key = #elm->first
let value = #elm->second
order by #key
do { ... }
I doubt you problem is the pair with map construct per se.
This test code works as expected:
var(testcontainer = array)
inline(-database = 'mysql', -table = 'help_topic', -findall) => {
resultset(1) => {
records => {
if(!$testcontainer->contains(field('name'))) => {
local(i) = pair(field('name'), map('description' = field('description'), 'name' = field('name')))
$testcontainer->insert(#i)
}
}
}
}
$testcontainer
When Lasso hangs like that with no feedback and no immediate crash it is usually trapped in some kind of infinite loop. I'm speculating that it might have to do with Lasso using references whenever possible. Maybe some part of your code is using a reference that references itself. Or something.

How to access #1 inside conditional inside array->forEach

I trying to learn captures in Lasso 9, but I am struggling to figure out how to access the #1 local variable from within a conditional that's inside an array->forEach capture. Maybe my approach is all wrong. Is there a reference to the parent capture that I need to use? Following is the working code:
define paramstovars() => {
local(p = web_request->params)
#p->foreach => {
local(i = #1)
if(#i->type == 'pair') => {
var(#i->first->asstring = #i->second->asstring)
}
}
}
Following is the code I am trying to get working without relying on a redundant local variable definition:
define paramstovars() => {
local(p = web_request->params)
#p->foreach => {
if(#1->type == 'pair') => {
var(#1->first->asstring = #1->second->asstring)
}
}
}
In this second example, I receive an error that Position was out of range: 1 max is 0 (Error Code -1) on the line calling var().
Obvious security concerns with this custom method aside, what's the most efficient way to make #1 available inside nested conditionals?
#1 is replaced within each capture — so yes, you will need to assign it to another local in order to use it in deeper captures. If you need to work with the local again try using query expressions instead:
with i in web_request->params do {
if(#i->type == 'pair') => {
var(#i->first->asstring = #i->second->asstring)
}
}
Also, I wouldn't recommend settings variables in this fashion — it posses a security risk. It would be better to store the parameters in a single variable and then potentially set specific variables from that. There's a set of tags that does something similar here: getparam / postparam
It is my experience that #1 is consumed when called first time. At least I have never ben able to call it twice in the same capture.
If I need the value more than once I make it a local first. As you do in your example 1.
Some experiments later.
You can call #1 several times, contrary to what I wrote, but what trips your effort is that you have a capture inside the capture (the conditional).
The second capture will have it's own input params.
Here's a working, tested example to do what you want to do:
local(
myarray = array(1, 2 = 'two', 3 = 'four', 4),
mypairs = map
)
#myarray -> foreach => {
if(#1-> isa(::pair)) => {
#mypairs -> insert(#1 -> first -> asstring = #1 -> second -> asstring)
}(#1)
}
#my pairs
The result will be map(2 = two, 3 = four)
The trick is the sending of the foreach param to the conditional capture : `{some code}(#1)
Now, with all that worked out. I recommend that you take a look at Ke Carltons latest addition to tagswap. It will solve the same problem way better than creating a bunch of dynamic vars as you are attempting to do:
www.lassosoft.com/tagswap/detail/web_request_params

How can I tell if E4X expression has a match or not?

I am trying to access an XMLList item and convert it to am XML object.
I am using this expression:
masonicXML.item.(#style_number == styleNum)
For example if there is a match everything works fine but if there is not a match then I get an error when I try cast it as XML saying that it has to be well formed. So I need to make sure that the expression gets a match before I cast it as XML. I tried setting it to an XMLList variable and checking if it as a text() propertie like this:
var defaultItem:XMLList = DataModel.instance.masonicXML.item.(#style_number == styleNum);
if(defaultItem.text())
{
DataModel.instance.selectedItem = XML(defaultItem);
}
But it still give me an error if theres no match. It works fine if there is a match.
THANKS!
In my experience, the simplest way to check for results is to grab the 0th element of the list and see if it's null.
Here is your code sample with a few tweaks. Notice that I've changed the type of defaultItem from XMLList to XML, and I'm assigning it to the 0th element of the list.
var defaultItem:XML =
DataModel.instance.masonicXML.item.(#style_number == styleNum)[0];
if( defaultItem != null )
{
DataModel.instance.selectedItem = defaultItem;
}
OK I got it to work with this:
if(String(defaultItem.#style_number).length)
Matt's null check is a good solution. (Unless there is the possibility of having null items within an XMLList.. probably not, but I haven't verified this.)
You can also check for the length of the XMLList without casting it to a String:
if (defaultItem.#style_number.length() > 0)
The difference to String and Array is that with an XMLList, length() is a method instead of a property.

Resources