How to pass nested array in a postman automation test script - json

I'm creating test automation scripts through postman. I want to pass a nested array in postman body
{
"fruit":{{fruit}},
"Vehicles":[
{
"car":{{car}},
"bike":{{bike}}
}
]
}
I want to pass the vehicles array.
When I executed the APIs I get these vehicles as a empty variables. data is not passing
The Response body as follows
{
"fruit":"mango",
"Vehicles":[
{
"car":{{car}},
"bike":{{bike}}
}
]
}
The external json data file
[
{
"fruit":"mango",
"Vehicles":[
{
"car":"BMW",
"bike":"YAMAHA"
}
]
}
]
I'm executing this with postman collection runner and data inside the nested array is not passing.

Your request body is not valid JSON, after the values from the file have been inserted. You don't have quotation marks araound the values.
Try this:
{
"fruit":"{{fruit}}",
"Vehicles":[
{
"car":"{{car}}",
"bike":"{{bike}}"
}
]
}

Related

How to increment json object properties values to dynamically populate the json body of a jmeter request

Using JMeter I need to send a request which contains a body in such a format:
{
"key1":"value1",
"key2":{
"key21":[
{
"key211_1":"valueA",
"key212_1":"valueToIncrement",
"key213_1":{
"key2131_1":"valueB",
"key2132_1":"valueC",
},
"key214_1":{
"key2141_1":"valueD",
"key2142_1":"valueE"
}
},
{
"key211_2":"valueA",
"key212_2":"valueToIncrement+1",
"key213_2":{
"key2131_2":"valueB",
"key2132_2":"valueC",
},
"key214_2":{
"key2141_2":"valueD",
"key2142_2":"valueE"
}
}
]
}
}
Inside the body I can have a list of many json objects and each object will have some property with an unique id inside the request.
Something like:
{
"key212_1":"1"
},
{
"key212_2":"2"
}
...
{
"key212_2":"n"
}
Because I can have hundreds of objects inside a request body, can I create a logic in Jmeter, so that each time a request is sent, each value from the target property would have a value incremented by 1 in each of these properties without having to manually hardcode values?
You will have to maintain Counter. At the request where you want to increment the value, add a JSR223 Pre Processor. In that you will have to code to increment the value and save it back to the variable and also assign it your Json object.
EDIT :- I tried to simulate your setup as below.
Setup
RESULT

chef inspec validation of the nested json file

I am using chef inspec to validate in the below json file whether annotation is equal to Test.
"imdata": [
{
"aaaPwdStrengthProfile": {
"attributes": {
"annotation": "Test",
}
}
}
]
Trying with the below script but getting error
describe json('C:/output.json') do
its(['imdata','aaaPwdStrengthProfile','attributes','annotation']) { should eq 'Test' }
end
In the JSON file you are trying to test, imdata is an array []. The nested dictionary { .. } is the first element (0) of that array.
I created the JSON data from your question as /tmp/sample.json. So the inspec test should refer to 'imdata', 0,, like below:
describe json('/tmp/sample.json') do
its(['imdata', 0, 'aaaPwdStrengthProfile', 'attributes', 'annotation']) { should eq 'Test' }
end

How to manipulate json data in Jmeter

In a Jmeter Script, I need to process a http response e manipulate a json for send in next request, because actually this manipulation occurs in a Angular client.
My Http reponse:
[
{
"function":"nameA",
"rast":"F1",
"tag":"EE",
"probs":"0,987"
},
{
"function":"nameB",
"rast":"F2",
"tag":"SE",
"probs":"0,852"
},
{
"function":"nameC",
"rast":"F3",
"tag":"CE",
"probs":"0,754"
}
]
I need convert the result in json bellow to post in next request:
[
{
"function":"nameA",
"rast":"F1",
"type":{
"name":"EE"
},
"id":"alpha"
},
{
"function":"nameB",
"rast":"F2",
"type":{
"name":"SE"
},
"id":"alpha"
},
{
"function":"nameC",
"rast":"F3",
"type":{
"name":"CE"
},
"id":"alpha"
}
]
I filter the response with this JSON Extractor:
[*].["function", "rast", "tag"]
But now I need to solve other problems:
Add an id attribute (same for all functions)
Add an object with the name type.
Move the tag attribute into the object called type.
Rename the tag attribute to the name.
Add JSR223 PostProcessor as a child of the request which returns the original JSON
Put the following code into "Script" area:
def json = new groovy.json.JsonSlurper().parse(prev.getResponseData()).each { entry ->
entry << [type: [name: entry.get('tag')]]
entry.remove('tag')
entry.remove('probs')
entry.put('id', 'alpha')
}
def newJson = new groovy.json.JsonBuilder(json).toPrettyString()
log.info(newJson)
That's it, you should see the generated JSON in jmeter.log file.
If you need to have it in a JMeter Variable add the next line to the end of your script:
vars.put('newJson', newJson)
and you will be able to access generated value as ${newJson} where required
More information:
Groovy: Parsing and producing JSON
Apache Groovy - Why and How You Should Use It

