Given
I have defined array
* def array = [
{"code": "codeA", "boolValue": false, "a": 5, "c": false},
{"code": "codeA", "boolValue": true, "a": 7, "c": true},
{"code": "codeB", "boolValue": true, "a": 1, "c": false}
]
And variable
* def expected = { "code": "codeB", "boolValue": true }
Issues
In the last element of array there is expected value.
But it contains some additional values and that's why it failed all the time I try to check that.
How to check if array contains expected?
This will work in 0.9.6.RC4:
* match array contains deep expected
In prior versions:
* match array contains '#(^expected)'
Please read the docs: https://github.com/intuit/karate/tree/develop#schema-validation
Related
I have a POST request in which the data comes as JSON. For testing via collection runner, I want to pick those from JSON File but I am unable to define variables in Array and am stuck need support. Input data is like:
Input Data:
{
"field1": "1",
"field2": "111111111111111",
"field3": "value3",
"field4": [
[],
[],
[]
],
"master_field": {
"field5": 11,
"field6": 33.0,
"field7": [5, 184]
},
"field8": [
[10, 6, -1030],
[-83, 0, -999],
[-54, 21, -1054],
[-162, 21, -990]
],
"field9": 92
}
I tried making a request in POSTMAN Body like JSON, it worked till field3 only:
{
"field1": "{{field1}}",
"field2": "{{field2}}",
"field3": "{{field3}}",
"field4":
[
"{{field4}}"
]
}
It Doesn't parse field4 onward. Thanks
In order to store arrays in Postman variables, you have to stringify them.
Say your field4 value is an array in your test script, just JSON.stringify() it and save it in the environment variable.
Then you can use the variable directly in your request body.
{
"field1": "{{field1}}",
"field2": "{{field2}}",
"field3": "{{field3}}",
"field4": {{field4}}
}
Note that the field4 variable is NOT inside quotes.
there
I have a JSON response as below:
"log": [{
"a": 0.40,
"b": "ED",
}, {
"c": 82,
"d": "ABC",
}, {
"e": 36,
"f": 23,
}, {
"g": 12,
"h": 40,
}
]
I need to count a number of lists in a 'log' object to be able to address the last one and find a specific element in it. The response is dynamic and has different amount of lists in it (in this case 4)
I tried log[-1], and examples form js-arrays.feature as in the link below:
https://github.com/intuit/karate/blob/master/karate-junit4/src/test/java/com/intuit/karate/junit4/demos/js-arrays.feature#L83
It is easy to find a number of elements in a list, but I frequently have variable amount of lists and I cant make it work
Many Thanks,
Take into account that log[-1] would return you undefined due to there is no index -1 in the array. For get the number of elements in your array you must do
log.length
Also about the link you've posted
log[log.length-1]; //This will return the last element of the array in this case { "g": 12, "h": 40, }
I have a response from an API like this one:
[{
"id": 1,
"name": "Microsoft",
"status": true,
"consoles": [{
"id": 4,
"name": "Xbox",
"status": true,
"subconsoles": [{
"id": 7,
"name": "Xbox 360",
"status": true,
"subconsoles": []
},
{
"id": 90,
"name": "Xbox One",
"status": false,
"subconsoles": [{
"id": 21,
"name": "Xbox One S",
"status": true,
"subconsoles": []
},
{
"id": 12,
"name": "Xbox One X",
"status": false,
"subconsoles": [{
"id": 41,
"name": "Xbox One X model 1",
"status": false,
"subconsoles": []
}]
}]
}]
}]
}]
Here is also the data in nice format:
What I am trying to achieve is to modify the status of the subconsoles.
So from layout section I pass the ID of what I want to change, but I am really stuck on how to access the subelement (and eventually the sub-subelement) in the reducers of Redux:
case SUBCONSOLES_ENABLE:
return {
...state,
id: action.payload.subconsoleId,
.....
}
You would need to create a copy of each of the arrays you have in your state before you can change them, to keep your state immutable. With so many levels, that would get complicated fast!
case SUBCONSOLES_ENABLE:
let newState = [...state]; //create copy of state array
..... //find the index of the object in the newState array which you would want to change
let newConsoles = [...newState[index].consoles]; //create a copy of consoles
..... //find the index of the object in the newConsoles array which you would want to change
let newSubConsoles = [...newConsoles[index2].subconsoles];//create a copy of subconsoles
let subConsole = newSubConsoles.find((a)=>a.id===action.payload.subconsoleId); //find the object in the newConsoles array which you would want to change
let newSubConsole = {...subConsole} //make a copy of your object
..... //make your changes to the copy
//replace the old object with the new object in newSubConsoles
//replace subConsoles array with newSubConsoles array in newConsoles
// replace consoles array with newConsoles array in new State.
//and finally!!
return newState;
Based on the shape of your state, I would suggest looking at normalizing your state and using Immutable.js
Check out the update fn from the 'immutability-helper' library, its recommended in the react docs themselves.
Hi currently I want to sort array of object, I use ARRAY_SORT function, it will use the first field of object to sort & it work well if every object has the same JSON structure. If one element in array has different JSON structure, the result is incorrect.
The query I use :
SELECT ARRAY_SORT(c.student) as student FROM Class c
Result :
"student": [
{
"id": 3,
"name": "Kenny35"
},
{
"id": 6,
"name": "Kenny35"
},
{
"id": 7,
"name": "Kenny35"
},
{
"id": 8,
"name": "Kenny35"
},
{
"hobby": "video game",
"id": 5,
"name": "Kenny35"
}
]
How can I specify property of object in array for ARRAY_SORT function ?
dev,
Objects are first compared by length/size of the object, then followed by fields in the object.
http://developer.couchbase.com/documentation/server/4.5/n1ql/n1ql-language-reference/comparisonops.html
That is the only collation supported now.
-Prasad
You can issue a query and use ORDER BY.
SELECT *
FROM Class c
UNNEST c.student s
ORDER BY ...
I need to be able to quickly discern whether or not a JSON data structure contains an array in it. For example the regex should return true for
{
"array": [
1,
2,
3
],
"boolean": true,
"null": null,
"number": 123,
"object": {
"a": "b",
"c": "d",
"e": "f"
},
"string": "Hello World"
}
and false for
{
"boolean": true,
"null": null,
"number": 123,
"object": {
"a": "b",
"c": "d",
"e": "f"
},
"string": "[Hello World]"
}
Any ideas?
I Would MUCH prefer if this check could be done via regex instead of traversing json, unless anyone can show me a better way.
You could format the JSON on some "canonical" from using a tool like jshon.
I do not recommend this approach.
$ cat foo.json
{"root": { "foo": 1, "bar": [2,3,4], "baz":"BAZ", "qux":[5,6, [7,8, [9, 10]]]}}
$ jshon < foo.json
{
"root": {
"foo": 1,
"bar": [
2,
3,
4
],
"baz": "BAZ",
"qux": [
5,
6,
[
7,
8,
[
9,
10
]
]
]
}
}
$ jshon < foo.json | grep '^\s*\]'
],
]
]
]
$ echo $?
0
Use this regex:
/:?\s+\[/g
And then you can simply do:
var json = "json here";
var containsArray = json.match(/:?\s+\[/g) !== null; // returns true or false
Demo: http://jsfiddle.net/pMMgb/1
Try
var a = {} // required json.
var regex = /\[(.*)\]/
regex.test(a)
After reviewing the answers, I've reconsidered, and I will go with parsing and traversing to find the answer to my question.
Regexing turned out to be unreliable, and the other options just add more convolution.
Thanks to everyone for the answers!