How to Extract Json value from an array based on some value in sub array - jmeter-5.0

I want to get the "id":1922 from the main array based on the "id":1 from the sub array 'media_type':
{
"data":[
{
"id":1922,
"media_count":1,
"title":"test",
"description":"Test",
"address":null,
"latitude":null,
"longitude":null,
"privacy":1,
"license":1,
"is_comment_disable":0,
"is_adult":0,
"media_type":{
"id":1,
"slug":"photo",
"title":"Photo"
}
}
]
}

Instead of converting json to object, you can use json parser
For reference: https://www.programcreek.com/java-api-examples/index.php?api=javax.json.stream.JsonParser

Try with JSON path to get value base on media_type
$..data[?(#.media_type>0)].id

Related

JSONata transformation script for taking source as it is with some override/modifications

I have JSON, for example:
{
"PDMSReferenceNumber": "11340",
"OntologyClass": "rdl:P101003917",
"TopTag": "DEEP1",
"ServiceDescription2": "Main manual",
"SystemVoltagePrimaryWinding": "",
"ClearOpeningHeight": "true"
}
Is it possible to create JSONata script like this:
{
"*": *,
"MainTag": TopTag
}
The result should be
{
"PDMSReferenceNumber": "11340",
"OntologyClass": "rdl:P101003917",
"ServiceDescription2": "Main manual",
"SystemVoltagePrimaryWinding": "",
"ClearOpeningHeight": "true",
"MainTag": "DEEP1"
}
So I want to take the source JSON is it is and make some override and modifications.
Thank you!
You could try using the Transform Function - https://docs.jsonata.org/other-operators#-------transform
So this basic "copys" TopTag to MainTag, and adds to object and then deletes TopTag
$ ~> |$|{'MainTag': TopTag}, ['TopTag']|
Here you go to show it:
https://try.jsonata.org/YqpO6oUk9
Exactly what JSONata does for you "transform" JSON.
You can simply do this:
$.{
"MainTag": TopTag,
"PDMSReferenceNumber": PDMSReferenceNumber,
"OntologyClass": OntologyClass,
"ServiceDescription2": ServiceDescription2,
"SystemVoltagePrimaryWinding": SystemVoltagePrimaryWinding,
"ClearOpeningHeight": ClearOpeningHeight
}
So the left part is the "key" for your new object, and the right is the "Key" from your source JSON (hence we have "MainTag": TopTag)

Mocha and Chai: JSON contains/includes certain text

Using Mocha and Chai, I am trying to check whether JSON array contains a specific text. I tried multiple things suggested on this site but none worked.
await validatePropertyIncludes(JSON.parse(response.body), 'scriptPrivacy');
async validatePropertyIncludes(item, propertyValue) {
expect(item).to.contain(propertyValue);
}
Error that I getting:
AssertionError: expected [ Array(9) ] to include 'scriptPrivacy'
My response from API:
[
{
"scriptPrivacy": {
"settings": "settings=\"foobar\";",
"id": "foobar-notice-script",
"src": "https://foobar.com/foobar-privacy-notice-scripts.js",
}
You can check if the field is undefined.
If field exists in the JSON object, then won't be undefined, otherwise yes.
Using filter() expresion you can get how many documents don't get undefined.
var filter = object.filter(item => item.scriptPrivacy != undefined).length
If attribute exists into JSON file, then, variable filter should be > 0.
var filter = object.filter(item => item.scriptPrivacy != undefined).length
//Comparsion you want: equal(1) , above(0) ...
expect(filter).to.equal(1)
Edit:
To use this method from a method where you pass attribute name by parameter you can use item[propertyName] because properties into objects in node can be accessed as an array.
So the code could be:
//Call function
validatePropertyIncludes(object, 'scriptPrivacy')
function validatePropertyIncludes(object, propertyValue){
var filter = object.filter(item => item[propertyValue] != undefined).length
//Comparsion you want: equal(1) , above(0) ...
expect(filter).to.equal(1)
}

What kind of Java type to pass into Criteria.all()?

I am trying to find a document with an array of tags which match a list of values,
using the MongoDB's $all function through Spring Data MongoDB API for all().
Tag is a embedded document with two fields: type and value.
I am not sure what kind of Java type to pass in to the method as it accepts an array of Objects, tried to pass in an array of Criteria objects into the the function but the output is below:
Query: { "tags" : { "$all" : [ { "$java" : org.springframework.data.mongodb.core.query.Criteria#5542c4fe }, { "$java" : org.springframework.data.mongodb.core.query.Criteria#5542c4fe } ] } }, Fields: { }, Sort: { }
How should I proceed?
I want to achieve the following:
db.template.find( { tags: { $all: [ {type:"tagOne", value: "valueOne"}, {type:"tagTwo", value: "valueTwo"} ] } } )
Edited for clarity:
The code which I used is similar to:
Query query = new Query(baseCriteria.and("tags").all( criteriaList.toArray()))
The criteria list is formed by:
Criteria criteria = new Criteria();
criteria.and("type").is(tag.getType()).and("value").is(tag.getValue());
criteriaList.add(criteria);
OK, I've just found out, the Java type required is org.bson.Document, which you can get using:
criteria.getCriteriaObject()

Graphql object ( or JSON) as filter argument

Is it possible to have a JSON object as filed in filter arguments. Something like:
Query{
building(location:{lon,lat}){
name,...
}
}
I need to pass location, and I would like to pass it as js object ( to apollo client) or as stringified JSON.
You can use input types to achieve that. You need to edit your schema
type Query {
building(location: Location): Building
}
input Location {
lon: String
lat: String
}
Then you can post your query like this
query {
building(location: {lon:"100.332680",lat:"5.416393"}) {
name,...
}
}

How to pass GraphQLEnumType in mutation as a string value

I have following GraphQLEnumType
const PackagingUnitType = new GraphQLEnumType({
name: 'PackagingUnit',
description: '',
values: {
Carton: { value: 'Carton' },
Stack: { value: 'Stack' },
},
});
On a mutation query if i pass PackagingUnit value as Carton (without quotes) it works. But If i pass as string 'Carton' it throws following error
In field "packagingUnit": Expected type "PackagingUnit", found "Carton"
Is there a way to pass the enum as a string from client side?
EDIT:
I have a form in my front end, where i collect the PackagingUnit type from user along with other fields. PackagingUnit type is represented as a string in front end (not the graphQL Enum type), Since i am not using Apollo Client or Relay, i had to construct the graphQL query string by myself.
Right now i am collecting the form data as JSON and then do JSON.stringify() and then remove the double Quotes on properties to get the final graphQL compatible query.
eg. my form has two fields packagingUnitType (An GraphQLEnumType) and noOfUnits (An GraphQLFloat)
my json structure is
{
packagingUnitType: "Carton",
noOfUnits: 10
}
convert this to string using JSON.stringify()
'{"packagingUnitType":"Carton","noOfUnits":10}'
And then remove the doubleQuotes on properties
{packagingUnitType:"Carton",noOfUnits:10}
Now this can be passed to the graphQL server like
newStackMutation(input: {packagingUnitType:"Carton", noOfUnits:10}) {
...
}
This works only if the enum value does not have any quotes. like below
newStackMutation(input: {packagingUnitType:Carton, noOfUnits:10}) {
...
}
Thanks
GraphQL queries can accept variables. This will be easier for you, as you will not have to do some tricky string-concatenation.
I suppose you use GraphQLHttp - or similar. To send your variables along the query, send a JSON body with a query key and a variables key:
// JSON body
{
"query": "query MyQuery { ... }",
"variables": {
"variable1": ...,
}
}
The query syntax is:
query MyMutation($input: NewStackMutationInput) {
newStackMutation(input: $input) {
...
}
}
And then, you can pass your variable as:
{
"input": {
"packagingUnitType": "Carton",
"noOfUnits": 10
}
}
GraphQL will understand packagingUnitType is an Enum type and will do the conversion for you.

Resources