I have made a rest request which is returning me a Set in JSON format which is "[\"TestBack\"]".
If I directly parse it in Apex using
Set<String> rw = (Set<String>)JSON.deserialize(response.getBody(),Set<String>.class);
then I get following error
FATAL_ERROR System.JSONException: Malformed JSON: Expected '[' at the beginning of List/Set
but if I explicitly remove double quote sign by using
Set<String> rw = (Set<String>)JSON.deserialize(response.getBody().substringAfter('"').substringBeforeLast('"'),Set<String>.class);
I got following error
FATAL_ERROR System.JSONException: Unexpected character ('\' (code 92)): expected a valid value
and if I try to use replaceAll method
Set<String> rw = (Set<String>)JSON.deserialize(response.getBody().substringAfter('"').substringBeforeLast('"').replaceAll('\\',''),Set<String>.class);
it shows following error
FATAL_ERROR System.StringException: Invalid regex: Unexpected internal error near index 1
Is there any way to get parse back into Set?
Related
I am trying to receive name and value (query details) from a URL but I have a restriction that the URL shouldn't be parsed to find out query details from the URL. I tried to find the index of '?' and then add the substring on a list till index of '='-1 from the query which I found using the statement:
String query = uri.getQuery() == null || uri.getQuery().trim().isEmpty() ? "" : uri.getQuery();
The issue that comes here is I am getting an error which states:
java.net.URISyntaxException: Illegal character in query at index
Can anyone point where am I going wrong ?
The URL which I am using is:
https://xyzz.com.in/collections/two-tone-wedding-rings?ugg_9ty554_tags=ROSE|WHITE&ugg_yuiolz_price=%3A700%201000%20%7B0%7D%20-%20%7B1%7D
(The | symbol is creating issues)
I think you're using the wrong class for this
I tried doing it with URL class instead
String input = "https://xyzz.com.in/collections/two-tone-wedding-rings?ugg_9ty554_tags=ROSE|WHITE&ugg_yuiolz_price=%3A700%201000%20%7B0%7D%20-%20%7B1%7D\n"
URL uri = new URL(input);
System.out.println(uri.getQuery());
Output:
ugg_9ty554_tags=ROSE|WHITE&ugg_yuiolz_price=%3A700%201000%20%7B0%7D%20-%20%7B1%7D
Hi I am having a Java String with following value received from HTTPRequest
{SubRefNumber:"3243 ",QBType:"-----",Question:"<p><img title="format.jpg" src="data:image/jpeg;base64,/9j/4AAQSkZJRgAB..."></img></p>"};
As the String contains HTML elements as part of it,while i try to parse the String as JsonObject as below (quesRow is the variable with above String as value)
JSONObject jsonObject = new JSONObject(quesRow);
I get parse error
org.codehaus.jettison.json.JSONException: Expected a ',' or '}' at character 103 of {SubRefNumber:"3243.....
I need to parse the HTML elements within Question Key as a seperate data from this JSONString. is there any way to handle this scenario? Please Guide...TIA
A valid JSON does not contain an unescaped quotation mark (") inside a string (See RFC 7159 Chapter 7 - https://www.rfc-editor.org/rfc/rfc7159#page-9).
There are different options to escape the quotation mark in your source string, already when putting it into the JSON string parameter:
escape with a backslash - "
escape as unicode sequence - \u0022
I am trying to use Rest Assured in the Serenity framework to validate an endpoint response. I send an xml body to the endpoint and expect a JSON response back like so:
{"Entry ID" : "654123"}
I want to send the XML and verify in the JSON response that the value of the key "Entry ID" is not empty or null. The problem is, the key has a space in it, and I believe it is causing an error. Here is what I have so far:
SerenityRest.given().contentType(ContentType.XML)
.body(xmlBody)
.when().accept(ContentType.JSON).post(endpoint)
.then().body("Entry ID", not(isEmptyOrNullString()))
.and().statusCode(200);
This produces the error:
java.lang.IllegalArgumentException: Invalid JSON expression:
Script1.groovy: 1: unable to resolve class Entry
# line 1, column 33.
Entry ID
^
1 error
I have tried wrapping the "Entry ID" term in different ways to no avail:
.body("'Entry ID'", not(isEmptyOrNullString()))
.body("''Entry ID''", not(isEmptyOrNullString()))
.body("\"Entry ID\"", not(isEmptyOrNullString()))
.body("['Entry ID']", not(isEmptyOrNullString()))
.body("$.['Entry ID']", not(isEmptyOrNullString()))
Is it possible to get the value of a key that contains a space in Rest Assured?
You just need to escape the key with single quotes:
then().body("'Entry ID'", not(isEmptyOrNullString()))
Here's an example (tested in version 3.0.6):
// Given
String json = "{\"Entry ID\" : \"654123\"}";
// When
JsonPath jsonPath = JsonPath.from(json);
// Then
assertThat(jsonPath.getString("'Entry ID'"), not(isEmptyOrNullString()));
I want to parse some text using Lucene query parser to carry out basic text preprocessing on the texts. I used following lines of code:
Analyzer analyzer = new EnglishAnalyzer();
QueryParser parser = new QueryParser("", analyzer);
String text = "...";
String ret = parser.parse(QueryParser.escape(text)).toString();
But, I am getting an error:
Exception in thread "main" org.apache.lucene.queryparser.classic.ParseException: Cannot parse '': Encountered "<EOF>" at line 1, column 0.
Using Query.escape() removes the special characters. However it doesn't remove
AND, NOT, OR
which are keywords used in lucene search.
There are two ways to deal with it :
Replace AND, NOT, OR in the query string.
Convert the query string to lower case.
Converting to lower case resolves the issue as only the capitalized AND, NOT, OR are keywords. They are treated as a regular word in lower case.
for those who face this problem, I realized that my parser throw exception for the word "NOT", even after escaped. I had to manually replace it by other word.
In JMETER I have HTTP query which returns JSON String as following:
{"url":"/some/path?Id=343\u0026"}
I'm trying to parse parameter Id from it with BeanShell sampler:
url = prev.getResponseDataAsString();
int start=url.indexOf('Id=');
int end = url.indexOf('u0026')-1;
newId=url.substring(start,end);
vars.put("newId", newId);
and get an error: Token Parsing Error: Lexical error at line 4, column 25. Encountered: "u" (117), after : "\'s"
Any ideas?
So it seems like backslash confuses the parser. Tried some Java String operations (replaceAll, URLEncoder.encode) - none of them seems to help.
JSON URL contained extra parameter: {"url":"/some/path?Id=343\u0026success=1"}, so following code worked:
url = prev.getResponseDataAsString();
int start=url.indexOf("Id=")+3;
int end = url.indexOf("success=1")-6; //note: "\u0026" is 6 characters
newId=url.substring(start,end);
vars.put("newId", newId);
\u0026 is actually one character, not 6. Change line 3 to
int end = url.indexOf("\u0026");
By the way, I am not sure about your usage of single quotes. I suspect BeanShell is converting from a character array to a String in order to make it work. Best make them double quotes, just in case.