Json Path logical NOT operator with Filter not working - jsonpath

I have JSON data and want to use the logical not operator in JSONPath, however, I want to use it dynamically since my input string consists of many filters which I convert into a JSONPath query.
v1/movie?filter=not metadata.sample eq "NETFLIX" this way I am using my APY. This should be converted to [?(!(#['metadata']['PLM-CODE'] == 'NETFLIX'))] using the Filter object of JSONPath.
Now this below one works perfectly
JsonPath.using(configuration).parse(jsonString).read("$.data[?(!(#['metadata']['PLM-CODE'] == 'NETFLIX'))]",typeRef);
but I can not directly add ! when using the code below:
String stringFilter = rightPred.toString();
String notFilterString = "[?(!" + stringFilter.substring(2,stringFilter.length()-1) + ")]";
return Filter.parse(notFilterString);
but parse is converting it to [?(#['metadata']['PLM-CODE'] == 'NETFLIX' ! null)]

Related

r json mongodb query $in operator syntax error due to double quotes?

I'm building a json query to pass to a mongodb database in R.
In one scenario, I have a vector of dates and I want to query the database to return all records which have a date in the relevant field that matches a date in my vector of dates.
The second scenario is the same as the first, but this time I have a vector of character strings (IDs) and need to return all the records with matching IDs.
I understood the correct way to do this in a json query is to use the $in operator, and then put my vector in an array.
However, when I pass the query to my mongodb database, the exportLogId returns NULL. I'm quite sure that the problem is something to do with how I am representing the $in operator in the final query, since I have very similarly structured queries without the $in operator and they are all working. If I look for just one of my target dates or character strings, I get the desired result.
I followed the mongodb manual here to construct my query, and the only issue I can see is that the $in operator in the output of jsonlite::toJSON() is enclosed in double quotes; whereas I think it might need to be in single quotes (or no quotes at all, but I don't know how to write the syntax for that).
I'm creating my query in two steps:
Create the query as a series of nested lists
Convert the list object to json with jsonlite::toJSON()
Here is my code:
# Load libraries:
library(jsonlite)
# Create list of example dates to query in mongodb format:
sampledates <- c("2022-08-11T00:00:00.000Z",
"2022-08-15T00:00:00.000Z",
"2022-08-16T00:00:00.000Z",
"2022-08-17T00:00:00.000Z",
"2022-08-19T00:00:00.000Z")
# Create query as a list object:
query_list_l <- list(filter =
# Add where clause:
list(where =
# Filter results by list of sample dates:
list(dateSampleTaken = list('$in' = sampledates),
# Define format of column names and values:
useDbColumns = "true",
dontTranslateValues = "true",
jsonReplaceUndefinedWithNull = "true"),
# Define columns to return:
fields = c("id",
"updatedAt",
"person.visualId",
"labName",
"sampleIdentifier",
"dateSampleTaken",
"sequence.hasSequence")))
# Convert list object to JSON:
query_json = jsonlite::toJSON(x = query_list_l,
pretty = TRUE,
auto_unbox = TRUE)
The JSON query now looks like this:
> query_json
{
"filter": {
"where": {
"dateSampleTaken": {
"$in": ["2022-08-11T00:00:00.000Z", "2022-08-15T00:00:00.000Z", "2022-08-16T00:00:00.000Z", "2022-08-17T00:00:00.000Z", "2022-08-19T00:00:00.000Z"]
},
"useDbColumns": "true",
"dontTranslateValues": "true",
"jsonReplaceUndefinedWithNull": "true"
},
"fields": ["id", "updatedAt", "person.visualId", "labName", "sampleIdentifier", "dateSampleTaken", "sequence.hasSequence"]
}
}
As you can see, $in is now enclosed in double quotes, even though I put it in single quotes when I created the query as a list object. I have tried replacing with sprintf() but that just adds a lot of backslashes to my query. I also tried:
query_fixed <- gsub(pattern = "\\"\\$\\in\\"",
replacement = "\\'$in\\'",
x = query_json)
... but this fails with an error.
I would be very grateful to know if:
The syntax problem that is preventing $in from working is actually the double quotes?
If double quotes is the problem, how do I replace them with single quotes without messing up the JSON format?
UPDATE:
The issue seems to occur when R is passing the query to the database, but I still can't work out exactly why.
If I try the query out in loopback explorer in the database, it works and using the export log ID produced, I can then fetch the results with httr::GET() in R. Example query results are shown below (sorry for the hashes - the main point is you can see the format of the returned values):
[1] "[{\"_id\":\"e59953b6-a106-4b69-9e25-1c54eef5264a\",\"updatedAt\":\"2022-09-12T20:08:39.554Z\",\"dateSampleTaken\":\"2022-08-16T00:00:00.000Z\",\"labName\":\"LNG_REFERENCE_DATA_CATEGORY_LAB_NAME_LAB_A\",\"sampleIdentifier\":\"LS0044-SCV2-PCR\",\"sequence\":{\"hasSequence\":false},\"person\":{\"visualId\":\"C-2022-0002\"}},{\"_id\":\"af5cd9cc-4813-4194-b60b-7d130bae47bc\",\"updatedAt\":\"2022-09-12T20:11:07.467Z\",\"dateSampleTaken\":\"2022-08-17T00:00:00.000Z\",\"labName\":\"LNG_REFERENCE_DATA_CATEGORY_LAB_NAME_LAB_A\",\"sampleIdentifier\":\"LS0061-SCV2-PCR\",\"sequence\":{\"hasSequence\":false},\"person\":{\"visualId\":\"C-2022-0003\"}},{\"_id\":\"b5930079-8d57-43a8-85c0-c95f7e0338d9\",\"updatedAt\":\"2022-09-12T20:13:54.378Z\",\"dateSampleTaken\":\"2022-08-16T00:00:00.000Z\",\"labName\":\"LNG_REFERENCE_DATA_CATEGORY_LAB_NAME_LAB_A\",\"sampleIdentifier\":\"LS0043-SCV2-PCR\",\"sequence\":{\"hasSequence\":false},\"person\":{\"visualId\":\"C-2022-0004\"}}]"

Search for RestASSURED values by sending a variable

In the following code fragment I am trying to find in my responseBody a specific number by a variable and thus save its brother value.
However when I try to do it that way he answers me null and if I remove the quotes he answers me:
The parameter "n0" was used but not defined. Define parameters using the JsonPath.params(...) function
Response response = get(keyrest);
System.out.println("Value 0: " + response.asString());
semilla = response.path("seed");
String v1 = response.path("keys.find{it.number == 'n0'}.value");

Passing list of search string in contains in FilterExpression

Is there any way to pass a list of search strings in the contains() method of FilterExpression in DynamoDb?
Something like below:
search_str = ['value-1', 'value-2', 'value-3']
result = kb_table.scan(
FilterExpression="contains (title, :titleVal)",
ExpressionAttributeValues={ ":titleVal": search_str }
)
For now I can only think of looping through the list and scanning the table multiple times (as in below code), but I think it will be resource heavy.
for item in search_str:
result += kb_table.scan(
FilterExpression="contains (title, :titleVal)",
ExpressionAttributeValues={ ":titleVal": item }
)
Any suggestions.
For the above scenario, the CONTAINS should be used with OR condition. When you give array as input for CONTAINS, DynamoDB will check for the SET attribute ("SS", "NS", or "BS"). It doesn't looks for the sub-sequence on the string attribute.
If the target attribute of the comparison is of type String, then the
operator checks for a substring match. If the target attribute of the
comparison is of type Binary, then the operator looks for a
subsequence of the target that matches the input. If the target
attribute of the comparison is a set ("SS", "NS", or "BS"), then the
operator evaluates to true if it finds an exact match with any member
of the set.
Example:-
movies1 = "MyMovie"
movies2 = "Big New"
fe1 = Attr('title').contains(movies1)
fe2 = Attr('title').contains(movies2)
response = table.scan(
FilterExpression=fe1 or fe2
)
a little bit late but to allow people to find a solution i give here my method.
lets assume that in your DB you have a props called 'EMAIL you want to filter your scan on this EMAIL with a list of value. you can proceed as following.
list_of_elem=['mail1#mail.com','mail2#mail.com','mail3#mail.com']
#set an empty string to create your query
stringquery=""
# loop each element in your list
for index,value in enumerate(list_of_elem):
# add your query of contains with mail value
stringquery=stringquery+f"Attr('EMAIL').contains('{value }')"
# while your value is not the last element in list add the 'OR' operator
if index < len(list_of_elem)-1:
stringquery=stringquery+ ' | '
dynamodb = boto3.resource('dynamodb')
# Use eval of your query string to parse the string as filter expression
tableUser = dynamodb.Table('mytable')
tableUser.scan(
FilterExpression=eval(stringquery)
)

Asp.net mvc razor view string.format does not seems to work

I am using string.format to format my model value inside razor view but it does not gives desired result
#string.Format("{0:00}", Model.Range == null ? "" : Model.Range.ToString())
It should result as 05
if i am using below it gives me result but not from model
#string.Format("{0:00}", 5)
Someone have any idea or same experience ?
If Model.Range is a number type then you need to write:
#string.Format("{0:00}", Model.Range == null ? "" : Model.Range)
because with the Model.Range.ToString() you have converted your Range to string so the number formatting cannot be applied because it is not a number anymore.
By the way string.Format handles null arguments so it is enough to write:
#string.Format("{0:00}", Model.Range)
If Model.Range is not a number but with Model.Range.ToString() you get a number in a string representation then you need to first convert it to a number (like using int.Parse or its other variants) then you can pass the number to string.Format which can now apply the correct formatting.

Simple.Data casting to ToList<string>()

I am using Simple.Data and want to know if I can Select a single column and then cast it to a list of string values. For example using the query below I get the error:
Cannot implicitly convert type 'Simple.Data.SimpleRecord' to 'string'
var result = _database.ParentRegionList.All()
.Select(_database.ParentRegionList.RegionName)
.Where(_database.ParentRegionList.RegionName.Like(startsWith + "%"))
.Distinct()
.ToList<string>();
However if I create a class LocationAutoComplete that has a single public property "RegionName" of type string then the cast works fine.
var result = _database.ParentRegionList.All()
.Select(_database.ParentRegionList.RegionName)
.Where(_database.ParentRegionList.RegionName.Like(startsWith + "%"))
.Distinct()
.ToList<LocationAutoComplete>();
The ToList<T> Simple.Data method expects you to be casting the contents of a SimpleRecord to an object, which is why it works with your LocationAutoComplete class. Full details can be found here.
If you are returning only one field which you wish to return as a scalar value or a list of scalar values, use the ToScalar<T> or ToScalarList<T> method instead. Full details can be found here
If RegionName is a varchar then all you need is
var result = _database.ParentRegionList.All()
.Where(_database.ParentRegionList.RegionName.Like(startsWith + "%"))
.Select(_database.ParentRegionList.RegionName)
.Distinct()
.ToList();
You don't need the <string>

Resources