Is it possible to single out a json object and set it as another object only using JSTL? - jstl

I'm new to Jstl. Hope this question is not a duplicate.
Say, if there's a json object like below,
jsonObj = {"name":"John", "age":30, "car":null}
what I would like to do is take "John" from name and set it as value for userName.
So what I tried is <c:set var="userName" value="${jsonObj}.name" scope="page" />.
If I print userName with command <script>console.log(${userName})</script>, I can see John on the console panel.
However, if I look through the Sources in developer tools, I find that whole jsonObj value showing on every objects that I created from jsonObj.
Am I asking for something that's not available?
If not, any advice would be grateful no matter how small they are.
Thank you in advance.

<c:set var="userName" value="${jsonObj}.name" /> is equivalent to something like:
userName = jsonObj.toString() + ".name";
Assuming jsonObj is an org.json.JsonObject, you can access the content of your Json with:
<c:set var="userName" value="${jsonObj.getString('name')}"/>

Related

How to edit the data in CK editor that comes from API?

I post the data in API and want to edit this data after getting from API. when I try to edit the data it gives me the following error:
CKEditorError: datacontroller-set-non-existent-root: Attempting to set data on a non-existing root. Read more: https://ckeditor.com/docs/ckeditor5/latest/framework/guides/support/error-codes.html#error-datacontroller-set-non-existent-root
<CKEditor
editor={ClassicEditor}
onChange={this.handleChange}
data={html}
></CKEditor>
Here is the dummy solution for now, I don't know what is the proper reason of that:
Create one variable:
let a = "";
Replace the content of the variable a with the your content coming from the API
and parse it with htmlparser:
let data = a.replace("", htmlparser(/*data coming from your api*/))

how to pull data using xpath from script?

Just started learning scraping and for my test project i'm trying to retreive the quantity of a certain project in scrapy shell by using
response.xpath('//script[contains("quantity")]/text()').extract()
This doesn't work.
help me understand what should be the right covention to retreive data from such attributes like quantity, category_path & etc
<script>
window.dataLayer = window.dataLayer || [];
dataLayer.push({"event":"datalayer-initialized","region":"India","account_type":"ecom","customer":{"id":""},"page_type":"Product","product":{"ffr":"csddfas","name":"tote bag by singh","materials":"100% polyester","specs":"Dimensions: 18.5\" x 6.75\"; 24L","color":null,"size":null,"upc":null,"new":false,"brand":null,"season":"HOLIDAY 2017","on_sale":false,"quantity":"158","original_price":100,"price":100,"category_path":
["Mens","Accessories","Backpacks \/ Bags"],"created":"2016-09-07","modified":"2018-02-12",
"colors":["BLACK"],"sizes":["S","M","L","XS","XL","XXL"]}});
</script>
Scrapy selectors have built-in support for regular expressions and it can help you on this case:
response.xpath('//script[contains(text(),"quantity")]/text()').re(r'"quantity":"(\d+)"')
(you need to update your xpath to collect script content since your script not good enough)
Another Way: You also can use regular expressions to collect the json content on script, parse them to json obj and work with it as easier!
You are using css method and giving it an Xpath
Try
response.xpath('//script[contains(text(),'quantity')]').extract()
or
response.css('script::contains(quantity)').extract()
And you will need a Regex to extract that JSON string
re.findall(r'(?<=dataLayer\.push\().*(?=\)\;)', your_script_tag_data, re.DOTALL)
javascript = response.xpath('//script[contains("quantity")]/text()').extract_first()
json_string = re.search( r'dataLayer\.push\((.+?)\);', javascript, re.DOTALL ).group(1)
data = json.loads(json_string)
print( "Quantity: {0}".format(data["product"]["quantity"]) )
In my experience, have no way to get quantity, category_path & etc by only Xpath, because they're in Json format. Xpath can get information in XML data.
I assume that you've already have xml data, use
python
data = yourXML.xpath('//script//text()')
Now data is a string that contain all information. Then, you need to get string in dataLayer.push and convert it to Json format. With Json, it' easy to get your information.

How to make jmeter while controller work

I'm having trouble getting the while controller to work in jmeter.
I've a feeling that I read that it doesn't re-evalute user defined variables, so I am trying to use properties instead.
I start off by using a BSF assertion to set a property called keepLooping
${__setProperty(keepLooping, true)};
This seems to work as it enters the While controller with a condition of
${__property(keepLooping)}
But I cannot for the life of me get it to change that property to something else. I want it to change the property depending on the resulting text of an http request.
So I am using a Regular Expression Extractor to set a variable, which I can see is getting set. Then I am trying to use a BSF assertion to set the keepLooping property on the basis of the variable that I have set. I am using javascript as follows:
log.info("IM IN HERE");
log.info("props is "+props);
//log.info("props keep looping is "+props["keepLooping"]);
if (${surveyRequired} == false){
log.info("IM IN HERE 1A and props is "+props);
${__setProperty(keepLooping, true)};
log.info("IM IN HERE 1B");
}
else {
log.info("IM IN HERE 2A");
${__setProperty(keepLooping, false)};
log.info("IM IN HERE 2B");
}
I can't figure out how to set the property with javascript - I've tried several things. Can anyone help? Many thanks!
Also can anyone recommend a good resource that negotiates what seem to be the many 'quirks' of jmeter? Many thanks!
"I've a feeling that I read that it doesn't re-evalute user defined variables" -- I use JMeter 2.9 and it really does. I use user defined variable in order to count number of loops. It looks like: ${__javaScript(${MY_USER_DEFINED_VARIABLE}>0)}. The only one annoying thing is that I have to get value of variable, increment it, cast to string (toString() in Groovy), and then put new value into MY_USER_DEFINED_VARIABLE (by using vars.putObject("MY_USER_DEFINED_VARIABLE",localBSFVariable))
Using vars.put or props.put will help, as explained in detailed in detail in this jmeter thread.

mvc3 dapper parameter issue

I have dapper working correctly, but it is unsecure as in I haven't been using parameters, how can I best turn my dapper variables into parameters for instance this is the unparameterized code that I had that worked..
var getinfo = sqlConnection.Query<test>("Select name,location from tests where location="+ myplace).FirstOrDefault();
myplace is a textbox that users put information on, now when I tried to parameterized that code like
var getinfo = sqlConnection.Query<test>("Select name,location from tests where location='#location'", new {location = myplace}).FirstOrDefault();
I get absolutely no returns back, yet no error messages. What can I be missing here or whats the best way to parameterized variables.
You do not need to place the single quotes around the parameter. Hope this helps.
var getinfo = sqlConnection.Query<test>("Select name,location from tests where location=#location", new {location = myplace}).FirstOrDefault();

JSTL String concatenation - How can we achieve this?

In my JSP.. I am now accessing a session scoped variable using ${sessionScope.var1}, $sessionScope.var2}, but i want this 1,2 .. so on, in the var1, to be appended from jstl like ${page} which gives me 0,1,2.. so on.. I tried ${sessionScope.var{page}}, also ${sessionScope['var${page}'] and other things.. but still i am not able to get it.. So, can someone please help me out in this isuue..
try this ;)
<c:set var="myVar" value="var${page}" />
${sessionScope[myVar]}

Resources