Can't pass extracted string into body object in JMeter [closed] - java

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 months ago.
Improve this question
I am trying to pass a string extracted from an API response in a subsequent call in JMeter. I am able to extract the object I want ("thing": "THING"), store it as variable $thisThing and then pass it, but not as a string.
Using this as my body data:
{
"foo": "bar",
"thing": ${thisThing}
}
...results in this request body:
{
"foo": "bar",
"thing": THING,
}
And the API errors out, unexpected token. Looking into post-processing solutions but I can't dig up anything of relevance.

As per JSON Object Literals:
Keys must be strings, and values must be a valid JSON data type:
string
number
object
array
boolean
null
so if this THING supposed to be a JSON String - you need to surround it with quotation marks:
{
"foo": "bar",
"thing": "${thisThing}"
}
or amend your Post-Processor configuration to extract the THING value along with surrounding quotation marks.
You might also need to add a HTTP Header Manager and configure it to send Content-Type header with the value of application/json

Related

Is there alternative to JSONObject in Java to read a huge payload, then change some values in it? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 days ago.
Improve this question
Rest payload is 2000 lines long. I need to preserve number as 2000.0. JSONObject changes to 2000. What can I use to read this payload instead of JSONObject?
Researched StackOverflow. Changing to String not a good option as there are several values which I don't know and I need to change them back to with decimal. For example, need them as 2000.0 with 0 after decimal. Request returns 400 without data like this.
Reading payload from a .json file. It could be anything else too. But I need to read payload from a file.
When I read this payload in JSONObject, it removes decimal zero after the number. This makes request fail.
When I run query in Postman with payload given by dev, it passes. When I run it with payload of my test, it fails. On debugging, we find the difference is decimal stripped.
{
"id": "106b25c9-fe0c-40d1-8dad-87e7b7550cfe",
"perOccurrenceDeductibleAmt": 2000.0,
"perOccurrenceDeductibleAmtStr": "2000"
}
String body = APIUtil.readText("TransportationAgreementUAT.json");
try {
json = new JSONObject(body); //find alternative that converts to string but does not remove trailing zero decimals
agreement = json.getJSONObject("agreement");
transaction = agreement.getJSONObject("transaction");
transaction.put("transactionSubEntCd", requestTransactionSubEntCd);
} catch (JSONException e) {
e.printStackTrace();
}
return json.toString();
}
I have asked a genuine question. Doing it -1 is not the answer.
The size tells that I cannot manually check where all '.0' is there. JSONObject does not support it. Asking for alternative to read a JSON payload, update a node in it then pass updated payload.

