I am using RestAssured for API automation and as a part of it I have to extract a value from the JSON reponse.
JSON reponse is given below
{
"resourceName": "SERVICE_STATUS",
"records": [
{
"CREATED_DTTM": "2019-08-12T05:58:12.940Z",
"STATUS": "SUCCESS",
"SERVICE_EXECUTION_ID": "e760bed8-2ebb-4563-9812-7c0e5ed565cf",
"OUTPUT_FILE(S)": [
"FileC_FileA.csv"
],
"SERVICE_ID": "EXCEL2CSV"
}
],
"status": "SUCCESS"
}
I have to extract the value "FileC_FileA.csv" from the response, but when i do it via Json Path it is failing as "()" has special meaning in json path.
JsonPath js1 = new JsonPath(statusrespstring);
String outputfile = js1.get("OUTPUT_FILE(S)"); -----> This is failing
I have tried using
String outputfile = js1.get("OUTPUT_FILE"+"("+"S"+")");
String outputfile = js1.get('OUTPUT_FILE(S)');
But getting error as
: The parameter "S" was used but not defined. Define parameters using the JsonPath.params(...) function
Someone please help, how to select a value whose key parameter has () in it.
I have found the solution.
ArrayList<String> outputfile = js1.get("records[0].'OUTPUT_FILE(S)'");
If the property has special character then surrounding with single quotes will work.
Reference : https://support.smartbear.com/alertsite/docs/monitors/api/endpoint/jsonpath.html
Related
I have a .json file in MarkLogic as :
{
"contextProductIdCDM": {
"variables": {
"source": "'Inm'",
"source_table_id": "'123'",
"Referdate": "normalize-space(string-join((J_MCCC, if(J_MCDC eq '') then '000' else J_MCDC),''))"
},
"$ref": "#/contexts/Closure"
},
"predcondition": "xyz=1"
}
I want to delete the predcondition and also want to delete some part of referdata also using xquery. Can anyone help how to achieve that?
If you wanted to remove the precondition field and modify the contextProductIdCDM.variables.Referdate value, you could address those JSON properties with XPath and use xdmp:node-delete() and xdmp:node-replace() functions:
xquery version "1.0-ml";
let $doc := fn:doc("/test.json")
return (
xdmp:node-replace($doc/contextProductIdCDM/variables/Referdate, text{ "000" }),
xdmp:node-delete($doc/predcondition)
)
How it could be done in JavaScript; convert to a plain old object, make the modifications, then replace the document with that updated JSON object:
declareUpdate();
const doc = cts.doc("/test.json");
let obj = doc.toObject(); // create mutable representation
obj.contextProductIdCDM.variables.Referdate = '000'; //change the Referdate property value
delete obj.predcondition; // delete precondition field
xdmp.nodeReplace(doc, obj); // update the JSON document with the changes
I'm new to Elastic Search in ASP.NET, and I have a problem which I'm, so far, unable to resolve.
From documentation, I've seen that & sign is not listed as a special character. Yet, when I submit my search ampersand sign is fully ignored. For example if I search for procter & gamble, & sign is fully ignored. That makes quite a lot of problems for me, because I have companies that have names like M&S. When & sign is ignored, I get basically everything that has M or S in it. If I try with exact search (M&S), I have the same problem.
My code is:
void Connect()
{
node = new Uri(ConfigurationManager.AppSettings["Url"]);
settings = new ConnectionSettings(node);
settings.DefaultIndex(ConfigurationManager.AppSettings["defaultIndex"]);
settings.ThrowExceptions(true);
client = new ElasticClient(settings);
}
private string escapeChars(string inStr) {
var temp = inStr;
temp = temp
.Replace(#"\", #"\\")
.Replace(#">",string.Empty)
.Replace(#"<",string.Empty)
.Replace(#"{",string.Empty)
.Replace(#"}",string.Empty)
.Replace(#"[",string.Empty)
.Replace(#"]",string.Empty)
.Replace(#"*",string.Empty)
.Replace(#"?",string.Empty)
.Replace(#":",string.Empty)
.Replace(#"/",string.Empty);
return temp;
}
And then inside one of my functions
Connect();
ISearchResponse<ElasticSearch_Result> search_result;
var QString = escapeChars(searchString);
search_result = client.Search<ElasticSearch_Result>(s => s
.From(0)
.Size(101)
.Query(q =>
q.QueryString(b =>
b.Query(QString)
//.Analyzer("whitespace")
.Fields(fs => fs.Field(f => f.CompanyName))
)
)
.Highlight(h => h
.Order("score")
.TagsSchema("styled")
.Fields(fs => fs
.Field(f => f.CompanyName)
)
)
);
I've tried including analyzers, but then I've found out that they change the way tokenizers split words. I haven't been able to implement changes to the tokenizer.
I would like to be able to have following scenario:
Search: M&S Company Foo Bar
Tokens: M&S Company Foo Bar + bonus is if it's possible to have M S tokens too
I'm using elastic search V5.0.
Any help is more than welcome. Including better documentation than the one found here: https://www.elastic.co/guide/en/elasticsearch/client/net-api/5.x/writing-queries.html.
By default for a text field the analyzer applied is standard analyzer. This analyzer applies standard tokenizer along with lowercase token filter. So when you are indexing some value against that field, the standard analyzer is applied on that value and the resultant tokens are indexed against the field.
Let's understand this by e.g. For the field companyName (text type) let us assume that the value being passed is M&S Company Foo Bar while indexing a document. The resultant tokens for this value after the application of standard analyzer will be:
m
s
company
foo
bar
What you can notice is that not just whitespace but also & is used as delimiter to split and generate the tokens.
When you query against this field and don't pass any analyzer in the search query, it by default apply the same analyzer for search as well which is applied for indexing against the field. Therefore, if you search for M&S it get tokenised to M and S and thus actual search query search for these two tokens instead of M&S.
To solve this, you need to change the analyzer for the field companyName. Instead of standard analyzer you can create a custom analyzer which use whitespace tokenizer and lowercase filter (to make search case insensitive). For this you need to change the setting and mapping as below:
{
"settings": {
"analysis": {
"analyzer": {
"whitespace_lowercase": {
"tokenizer": "whitespace",
"filter": [
"lowercase"
]
}
}
}
},
"mappings": {
"_doc": {
"properties": {
"companyName": {
"type": "text",
"analyzer": "whitespace_lowercase",
"fields": {
"keyword": {
"type": "keyword"
}
}
}
}
}
}
}
Now for the above input the tokens generated will be:
m&s
company
foo
bar
This will ensure that when searching for M&S, & is not ignored.
I'm trying to write unit tests for a rest endpoint with Spring Boot Test that's going well but when I try to assert on an object in the json response with jsonPath an AssertionError is thrown even when contents are identical and the same.
Sample Json
{
"status": 200,
"data": [
{
"id": 1,
"placed_by": 1,
"weight": 0.1,
"weight_metric": "KG",
"sent_on": null,
"delivered_on": null,
"status": "PLACED",
"from": "1 string, string, string, string",
"to": "1 string, string, string, string",
"current_location": "1 string, string, string, string"
}
]
}
Code in Kotlin
mockMvc.perform(
get("/api/v1/stuff")
.contentType(MediaType.APPLICATION_JSON_UTF8)
).andExpect(status().isOk)
.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))
.andExpect(jsonPath("\$.status").value(HttpStatus.OK.value()))
.andExpect(jsonPath("\$.data.[0]").value(equalTo(stuffDTO.asJsonString())))
That throws AssertionError but the values are the same
Clicking on see difference says
How can I match an object in JSON with jsonPath? I need to be able to match an object because the object can contain many fields and it will be a PITA to match them individually
I came across what looks like the same issue, though it's hard to say without knowing what your asJsonString function is. Also I was using Java, not Kotlin. If it is the same issue:
It's due to jsonPath(expression) not returning a string, so matching it with one doesn't work. You need to convert stuffDTO into the correct type for matching using JsonPath ie. in a function such as:
private <T> T asParsedJson(Object obj) throws JsonProcessingException {
String json = new ObjectMapper().writeValueAsString(obj);
return JsonPath.read(json, "$");
}
Then .andExpect(jsonPath("\$.data.[0]").value(equalTo(asParsedJson(stuffDTO)))) should work.
I am trying to write a pact consumer test to match the following response.
[
{
"accountId" : 1,
"permissions" : [
{
"schedule" : {
"01/01/2018" : false,
"01/01/1900" : true
},
"permissionId" : 3
}
]
}
]
Each schedule object is composed of an unknown number of keys which match a simple regular expression. But I don't see a way to match a key using a regular expression while having the value map to a simple boolean.
For instance, I see the following method in the API.
public LambdaDslObject eachKeyLike(
String exampleKey,
Consumer<LambdaDslObject> nestedObject)
But that is going to expect a new object as the value, instead of a primitive type.
"schedule" : {
"01/01/2018" : { ... }, // not what I want to match
"01/01/1900" : false // what I want to match
}
Is there a way to specify an imprecise key mapped to a primitive value in pact-jvm?
Sorry, this feature doesn't exist yet, but it's been discussed for the next version of the pact specification. You can add your thoughts on this issue: https://github.com/pact-foundation/pact-specification/issues/47
How do I iterate through the Json object that has an array within it and places the description object in a variable that can be used in another function?
This is the Json scheme that is being pulled in with $regResult:
{
"errors": [
{
"code": "401.07.001",
"description": "Invalid Access Token",
"link": "https://developer.arity.com/registration-services/apis"
}
]
}
You have multiple options
just use return $registration_result; and pass it to the other function as a parameter
Declare your variable as global global $registration_result = drupal_json_decode($regResult);
Save the value to the database