Jmeter - How to validate portions of response json

I have the following json:
{
"content":
[
{
"id":1,
"userId":2,
"storeId":8,
"userFirstName":"Max",
"userLastName":"Mustermann",
"city":"Berlin",
"spendQuantity":100,
"paymentStatus":"UNPAID",
"userBalanceStatus":null,
"rateObject":
{
"identifier":23,
"id":"432",
"rate":"1.9345345",
"symbol":"USD",
"rank":2,
}
},
{
"id":2,
"userId":2,
"storeId":3,
"userFirstName":"Newman",
"userLastName":"Mustermann",
"city":"Berlin",
"spendQuantity":1000,
"paymentStatus":"UNPAID",
"userBalanceStatus":null,
"rateObject":
{
"identifier":3,
"id":"234",
"rate":"1.922222245345",
"symbol":"USD",
"rank":2,
}
},
{
"id":3,
"userId":2,
"storeId":3,
"userFirstName":"Newman",
"userLastName":"Mustermann",
"city":"Munich",
"spendQuantity":3000,
"paymentStatus":"UNPAID",
"userBalanceStatus":null,
"rateObject":
{
"identifier":2332,
"id":"234",
"rate":"3.234234",
"symbol":"USD",
"rank":2,
}
},
{
"id":4,
"userId":2,
"storeId":3,
"userFirstName":"Newman",
"userLastName":"Mustermann",
"city":"Essen",
"spendQuantity":4000,
"paymentStatus":"UNPAID",
"userBalanceStatus":null,
"rateObject":
{
"identifier":234,
"id":"234",
"rate":"333.234234",
"symbol":"USD",
"rank":2,
}
}
}
But I need to verify it partially - Only the fields in the nested jsons where city is Berlin or Essen, but without the rateObject (I don't need to verify this part). With other words I need to verify nested jsons with ids- 1,2,4 - all fields, without the information in rateObject.
Partial Answer and Suggestion:
We can apply the filter condition in the JSON Query to fetch the matched details.
For Example: To get the id of the mentioned city,
JSON Query:
$.content[?(#.city=="Berlin" || #.city=="Essen")].id
Output:
[
1,
2,
4
]
Similarly, you can assert all the required fields using the filter JSON Query.
JMeter JSON Extractor will provide only one value at a time.So, you can either add some logic to verify all the occurrences or multiple verification can be added by specifying the id index ( $.content[?(#.city=="Berlin" || #.city=="Essen")].id[0] --> It gives the first occurrence Id )
If you want to validate multiple fields,then you can write the customized script in Bean Shell Post Processor.(Refer the below link and you will get some idea)
Extracting JSON Response using Bean Shell Postprocessor
If you are using the Bean Shell Post Processor, then required java jar files should be placed in JMeter ClassPath( Folder: apache-jmeter-4.0\lib\ext)

Parsing JSON objects with arbitrary keys in Logstash

Consider a subset of a sample output from http://demo.nginx.com/status:
{
"timestamp": 1516053885198,
"server_zones": {
"hg.nginx.org": {
... // Data for "hg.nginx.org"
},
"trac.nginx.org": {
... // Data for "trac.nginx.org"
}
}
}
The keys "hg.nginx.org" and "track.nginx.org" are quite arbitrary, and I would like to parse them into something meaningful for Elasticsearch. In other words, each key under "server_zones" should be transformed into a separate event. Logstash should thus emit the following events:
[
{
"timestamp": 1516053885198,
"server_zone": "hg.nginx.org",
... // Data for "hg.nginx.org"
},
{
"timestamp": 1516053885198,
"server_zone": "trac.nginx.org",
... // Data for "trac.nginx.org"
}
]
What is the best way to go about doing this?
You can try using the ruby filter. Get the server zones and create a new object using the key value pairs you want to include. From the top of my head, something like below should work. Obviously you then need to map the object to your field in the index. Change the snipped based on your custom format i.e. build the array or object as you want.
filter {
ruby {
code => " time = event.get('timestamp')
myArr = []
event.to_hash.select {|k,v| ['server_zones'].include?(k)}.each do |key,value|
myCustomObject = {}
#map the key value pairs into myCustomObject
myCustomObject[timestamp] = time
myCustomObject[key] = value
myArr.push(myCustomObject) #you'd probably move this out based on nesting level
end
map['my_indexed_field'] = myArr
"
}
}
In the output section use rubydebug for error debugging
output {
stdout { codec => rubydebug }
}