Java Jackson JSON without property name [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I have this JSON example
{ "name": "custom_text" }
My Object look like
public class NameObj {
private String name;
}
And with readValue() method from Jackson can deserialized my json into NameObj.
The real problem is when i don't have "name"
{ [ "custom_text" ] }
How create object in this case? And deserialization is same?
In the example { "names" : [ "custom_text" ] } it does not really
matter if you represent the [ "custom_text" ] as list or array but if
you choose the latter, it would rather be String[] instead of Array[].
Also mind the type erasure if you choose List String . Then you have
to use e.g. TypeReference helpers.
Answer by mle
List String work for me. Thank you mle

Parse JWT token payload data to get certain value in android/java [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 6 years ago.
Improve this question
I just got introduced to the JWT and wanted to know how to parse the payload data to get a certain value from it using the key.
Example in the following JWT token eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWV9.TJVA95OrM7E2cBab30RMHrHDcEfxjoYZgeFONFh7HgQ
The payload data contains the following object when decoded in jwt.io
{
"sub" : "1234567890"
"name" : "John Doe"
"admin" : "true"
}
I want to be able to parse this object to get the value of name which in this case is John Doe from the above JWT token.
I have already read this Android JWT parsing payload/claims when signed
But i wanted to know if there is efficient way to do this in android using some library. Any help would be appreciated.
You can use Java JWT for that:
String name = Jwts.parser().setSigningKey(keyUsedWhenSigningJwt).parseClaimsJws("base64EncodedJwtHere").getBody().get("name", String.class);
I solved it, by normally parsing the string and decoding it from base64 and then casting the string to JSONObject and parsing the JSONObject to get the required value.

NullpointerException while reading a json with null value for array [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
Is there a way to read this json without causing nullpointer exception through jackson.
{
"years": [
null
]
}
years is an array of String
Also is that a valid format of json when there are no years?
If you look closely at the tutorials for Jackson, you will see that the JSON this library produces itself for empty arrays uses this notation: { "empty" : [ ]}
Therefore you may try replacing any singular null value in an array with just an empty array before sending your JSON to Jackson, it should accept without throwing any exceptions.
Canonically, a ' null' member of an array is actually valid JSON syntax. See also http://en.wikipedia.org/wiki/JSON. Members of an array can be of any type, thus they can be:
Strings
Numbers
Booleans
Arrays
Objects
null
For your usage scenario however, I would recommend using the empty array instead, because it is simply far easier to program with. For example, consider a usage case where you call some function f() on each of your 'years' which wants an integer input. Code like
foreach(x in array){f(x);} will fail because you will call f() with a 'null' type instead of an integer, causing errors. If you instead used the empty array, the correct behaviour will happen without having to treat the case where there are no years diffrently by explicitly looking for the null. Just makes your life easier that way.

Java-Json Parsing issue-Escaping the XML characters [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I'm unable to escape the XML charecters in the Json response.
Here is the JSON response
{"projectId":14,"modificationDate":1379677731000,"projectJsonData":"{"playerConfig":{"autoPlay":true,"initVolume":"50","initFullScreen":false,"size":"container","splashPoster":"defailt.png","projectId":123456},"formList":[{"type":"form","subType":"form-image","position":"inscreen","transition":"comealive-fade","width":50,"zindex":998,"start":12.9125390745513,"end":16.90343280291607,"id":"complexForm1","layout":"formTemplate","imgSrc":"images/birds.jpg","title":"Gannets usually found in Australia..","desc":"The birds that you see in this video are Gannets usually found in Australia and New Zealand. Diving birds are birds which plunge into water to catch fish or other food. They may enter the water from flight, as does the brown pelican or they may dive from the surface of the water. More than likely they evolved from birds already adapted for swimming that were equipped with such adaptations as lobed or webbed feet for propulsion.","button":[{"title":"Gannets","action":"http://en.wikipedia.org/wiki/Gannet","type":"link"},{"title":"Aussie Gannet","action":"http://en.wikipedia.org/wiki/Australasian_Gannet","type":"link"},{"title":"Photography","action":"http://aloneatseaphotography.com.au/8950","type":"link"}],"displayType":"non-intrusive","action":"internal-seek"},{"type":"form","subType":"form-video","start":26.9125390745513,"end":35.90343280291607,"target":"video-container","position":"custom","transition":"comealive-fade","id":"complexForm2","layout":"formTemplate","videoSrc":[{"src":"videos/ocean.mp4","type":"video/mp4","codecs":"vp8,vorbis"},{"src":"videos/ocean.webm","type":"video/webm","codecs":"vp8,vorbis"}],"title":"Slow Motion","desc":"Gannets in super slow-mo","button":[],"displayType":"non-intrusive","tags":"Gannets","action":"internal-seek"},{"type":"form","subType":"form-custom","start":4,"end":10,"target":"video-container","position":"custom","id":"complexForm3","layout":"custom","title":"Gannets","content":"%3Cimg%20style%3D%22position%3Aabsolute%3Bright%3A10%25%3Btop%3A20px%22%20width%3D%22150%22%20height%3D%22150%22%20src%3D%22images%2Fbirds.jpg%22%20%2F%3E","displayType":"intrusive-parallel","desc":"Gannets","tags":"Gannets","action":"internal-seek","animation":"slideIn-600-easeOutBounce"}
I have given partial Json response due to size limitation here.
I have used the following to parse the response
JSONObject jsonProjectDataObject = new JSONObject(
StringEscapeUtils.escapeXml(videoGetResponse.get("projectJsonData").toString()));
Now I get the exception as
org.json.JSONException: Expected ':' after &quot at character 7 of {"playerConfig":{"autoPlay":true,"initVolume":"50","initFullScreen&quot
JSON is not XML. Using XML entities is not the appropriate way to escape JSON.
Double quote escaped using XML entities: "
Double quote escaped using JSON : \"
Paste your output here it says its invalid json. Before parsing json check this link

Categories